2020-08-31 10:11:06 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZM;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2021-06-16 00:17:30 +08:00
|
|
|
|
use Phar;
|
|
|
|
|
|
use ZM\Command\BuildCommand;
|
2021-03-24 23:34:46 +08:00
|
|
|
|
use ZM\Command\CheckConfigCommand;
|
2021-06-16 00:17:30 +08:00
|
|
|
|
use ZM\Command\Daemon\DaemonReloadCommand;
|
|
|
|
|
|
use ZM\Command\Daemon\DaemonStatusCommand;
|
|
|
|
|
|
use ZM\Command\Daemon\DaemonStopCommand;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use ZM\Command\InitCommand;
|
2021-06-16 00:17:30 +08:00
|
|
|
|
use ZM\Command\Module\ModuleListCommand;
|
|
|
|
|
|
use ZM\Command\Module\ModulePackCommand;
|
|
|
|
|
|
use ZM\Command\Module\ModuleUnpackCommand;
|
2020-09-29 15:07:43 +08:00
|
|
|
|
use ZM\Command\PureHttpCommand;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use ZM\Command\RunServerCommand;
|
|
|
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2021-03-25 16:18:09 +08:00
|
|
|
|
use ZM\Command\SystemdCommand;
|
2021-06-16 00:17:30 +08:00
|
|
|
|
use ZM\Console\Console;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
|
|
|
|
|
|
class ConsoleApplication extends Application
|
|
|
|
|
|
{
|
2021-06-16 00:17:30 +08:00
|
|
|
|
const VERSION_ID = 408;
|
|
|
|
|
|
const VERSION = "2.5.0-b1";
|
2021-03-13 15:16:10 +08:00
|
|
|
|
|
2020-12-20 18:49:03 +08:00
|
|
|
|
public function __construct(string $name = 'UNKNOWN') {
|
2021-03-13 15:16:10 +08:00
|
|
|
|
define("ZM_VERSION_ID", self::VERSION_ID);
|
|
|
|
|
|
define("ZM_VERSION", self::VERSION);
|
|
|
|
|
|
parent::__construct($name, ZM_VERSION);
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-27 16:30:15 +08:00
|
|
|
|
public function initEnv($with_default_cmd = ""): ConsoleApplication {
|
2020-10-03 23:00:18 +08:00
|
|
|
|
$this->selfCheck();
|
2020-12-10 16:37:04 +08:00
|
|
|
|
|
2021-03-27 16:30:15 +08:00
|
|
|
|
define("WORKING_DIR", getcwd());
|
2021-06-16 00:17:30 +08:00
|
|
|
|
if (Phar::running() !== "") {
|
|
|
|
|
|
echo "* Running in phar mode.\n";
|
|
|
|
|
|
define("SOURCE_ROOT_DIR", Phar::running());
|
|
|
|
|
|
define("LOAD_MODE", is_dir(SOURCE_ROOT_DIR . "/src/ZM") ? 0 : 1);
|
|
|
|
|
|
define("FRAMEWORK_ROOT_DIR", LOAD_MODE == 1 ? (SOURCE_ROOT_DIR . "/vendor/zhamao/framework") : SOURCE_ROOT_DIR);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
define("SOURCE_ROOT_DIR", WORKING_DIR);
|
|
|
|
|
|
define("LOAD_MODE", is_dir(SOURCE_ROOT_DIR . "/src/ZM") ? 0 : 1);
|
|
|
|
|
|
define("FRAMEWORK_ROOT_DIR", realpath(__DIR__ . "/../../"));
|
|
|
|
|
|
}
|
2021-03-02 21:24:31 +08:00
|
|
|
|
if (LOAD_MODE == 0) {
|
2021-06-16 00:17:30 +08:00
|
|
|
|
$composer = json_decode(file_get_contents(SOURCE_ROOT_DIR . "/composer.json"), true);
|
2020-12-10 16:37:04 +08:00
|
|
|
|
if (!isset($composer["autoload"]["psr-4"]["Module\\"])) {
|
|
|
|
|
|
echo "框架源码模式需要在autoload文件中添加Module目录为自动加载,是否添加?[Y/n] ";
|
|
|
|
|
|
$r = strtolower(trim(fgets(STDIN)));
|
|
|
|
|
|
if ($r === "" || $r === "y") {
|
|
|
|
|
|
$composer["autoload"]["psr-4"]["Module\\"] = "src/Module";
|
|
|
|
|
|
$composer["autoload"]["psr-4"]["Custom\\"] = "src/Custom";
|
2021-03-27 16:30:15 +08:00
|
|
|
|
$r = file_put_contents(WORKING_DIR . "/composer.json", json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
2020-12-10 16:37:04 +08:00
|
|
|
|
if ($r !== false) {
|
2021-03-16 01:34:17 +08:00
|
|
|
|
echo "成功添加!请运行 composer dump-autoload !\n";
|
2021-03-27 16:30:15 +08:00
|
|
|
|
exit(0);
|
2020-12-10 16:37:04 +08:00
|
|
|
|
} else {
|
2021-06-16 00:17:30 +08:00
|
|
|
|
echo zm_internal_errcode("E00006") . "添加失败!请按任意键继续!";
|
2020-12-10 16:37:04 +08:00
|
|
|
|
fgets(STDIN);
|
|
|
|
|
|
exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-09-29 15:07:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-20 18:49:03 +08:00
|
|
|
|
$this->addCommands([
|
2021-01-29 20:47:00 +08:00
|
|
|
|
new DaemonStatusCommand(),
|
|
|
|
|
|
new DaemonReloadCommand(),
|
|
|
|
|
|
new DaemonStopCommand(),
|
2020-12-20 18:49:03 +08:00
|
|
|
|
new RunServerCommand(), //运行主服务的指令控制器
|
2021-03-25 16:18:09 +08:00
|
|
|
|
new PureHttpCommand(), //纯HTTP服务器指令
|
|
|
|
|
|
new SystemdCommand()
|
2020-12-20 18:49:03 +08:00
|
|
|
|
]);
|
2021-03-24 23:34:46 +08:00
|
|
|
|
if (LOAD_MODE === 1) {
|
|
|
|
|
|
$this->add(new CheckConfigCommand());
|
|
|
|
|
|
}
|
2021-06-16 00:17:30 +08:00
|
|
|
|
if (Phar::running() === "") {
|
|
|
|
|
|
$this->add(new BuildCommand());
|
|
|
|
|
|
$this->add(new InitCommand());
|
|
|
|
|
|
$this->add(new ModulePackCommand());
|
|
|
|
|
|
$this->add(new ModuleListCommand());
|
|
|
|
|
|
$this->add(new ModuleUnpackCommand());
|
|
|
|
|
|
}
|
2021-03-27 16:30:15 +08:00
|
|
|
|
if (!empty($with_default_cmd)) {
|
|
|
|
|
|
$this->setDefaultCommand($with_default_cmd);
|
|
|
|
|
|
}
|
2021-01-18 18:08:29 +08:00
|
|
|
|
return $this;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @param InputInterface|null $input
|
|
|
|
|
|
* @param OutputInterface|null $output
|
|
|
|
|
|
* @return int
|
|
|
|
|
|
*/
|
2021-03-18 14:56:35 +08:00
|
|
|
|
public function run(InputInterface $input = null, OutputInterface $output = null): int {
|
2020-08-31 10:11:06 +08:00
|
|
|
|
try {
|
|
|
|
|
|
return parent::run($input, $output);
|
|
|
|
|
|
} catch (Exception $e) {
|
2021-06-16 00:17:30 +08:00
|
|
|
|
die(zm_internal_errcode("E00005") . "{$e->getMessage()} at {$e->getFile()}({$e->getLine()})");
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-10-03 23:00:18 +08:00
|
|
|
|
|
2021-06-16 00:17:30 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 启动炸毛前要做的环境检查项目
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function selfCheck(): void {
|
|
|
|
|
|
if (!extension_loaded("swoole")) die(zm_internal_errcode("E00001") . "Can not find swoole extension.\n");
|
|
|
|
|
|
if (version_compare(SWOOLE_VERSION, "4.5.0") == -1) die(zm_internal_errcode("E00002") . "You must install swoole version >= 4.5.0 !");
|
|
|
|
|
|
if (version_compare(PHP_VERSION, "7.2") == -1) die(zm_internal_errcode("E00003") . "PHP >= 7.2 required.");
|
|
|
|
|
|
if (version_compare(SWOOLE_VERSION, "4.6.7") < 0 && !extension_loaded("pcntl")) {
|
|
|
|
|
|
Console::error(zm_internal_errcode("E00004") . "Swoole 版本必须不低于 4.6.7 或 PHP 安装加载了 pcntl 扩展!");
|
|
|
|
|
|
die();
|
|
|
|
|
|
}
|
2020-10-03 23:00:18 +08:00
|
|
|
|
}
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|