zhamao-framework/src/ZM/ConsoleApplication.php

109 lines
3.6 KiB
PHP
Raw Normal View History

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;
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;
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;
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-11-16 15:41:01 +08:00
use ZM\Command\Server\ServerReloadCommand;
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
{
2021-07-04 15:45:30 +08:00
private static $obj = null;
2022-01-08 16:19:43 +08:00
const VERSION_ID = 434;
const VERSION = "2.6.6";
2021-07-04 15:45:30 +08:00
/**
* @throws InitException
*/
public function __construct(string $name = 'UNKNOWN') {
2021-07-04 15:45:30 +08:00
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;
parent::__construct($name, ZM_VERSION);
2020-08-31 10:11:06 +08:00
}
2021-07-04 15:45:30 +08:00
/**
2021-09-01 14:14:00 +08:00
* @param string $with_default_cmd
* @return ConsoleApplication
2021-07-04 15:45:30 +08:00
* @throws InitException
*/
2021-11-16 15:41:01 +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();
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__ . "/../../"));
}
2020-09-29 15:07:43 +08:00
$this->addCommands([
new DaemonStatusCommand(),
new DaemonReloadCommand(),
new DaemonStopCommand(),
new RunServerCommand(), //运行主服务的指令控制器
2021-11-16 15:41:01 +08:00
new ServerStatusCommand(),
new ServerStopCommand(),
new ServerReloadCommand(),
new PureHttpCommand(), //纯HTTP服务器指令
2021-07-04 15:45:30 +08:00
new SystemdGenerateCommand()
]);
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());
}
if (!empty($with_default_cmd)) {
$this->setDefaultCommand($with_default_cmd);
}
return $this;
2020-08-31 10:11:06 +08:00
}
/**
* @param InputInterface|null $input
* @param OutputInterface|null $output
* @return int
*/
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
}
}
}