初始化代码
This commit is contained in:
79
vendor/topthink/think-swoole/src/command/RpcInterface.php
vendored
Normal file
79
vendor/topthink/think-swoole/src/command/RpcInterface.php
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace think\swoole\command;
|
||||
|
||||
use Nette\PhpGenerator\ClassType;
|
||||
use Nette\PhpGenerator\Helpers;
|
||||
use Nette\PhpGenerator\PhpFile;
|
||||
use think\console\Command;
|
||||
use think\helper\Arr;
|
||||
use think\swoole\contract\rpc\ParserInterface;
|
||||
use think\swoole\exception\RpcResponseException;
|
||||
use think\swoole\rpc\client\Client;
|
||||
use think\swoole\rpc\Error;
|
||||
use think\swoole\rpc\JsonParser;
|
||||
use think\swoole\rpc\server\Dispatcher;
|
||||
|
||||
class RpcInterface extends Command
|
||||
{
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('rpc:interface')
|
||||
->setDescription('Generate Rpc Service Interfaces');
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
go(function () {
|
||||
$clients = $this->app->config->get('swoole.rpc.client', []);
|
||||
|
||||
$file = new PhpFile;
|
||||
$file->addComment('This file is auto-generated.');
|
||||
$file->setStrictTypes();
|
||||
|
||||
foreach ($clients as $name => $config) {
|
||||
|
||||
$client = new Client($config['host'], $config['port']);
|
||||
|
||||
$response = $client->sendAndRecv(Dispatcher::ACTION_INTERFACE);
|
||||
|
||||
$parserClass = Arr::get($config, 'parser', JsonParser::class);
|
||||
/** @var ParserInterface $parser */
|
||||
$parser = new $parserClass;
|
||||
|
||||
$result = $parser->decodeResponse($response);
|
||||
|
||||
if ($result instanceof Error) {
|
||||
throw new RpcResponseException($result);
|
||||
}
|
||||
|
||||
$namespace = $file->addNamespace("rpc\\contract\\${name}");
|
||||
foreach ($result as $interface => $methods) {
|
||||
$class = $namespace->addInterface($interface);
|
||||
$class->addConstant("RPC", $name);
|
||||
|
||||
foreach ($methods as $methodName => ['parameters' => $parameters, 'returnType' => $returnType, 'comment' => $comment]) {
|
||||
$method = $class->addMethod($methodName)
|
||||
->setVisibility(ClassType::VISIBILITY_PUBLIC)
|
||||
->setComment(Helpers::unformatDocComment($comment))
|
||||
->setReturnType($returnType);
|
||||
|
||||
foreach ($parameters as $parameter) {
|
||||
|
||||
$param = $method->addParameter($parameter['name'])
|
||||
->setTypeHint($parameter['type']);
|
||||
|
||||
if (array_key_exists("default", $parameter)) {
|
||||
$param->setDefaultValue($parameter['default']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file_put_contents($this->app->getBasePath() . 'rpc.php', $file);
|
||||
|
||||
$this->output->writeln('<info>Succeed!</info>');
|
||||
});
|
||||
}
|
||||
}
|
||||
154
vendor/topthink/think-swoole/src/command/Server.php
vendored
Normal file
154
vendor/topthink/think-swoole/src/command/Server.php
vendored
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\swoole\command;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\input\Argument;
|
||||
use think\swoole\Manager;
|
||||
use think\swoole\PidManager;
|
||||
|
||||
/**
|
||||
* Swoole HTTP 命令行,支持操作:start|stop|restart|reload
|
||||
* 支持应用配置目录下的swoole.php文件进行参数配置
|
||||
*/
|
||||
class Server extends Command
|
||||
{
|
||||
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('swoole')
|
||||
->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload", 'start')
|
||||
->setDescription('Swoole HTTP Server for ThinkPHP');
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$this->checkEnvironment();
|
||||
|
||||
$action = $this->input->getArgument('action');
|
||||
|
||||
if (in_array($action, ['start', 'stop', 'reload', 'restart'])) {
|
||||
$this->app->invokeMethod([$this, $action], [], true);
|
||||
} else {
|
||||
$this->output->writeln("<error>Invalid argument action:{$action}, Expected start|stop|restart|reload .</error>");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查环境
|
||||
*/
|
||||
protected function checkEnvironment()
|
||||
{
|
||||
if (!extension_loaded('swoole')) {
|
||||
$this->output->error('Can\'t detect Swoole extension installed.');
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!version_compare(swoole_version(), '4.3.1', 'ge')) {
|
||||
$this->output->error('Your Swoole version must be higher than `4.3.1`.');
|
||||
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动server
|
||||
* @access protected
|
||||
* @param Manager $manager
|
||||
* @param PidManager $pidManager
|
||||
* @return void
|
||||
*/
|
||||
protected function start(Manager $manager, PidManager $pidManager)
|
||||
{
|
||||
if ($pidManager->isRunning()) {
|
||||
$this->output->writeln('<error>swoole http server process is already running.</error>');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->output->writeln('Starting swoole http server...');
|
||||
|
||||
$host = $manager->getConfig('server.host');
|
||||
$port = $manager->getConfig('server.port');
|
||||
|
||||
$this->output->writeln("Swoole http server started: <http://{$host}:{$port}>");
|
||||
$this->output->writeln('You can exit with <info>`CTRL-C`</info>');
|
||||
|
||||
$manager->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* 柔性重启server
|
||||
* @access protected
|
||||
* @param PidManager $manager
|
||||
* @return void
|
||||
*/
|
||||
protected function reload(PidManager $manager)
|
||||
{
|
||||
if (!$manager->isRunning()) {
|
||||
$this->output->writeln('<error>no swoole http server process running.</error>');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->output->writeln('Reloading swoole http server...');
|
||||
|
||||
if (!$manager->killProcess(SIGUSR1)) {
|
||||
$this->output->error('> failure');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->output->writeln('> success');
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止server
|
||||
* @access protected
|
||||
* @param PidManager $manager
|
||||
* @return void
|
||||
*/
|
||||
protected function stop(PidManager $manager)
|
||||
{
|
||||
if (!$manager->isRunning()) {
|
||||
$this->output->writeln('<error>no swoole http server process running.</error>');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->output->writeln('Stopping swoole http server...');
|
||||
|
||||
$isRunning = $manager->killProcess(SIGTERM, 15);
|
||||
|
||||
if ($isRunning) {
|
||||
$this->output->error('Unable to stop the swoole_http_server process.');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->output->writeln('> success');
|
||||
}
|
||||
|
||||
/**
|
||||
* 重启server
|
||||
* @access protected
|
||||
* @param Manager $manager
|
||||
* @param PidManager $pidManager
|
||||
* @return void
|
||||
*/
|
||||
protected function restart(Manager $manager, PidManager $pidManager)
|
||||
{
|
||||
if ($pidManager->isRunning()) {
|
||||
$this->stop($pidManager);
|
||||
}
|
||||
|
||||
$this->start($manager, $pidManager);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user