Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Command #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 46 additions & 32 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,27 @@
*/
namespace Simps;

use Simps\Console\Command;
use Simps\Utils\Env;
use Swoole\Coroutine;

class Application
{
/**
* @var string
*/
protected static $version = '1.0.5';
protected static $version = '2.0.0-dev';

protected $console = [
// Server相关
\Simps\Console\Server\StatusCommand::class,
\Simps\Console\Server\StartCommand::class,
\Simps\Console\Server\StopCommand::class,
\Simps\Console\Server\RestartCommand::class,

// 其他命令
\Simps\Console\Other\HelpCommand::class,
];

public static function welcome()
{
Expand Down Expand Up @@ -48,41 +63,40 @@ public static function echoError($msg)
self::println('[' . date('Y-m-d H:i:s') . '] [ERROR] ' . "\033[31m{$msg}\033[0m");
}

public static function run()
/**
* @return string[]
*/
public function getConsole()
{
return $this->console;
}

public function start()
{
// 初始化环境变量
container(Env::class);
// 获取所有命令
$this->console = array_merge($this->console, config('console', []));

self::welcome();
global $argv;
$count = count($argv);
$funcName = $argv[$count - 1];
$command = explode(':', $funcName);
switch ($command[0]) {
case 'http':
$className = \Simps\Server\Http::class;
break;
case 'ws':
$className = \Simps\Server\WebSocket::class;
break;
case 'mqtt':
$className = \Simps\Server\MqttServer::class;
break;
case 'main':
$className = \Simps\Server\MainServer::class;
break;
default:
// 用户自定义server
$configs = config('servers', []);
if (isset($configs[$command[0]], $configs[$command[0]]['class_name'])) {
$className = $configs[$command[0]]['class_name'];
} else {
exit(self::echoError("command {$command[0]} is not exist, you can use {$argv[0]} [http:start, ws:start, mqtt:start, main:start]"));
$command = $argv[1] ?? 'help';
$params = isset($argv[2]) ? array_slice($argv, 2) : [];
foreach ($this->console as $cls) {
$obj = new $cls($this, $params);
if ($obj instanceof Command) {
if ($command == $obj->getCommand()) {
if ($obj->getCoroutine()) {
Coroutine\run(function () use ($obj) {
$obj->handle();
});
} else {
$obj->handle();
}
return;
}
}
}
switch ($command[1]) {
case 'start':
new $className();
break;
default:
self::echoError("use {$argv[0]} [http:start, ws:start, mqtt:start, main:start]");
}
self::echoError("Command `{$command}` not found");
}
}
71 changes: 71 additions & 0 deletions src/Console/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);
/**
* This file is part of Simps.
*
* @link https://simps.io
* @document https://doc.simps.io
* @license https://github.com/simple-swoole/simps/blob/master/LICENSE
*/
namespace Simps\Console;

use Simps\Application;

abstract class Command
{
public $pid = BASE_PATH . '/runtime/%s.pid';

/** @var Application */
protected $app;

protected $help = 'No description';

protected $command = false;

protected $params = [];

protected $show = true;

protected $coroutine = false;

public function __construct(Application $app, $params = [])
{
$this->app = $app;
$this->params = $params;
}

public function getCoroutine()
{
return $this->coroutine;
}

/**
* @return bool
*/
public function getShow()
{
return $this->show;
}

/**
* @return string
*/
public function getHelp()
{
return $this->help;
}

/**
* @return string
*/
public function getCommand()
{
return $this->command;
}

/**
* 运行命令.
*/
abstract public function handle();
}
37 changes: 37 additions & 0 deletions src/Console/Other/HelpCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);
/**
* This file is part of Simps.
*
* @link https://simps.io
* @document https://doc.simps.io
* @license https://github.com/simple-swoole/simps/blob/master/LICENSE
*/
namespace Simps\Console\Other;

use Simps\Console\Command;

