Skip to content

Commit

Permalink
update: support gen tcp and ws middlware class
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 30, 2019
1 parent f6f0d60 commit 1ae31c3
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 27 deletions.
20 changes: 14 additions & 6 deletions src/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@ class CodeGenerator
'tplFilename' => 'ws-module', // ws-module-user
'outDir' => 'app/WebSocket',
],
// TODO wsMiddleware
// 'wsMiddleware' => [],
'wsMiddleware' => [
'suffix' => 'Middleware',
'namespace' => 'App\\WebSocket\\Middleware',
'tplFilename' => 'ws-middleware',
'outDir' => 'app/WebSocket/Middleware',
],
'wsController' => [
'suffix' => 'Controller',
'namespace' => 'App\\WebSocket\\Controller',
Expand All @@ -138,8 +142,12 @@ class CodeGenerator
'tplFilename' => 'tcp-controller',
'outDir' => 'app/Tpc/Controller',
],
// TODO tcpMiddleware
// 'tcpMiddleware' => [],
'tcpMiddleware' => [
'suffix' => 'Middleware',
'namespace' => 'App\\Tcp\\Middleware',
'tplFilename' => 'tcp-middleware',
'outDir' => 'app/Tcp/Middleware',
],
'userProcess' => [
'suffix' => 'Process',
'namespace' => 'App\\Process',
Expand Down Expand Up @@ -241,7 +249,7 @@ private function writeFile(Input $in, Output $out): int
$info['tplFilename'] = $this->current['tplFilename'];

// $out->writeln("Some Info: \n" . \json_encode($config, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
$out->writeln("Metadata: \n" . json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$out->writeln("<comment>Metadata:</comment> \n" . json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

if (!$saveDir = $in->getArg(1)) {
$saveDir = $this->current['outDir'];
Expand All @@ -257,7 +265,7 @@ private function writeFile(Input $in, Output $out): int
$file = $realpath . '/' . $this->tplData['className'] . '.php';
$yes = $in->sameOpt(['y', 'yes'], false);

$out->writeln("Target File:\n <info>$file</info>\n");
$out->writeln("\nTarget File:\n <info>$file</info>\n");

// vdump($this->tplData);
$renderer = new FileRenderer($this->current);
Expand Down
79 changes: 64 additions & 15 deletions src/Command/GenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function cliCommand(Input $in, Output $out): int
/**
* Generate HTTP controller class
*
* @CommandMapping("http-controller", alias="ctrl, http-ctrl, httpController")
* @CommandMapping("http-controller", alias="ctrl, http-ctrl")
*
* @CommandArgument("name", desc="The class name, don't need suffix and ext. eg: <info>demo</info>")
* @CommandArgument("dir", desc="The class file save dir", default="@app/Http/Controller")
Expand Down Expand Up @@ -116,7 +116,7 @@ public function httpController(Input $in, Output $out): int

/**
* Generate HTTP middleware class
* @CommandMapping("http-middleware", alias="http-mdl, httpmdl, http-middle, httpMiddleware")
* @CommandMapping("http-middleware", alias="http-mdl, httpmdl, http-middle")
*
* @CommandArgument("name", desc="The class name, don't need suffix and ext. eg: <info>demo</info>")
* @CommandArgument("dir", desc="The class file save dir", default="@app/Http/Middleware")
Expand All @@ -132,7 +132,7 @@ public function httpController(Input $in, Output $out): int
* @throws InvalidArgumentException
* @throws TemplateParsingException
* @example
* <info>{fullCommand} demo</info> Gen DemoMiddleware class to Middleware dir
* <info>{fullCommand} demo</info> Gen DemoMiddleware class to Http Middleware dir
*/
public function httpMiddleware(Input $in, Output $out): int
{
Expand Down Expand Up @@ -174,8 +174,8 @@ public function wsModule(Input $in, Output $out): int
}

/**
* Generate WebSocket module/controller class
* @CommandMapping("ws-controller", alias="wsc, ws-ctrl, wsController")
* Generate WebSocket message controller class
* @CommandMapping("ws-controller", alias="wsc, wsctrl, ws-ctrl")
*
* @CommandArgument("name", desc="The class name, don't need suffix and ext. eg: <info>demo</info>")
* @CommandArgument("dir", desc="The class file save dir", default="@app/WebSocket/Controller")
Expand Down Expand Up @@ -207,9 +207,34 @@ public function wsController(Input $in, Output $out): int
return $this->ger->runInCli($in, $out, CodeGenerator::WS_CONTROLLER);
}

/**
* Generate WebSocket middleware class
* @CommandMapping("ws-middleware", alias="wsmdl, ws-mdl, ws-middle")
*
* @CommandArgument("name", desc="The class name, don't need suffix and ext. eg: <info>demo</info>")
* @CommandArgument("dir", desc="The class file save dir", default="@app/WebSocket/Middleware")
*
* @CommandOption("namespace", short="n", desc="The class namespace", default="App\WebSocket\Middleware")
* @CommandOption("suffix", type="string", desc="The class name suffix", default="Middleware")
* @CommandOption("tpl-file", type="string", desc="The template filename", default="ws-middleware.stub")
* @param Input $in
* @param Output $out
*
* @return int
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws TemplateParsingException
* @example
* <info>{fullCommand} demo</info> Gen DemoMiddleware class to WebSocket Middleware dir
*/
public function wsMiddleware(Input $in, Output $out): int
{
return $this->ger->runInCli($in, $out, CodeGenerator::WS_MIDDLEWARE);
}

/**
* Generate RPC service class
* @CommandMapping("rpc-controller", alias="service, rpc-ctrl, rpcController")
* @CommandMapping("rpc-controller", alias="rpcctrl, service, rpc-ctrl")
*
* @CommandArgument("name", desc="The class name, don't need suffix and ext. eg: <info>demo</info>")
* @CommandArgument("dir", desc="The class file save dir", default="@app/Rpc/Service")
Expand All @@ -232,7 +257,7 @@ public function rpcController(Input $in, Output $out): int

/**
* Generate RPC middleware class
* @CommandMapping("rcp-middleware", alias="rpcmdl, rpc-mdl, rpc-middle, rcpMiddleware")
* @CommandMapping("rpc-middleware", alias="rpcmdl, rpc-mdl, rpc-middle")
*
* @CommandArgument("name", desc="The class name, don't need suffix and ext. eg: <info>demo</info>")
* @CommandArgument("dir", desc="The class file save dir", default="@app/Rpc/Middleware")
Expand All @@ -248,16 +273,16 @@ public function rpcController(Input $in, Output $out): int
* @throws InvalidArgumentException
* @throws TemplateParsingException
* @example
* <info>{fullCommand} demo</info> Gen DemoMiddleware class to Middleware dir
* <info>{fullCommand} demo</info> Gen DemoMiddleware class to Rpc Middleware dir
*/
public function rcpMiddleware(Input $in, Output $out): int
public function rpcMiddleware(Input $in, Output $out): int
{
return $this->ger->runInCli($in, $out, CodeGenerator::RPC_MIDDLEWARE);
}

/**
* Generate TCP controller class
* @CommandMapping("tcp-controller", alias="tcpc, tcp-ctrl, tcpController")
* @CommandMapping("tcp-controller", alias="tcpc, tcpctrl, tcp-ctrl")
*
* @CommandArgument("name", desc="The class name, don't need suffix and ext. eg: <info>demo</info>")
* @CommandArgument("dir", desc="The class file save dir", default="@app/Tcp/Service")
Expand All @@ -274,13 +299,37 @@ public function rcpMiddleware(Input $in, Output $out): int
*/
public function tcpController(Input $in, Output $out): int
{
// $out->writeln('un-completed ...');
return $this->ger->runInCli($in, $out, CodeGenerator::TCP_CONTROLLER);
}

/**
* Generate TCP middleware class
* @CommandMapping("tcp-middleware", alias="tcpmdl, tcp-mdl, tcp-middle")
*
* @CommandArgument("name", desc="The class name, don't need suffix and ext. eg: <info>demo</info>")
* @CommandArgument("dir", desc="The class file save dir", default="@app/Tcp/Middleware")
*
* @CommandOption("namespace", short="n", desc="The class namespace", default="App\Tcp\Middleware")
* @CommandOption("suffix", type="string", desc="The class name suffix", default="Middleware")
* @CommandOption("tpl-file", type="string", desc="The template filename", default="tcp-middleware.stub")
* @param Input $in
* @param Output $out
*
* @return int
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws TemplateParsingException
* @example
* <info>{fullCommand} demo</info> Gen DemoMiddleware class to Tcp Middleware dir
*/
public function tcpMiddleware(Input $in, Output $out): int
{
return $this->ger->runInCli($in, $out, CodeGenerator::TCP_MIDDLEWARE);
}

/**
* Generate an event listener class
* @CommandMapping()
* @CommandMapping(alias="event-listener")
*
* @CommandArgument("name", desc="The class name, don't need suffix and ext. eg: <info>demo</info>")
* @CommandArgument("dir", desc="The class file save dir", default="@app/Listener")
Expand Down Expand Up @@ -331,7 +380,7 @@ public function task(Input $in, Output $out): int

/**
* Generate user cronTab task class
* @CommandMapping("task-crontab", alias="taskCrontab")
* @CommandMapping("crontab", alias="task-crontab, taskCrontab")
*
* @CommandArgument("name", desc="The class name, don't need suffix and ext. eg: <info>demo</info>")
* @CommandArgument("dir", desc="The class file save dir", default="@app/Task/Crontab")
Expand All @@ -350,15 +399,15 @@ public function task(Input $in, Output $out): int
* <info>{fullCommand} demo</info> Gen DemoTask class to crontab task dir
*
*/
public function taskCrontab(Input $in, Output $out): int
public function taskCronTab(Input $in, Output $out): int
{
return $this->ger->runInCli($in, $out, CodeGenerator::TASK_CRONTAB);
}

/**
* Generate user custom process class
*
* @CommandMapping()
* @CommandMapping(alias="proc")
*
* @CommandArgument("name", desc="The class name, don't need suffix and ext. eg: <info>demo</info>")
* @CommandArgument("dir", desc="The class file save dir", default="@app/Process")
Expand Down
16 changes: 10 additions & 6 deletions src/Command/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ public function application(Input $input, Output $output): void
'workDir' => $input->getWorkDir(),
]);

$pcr->setOnExecCmd(function (string $cmd) {
Show::colored('> ' . $cmd, 'yellow');
});
if ($input->boolOpt('debug')) {
$pcr->setOnExecCmd(function (string $cmd) {
Show::colored('> ' . $cmd, 'yellow');
});
}

$pcr->validate();
if ($err = $pcr->getError()) {
Expand Down Expand Up @@ -161,9 +163,11 @@ public function component(Input $input, Output $output): void
'noLicense' => $input->getBoolOpt('no-license'),
]);

$ccr->setOnExecCmd(function (string $cmd) {
Show::colored('> ' . $cmd, 'yellow');
});
if ($input->boolOpt('debug')) {
$ccr->setOnExecCmd(function (string $cmd) {
Show::colored('> ' . $cmd, 'yellow');
});
}

if (!$ccr->validate()) {
$output->error($ccr->getError());
Expand Down
35 changes: 35 additions & 0 deletions template/classes/tcp-middleware.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);
{include file="file-header.stub"}
namespace {= namespace};

use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Log\Helper\CLog;
use Swoft\Tcp\Server\Contract\RequestHandlerInterface;
use Swoft\Tcp\Server\Contract\MiddlewareInterface;
use Swoft\Tcp\Server\Contract\RequestInterface;
use Swoft\Tcp\Server\Contract\ResponseInterface;

/**
* Class {= className} - Custom middleware
* @Bean()
*/
class {= className} implements MiddlewareInterface
{
/**
* @param RequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$start = '>before ';
CLog::info('before handle');

$resp = $handler->handle($request);
$resp->setData($start . $resp->getData() . ' after>');

CLog::info('after handle');
return $resp;
}
}
35 changes: 35 additions & 0 deletions template/classes/ws-middleware.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);
{include file="file-header.stub"}
namespace {= namespace};

use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Log\Helper\CLog;
use Swoft\WebSocket\Server\Contract\MessageHandlerInterface;
use Swoft\WebSocket\Server\Contract\MiddlewareInterface;
use Swoft\WebSocket\Server\Contract\RequestInterface;
use Swoft\WebSocket\Server\Contract\ResponseInterface;

/**
* Class {= className} - Custom middleware
* @Bean()
*/
class {= className} implements MiddlewareInterface
{
/**
* @param RequestInterface $request
* @param MessageHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(RequestInterface $request, MessageHandlerInterface $handler): ResponseInterface
{
$start = '>before ';
CLog::info('before handle');

$resp = $handler->handle($request);
$resp->setData($start . $resp->getData() . ' after>');

CLog::info('after handle');
return $resp;
}
}

0 comments on commit 1ae31c3

Please sign in to comment.