zhamao-framework/src/ZM/ConsoleApplication.php

92 lines
3.8 KiB
PHP
Raw Normal View History

2020-08-31 10:11:06 +08:00
<?php
namespace ZM;
use Exception;
2020-09-29 15:07:43 +08:00
use Symfony\Component\Console\Command\Command;
use TypeError;
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\Config\ZMConfig;
use ZM\Utils\DataProvider;
2020-08-31 10:11:06 +08:00
class ConsoleApplication extends Application
{
public function __construct(string $name = 'UNKNOWN') {
$version = json_decode(file_get_contents(__DIR__ . "/../../composer.json"), true)["version"] ?? "UNKNOWN";
parent::__construct($name, $version);
}
public function initEnv() {
2020-10-03 23:00:18 +08:00
$this->selfCheck();
2020-08-31 10:11:06 +08:00
$this->addCommands([
new RunServerCommand(), //运行主服务的指令控制器
2020-09-29 15:07:43 +08:00
new InitCommand(), //初始化用的用于项目初始化和phar初始化
new PureHttpCommand()
2020-08-31 10:11:06 +08:00
]);
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__ . "/../../"));
elseif (LOAD_MODE == 2) echo "Phar mode: " . WORKING_DIR . PHP_EOL;
if (file_exists(DataProvider::getWorkingDir() . "/vendor/autoload.php")) {
/** @noinspection PhpIncludeInspection */
require_once DataProvider::getWorkingDir() . "/vendor/autoload.php";
}
if (LOAD_MODE == 2) {
// Phar 模式2.0 不提供哦
//require_once FRAMEWORK_DIR . "/vendor/autoload.php";
spl_autoload_register('phar_classloader');
} elseif (LOAD_MODE == 0) {
/** @noinspection PhpIncludeInspection
* @noinspection RedundantSuppression
*/
require_once WORKING_DIR . "/vendor/autoload.php";
}
if (!is_dir(DataProvider::getWorkingDir() . '/src/')) {
die("Unable to find source directory.\nMaybe you need to run \"init\"?");
}
ZMConfig::setDirectory(DataProvider::getWorkingDir().'/config');
ZMConfig::env($args["env"] ?? "");
if(ZMConfig::get("global") === false) die("Global config load failed: ".ZMConfig::$last_error);
$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);
}
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) {
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
private function selfCheck() {
if (!extension_loaded("swoole")) die("Can not find swoole extension.\n");
if (version_compare(SWOOLE_VERSION, "4.4.13") == -1) die("You must install swoole version >= 4.4.13 !");
//if (!extension_loaded("gd")) die("Can not find gd extension.\n");
if (!extension_loaded("sockets")) die("Can not find sockets extension.\n");
if (substr(PHP_VERSION, 0, 1) != "7") die("PHP >=7 required.\n");
//if (!function_exists("curl_exec")) die("Can not find curl extension.\n");
//if (!class_exists("ZipArchive")) die("Can not find Zip extension.\n");
//if (!file_exists(CRASH_DIR . "last_error.log")) die("Can not find log file.\n");
return true;
}
2020-08-31 10:11:06 +08:00
}