2020-08-31 10:11:06 +08:00
|
|
|
<?php
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 10:11:06 +08:00
|
|
|
|
|
|
|
|
namespace ZM;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2021-06-16 00:17:30 +08:00
|
|
|
use Phar;
|
2022-03-15 18:05:33 +08:00
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2021-06-16 00:17:30 +08:00
|
|
|
use ZM\Command\BuildCommand;
|
2021-03-24 23:34:46 +08:00
|
|
|
use ZM\Command\CheckConfigCommand;
|
2021-07-04 15:45:30 +08:00
|
|
|
use ZM\Command\Generate\SystemdGenerateCommand;
|
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;
|
2021-11-16 15:41:01 +08:00
|
|
|
use ZM\Command\Server\ServerReloadCommand;
|
2022-05-14 23:40:22 +08:00
|
|
|
use ZM\Command\Server\ServerStartCommand;
|
2021-11-16 15:41:01 +08:00
|
|
|
use ZM\Command\Server\ServerStatusCommand;
|
|
|
|
|
use ZM\Command\Server\ServerStopCommand;
|
2021-07-04 15:45:30 +08:00
|
|
|
use ZM\Exception\InitException;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
|
|
|
|
class ConsoleApplication extends Application
|
|
|
|
|
{
|
2022-05-06 16:11:08 +00:00
|
|
|
public const VERSION_ID = 473;
|
2021-03-13 15:16:10 +08:00
|
|
|
|
2022-04-15 23:59:59 +08:00
|
|
|
public const VERSION = '2.8.0';
|
2022-03-15 18:05:33 +08:00
|
|
|
|
|
|
|
|
private static $obj;
|
2021-07-04 15:45:30 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws InitException
|
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
public function __construct(string $name = 'UNKNOWN')
|
|
|
|
|
{
|
|
|
|
|
if (self::$obj !== null) {
|
|
|
|
|
throw new InitException(zm_internal_errcode('E00069') . 'Initializing another Application is not allowed!');
|
|
|
|
|
}
|
|
|
|
|
define('ZM_VERSION_ID', self::VERSION_ID);
|
|
|
|
|
define('ZM_VERSION', self::VERSION);
|
2021-07-04 15:45:30 +08:00
|
|
|
self::$obj = $this;
|
2021-03-13 15:16:10 +08:00
|
|
|
parent::__construct($name, ZM_VERSION);
|
2020-08-31 10:11:06 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-04 15:45:30 +08:00
|
|
|
/**
|
|
|
|
|
* @throws InitException
|
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
public function initEnv(string $with_default_cmd = ''): ConsoleApplication
|
|
|
|
|
{
|
|
|
|
|
if (defined('WORKING_DIR')) {
|
|
|
|
|
throw new InitException();
|
|
|
|
|
}
|
2021-11-02 16:01:24 +08:00
|
|
|
|
|
|
|
|
_zm_env_check();
|
2020-12-10 16:37:04 +08:00
|
|
|
|
2022-03-13 22:50:01 +08:00
|
|
|
// 定义多进程的全局变量
|
|
|
|
|
define('ZM_PROCESS_MASTER', 1);
|
|
|
|
|
define('ZM_PROCESS_MANAGER', 2);
|
|
|
|
|
define('ZM_PROCESS_WORKER', 4);
|
|
|
|
|
define('ZM_PROCESS_USER', 8);
|
|
|
|
|
define('ZM_PROCESS_TASKWORKER', 16);
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
define('WORKING_DIR', getcwd());
|
2022-03-13 22:50:01 +08:00
|
|
|
|
|
|
|
|
if (!is_dir(_zm_pid_dir())) {
|
|
|
|
|
@mkdir(_zm_pid_dir());
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
if (Phar::running() !== '') {
|
2021-06-16 00:17:30 +08:00
|
|
|
echo "* Running in phar mode.\n";
|
2022-03-15 18:05:33 +08:00
|
|
|
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);
|
2021-06-16 00:17:30 +08:00
|
|
|
} else {
|
2022-03-15 18:05:33 +08:00
|
|
|
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-06-16 00:17:30 +08:00
|
|
|
}
|
2020-09-29 15:07:43 +08:00
|
|
|
|
2020-12-20 18:49:03 +08:00
|
|
|
$this->addCommands([
|
2021-11-16 15:41:01 +08:00
|
|
|
new ServerStatusCommand(),
|
|
|
|
|
new ServerReloadCommand(),
|
2022-05-14 23:40:22 +08:00
|
|
|
new ServerStopCommand(),
|
|
|
|
|
new ServerStartCommand(), // 运行主服务的指令控制器
|
2022-03-20 16:18:33 +08:00
|
|
|
new PureHttpCommand(), // 纯HTTP服务器指令
|
2022-03-15 18:05:33 +08:00
|
|
|
new SystemdGenerateCommand(),
|
2020-12-20 18:49:03 +08:00
|
|
|
]);
|
2021-03-24 23:34:46 +08:00
|
|
|
if (LOAD_MODE === 1) {
|
|
|
|
|
$this->add(new CheckConfigCommand());
|
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
if (Phar::running() === '') {
|
2021-06-16 00:17:30 +08:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
2022-03-15 18:05:33 +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) {
|
2022-03-15 18:05:33 +08:00
|
|
|
echo zm_internal_errcode('E00005') . "{$e->getMessage()} at {$e->getFile()}({$e->getLine()})" . PHP_EOL;
|
|
|
|
|
exit(1);
|
2020-08-31 10:11:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|