class HelpCommand extends Command
{
protected $command = 'help';

protected $help = 'Show all commands';

public function handle()
{
$commands = [];
$list = $this->app->getConsole();
foreach ($list as $cls) {
$obj = new $cls($this->app);
if ($obj->getShow()) {
$k = $obj->getCommand();
$v = $obj->getHelp();
$commands[$k] = $v;
}
}
foreach ($commands as $command => $help) {
echo "{$command}\t\t`{$help}`\n";
}
}
}
29 changes: 29 additions & 0 deletions src/Console/Server/RestartCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);
/**
* This file is part of Simps.
*
* @link https://simps.io
* @document https://doc.simps.io
* @license https://github.com/simple-swoole/simps/blob/master/LICENSE
*/
namespace Simps\Console\Server;

use Simps\Console\Command;

class RestartCommand extends Command
{
protected $command = 'restart';

protected $help = 'Restart the specified service';

public function handle()
{
$ser = $this->params[0] ?? 'http';
$pid = (int) @file_get_contents(sprintf($this->pid, $ser));
if ($pid > 0) {
\Swoole\Process::kill($pid, SIGUSR1);
}
}
}
53 changes: 53 additions & 0 deletions src/Console/Server/StartCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);
/**
* This file is part of Simps.
*
* @link https://simps.io
* @document https://doc.simps.io
* @license https://github.com/simple-swoole/simps/blob/master/LICENSE
*/
namespace Simps\Console\Server;

use Simps\Application;
use Simps\Console\Command;

class StartCommand extends Command
{
protected $command = 'start';

protected $help = 'Start the specified service';

public function handle()
{
$ser = $this->params[0] ?? 'http';
$met = 'start';

// 检测服务是否已经启动
$pid = (int) @file_get_contents(sprintf($this->pid, $ser));
if ($pid > 0) {
$r = \Swoole\Process::kill($pid, 0);
if ($r) {
Application::echoError("Service `{$ser}` is running");
return;
}
}

// 获取服务配置并启动
$config = config('servers', []);
$config = $config[$ser] ?? null;
if (! $config) {
Application::echoError('Service does not exist');
exit;
}
$config['name'] = $ser;
$cls = $config['provider'];
$obj = new $cls($config);
if (method_exists($obj, $met)) {
$obj->{$met}();
} else {
Application::echoError('Service does not exist');
}
}
}
42 changes: 42 additions & 0 deletions src/Console/Server/StatusCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);
/**
* This file is part of Simps.
*
* @link https://simps.io
* @document https://doc.simps.io
* @license https://github.com/simple-swoole/simps/blob/master/LICENSE
*/
namespace Simps\Console\Server;

use Simps\Application;

class StatusCommand extends \Simps\Console\Command
{
protected $command = 'status';

protected $help = 'Show all services list';

public function handle()
{
$config = config('servers', []);
foreach ($config as $ser => $v) {
$status = $this->getServerStatus($ser) ? 'runtime' : 'stop';
Application::echoSuccess("Service `{$ser}` {$status}");
}
}

/**
* @param string $ser
* @return bool|mixed
*/
protected function getServerStatus($ser)
{
$pid = (int) @file_get_contents(sprintf($this->pid, $ser));
if ($pid > 0) {
return \Swoole\Process::kill($pid, 0);
}
return false;
}
}
29 changes: 29 additions & 0 deletions src/Console/Server/StopCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);
/**
* This file is part of Simps.
*
* @link https://simps.io
* @document https://doc.simps.io
* @license https://github.com/simple-swoole/simps/blob/master/LICENSE
*/
namespace Simps\Console\Server;

use Simps\Console\Command;

class StopCommand extends Command
{
protected $command = 'stop';

protected $help = 'Close the specified service';

public function handle()
{
$ser = $this->params[0] ?? 'http';
$pid = (int) @file_get_contents(sprintf($this->pid, $ser));
if ($pid > 0) {
\Swoole\Process::kill($pid, SIGTERM);
}
}
}