2020-08-31 10:11:06 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZM;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2021-03-24 23:34:46 +08:00
|
|
|
|
use ZM\Command\CheckConfigCommand;
|
2021-01-29 20:47:00 +08:00
|
|
|
|
use ZM\Command\DaemonReloadCommand;
|
|
|
|
|
|
use ZM\Command\DaemonStatusCommand;
|
|
|
|
|
|
use ZM\Command\DaemonStopCommand;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use ZM\Command\InitCommand;
|
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;
|
2020-09-29 15:07:43 +08:00
|
|
|
|
use ZM\Utils\DataProvider;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
|
|
|
|
|
|
class ConsoleApplication extends Application
|
|
|
|
|
|
{
|
2021-03-24 23:34:46 +08:00
|
|
|
|
const VERSION_ID = 399;
|
|
|
|
|
|
const VERSION = "2.4.0";
|
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-18 14:56:35 +08:00
|
|
|
|
public function initEnv(): ConsoleApplication {
|
2020-10-03 23:00:18 +08:00
|
|
|
|
$this->selfCheck();
|
2020-12-10 16:37:04 +08:00
|
|
|
|
|
2021-03-02 21:24:31 +08:00
|
|
|
|
if (!is_dir(__DIR__ . '/../../vendor')) {
|
|
|
|
|
|
define("LOAD_MODE", 1); // composer项目模式
|
|
|
|
|
|
define("LOAD_MODE_COMPOSER_PATH", getcwd());
|
|
|
|
|
|
} else {
|
|
|
|
|
|
define("LOAD_MODE", 0); // 源码模式
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-29 15:07:43 +08:00
|
|
|
|
//if (LOAD_MODE === 0) $this->add(new BuildCommand()); //只有在git源码模式才能使用打包指令
|
|
|
|
|
|
if (LOAD_MODE === 0) define("WORKING_DIR", getcwd());
|
|
|
|
|
|
elseif (LOAD_MODE == 1) define("WORKING_DIR", realpath(__DIR__ . "/../../"));
|
|
|
|
|
|
if (file_exists(DataProvider::getWorkingDir() . "/vendor/autoload.php")) {
|
|
|
|
|
|
/** @noinspection PhpIncludeInspection */
|
|
|
|
|
|
require_once DataProvider::getWorkingDir() . "/vendor/autoload.php";
|
|
|
|
|
|
}
|
2021-03-02 21:24:31 +08:00
|
|
|
|
if (LOAD_MODE == 0) {
|
2020-12-10 16:37:04 +08:00
|
|
|
|
$composer = json_decode(file_get_contents(DataProvider::getWorkingDir() . "/composer.json"), true);
|
|
|
|
|
|
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";
|
|
|
|
|
|
$r = file_put_contents(DataProvider::getWorkingDir() . "/composer.json", json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
|
|
|
|
|
if ($r !== false) {
|
2021-03-16 01:34:17 +08:00
|
|
|
|
echo "成功添加!请运行 composer dump-autoload !\n";
|
2020-12-10 16:37:04 +08:00
|
|
|
|
exit(1);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
echo "添加失败!请按任意键继续!";
|
|
|
|
|
|
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(), //运行主服务的指令控制器
|
|
|
|
|
|
new InitCommand(), //初始化用的,用于项目初始化和phar初始化
|
|
|
|
|
|
new PureHttpCommand() //纯HTTP服务器指令
|
|
|
|
|
|
]);
|
2021-03-24 23:34:46 +08:00
|
|
|
|
if (LOAD_MODE === 1) {
|
|
|
|
|
|
$this->add(new CheckConfigCommand());
|
|
|
|
|
|
}
|
2020-12-20 18:49:03 +08:00
|
|
|
|
/*
|
|
|
|
|
|
$command_register = ZMConfig::get("global", "command_register_class") ?? [];
|
|
|
|
|
|
foreach ($command_register as $v) {
|
|
|
|
|
|
$obj = new $v();
|
|
|
|
|
|
if (!($obj instanceof Command)) throw new TypeError("Command register class must be extended by Symfony\\Component\\Console\\Command\\Command");
|
|
|
|
|
|
$this->add($obj);
|
|
|
|
|
|
}*/
|
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) {
|
|
|
|
|
|
die("{$e->getMessage()} at {$e->getFile()}({$e->getLine()})");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-10-03 23:00:18 +08:00
|
|
|
|
|
2021-03-18 14:56:35 +08:00
|
|
|
|
private function selfCheck(): bool {
|
2021-02-27 16:19:18 +08:00
|
|
|
|
if (!extension_loaded("swoole")) die("Can not find swoole extension.\nSee: https://github.com/zhamao-robot/zhamao-framework/issues/19\n");
|
2021-03-24 23:34:46 +08:00
|
|
|
|
if (version_compare(SWOOLE_VERSION, "4.5.0") == -1) die("You must install swoole version >= 4.5.0 !");
|
2021-02-24 23:37:00 +08:00
|
|
|
|
if (version_compare(PHP_VERSION, "7.2") == -1) die("PHP >= 7.2 required.");
|
2020-10-03 23:00:18 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|