diff --git a/src/ZM/Command/Module/ModuleCommand.php b/src/ZM/Command/Module/ModuleCommand.php new file mode 100644 index 00000000..f8134b5b --- /dev/null +++ b/src/ZM/Command/Module/ModuleCommand.php @@ -0,0 +1,46 @@ +addOption('env', null, InputOption::VALUE_REQUIRED, '设置环境类型 (production, development, staging)', ''); + $this->addOption('log-theme', null, InputOption::VALUE_REQUIRED, '改变终端的主题配色', 'default'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + ZMConfig::setDirectory(DataProvider::getSourceRootDir() . '/config'); + ZMConfig::setEnv($input->getOption('env')); + if (ZMConfig::get('global') === false) { + exit(zm_internal_errcode('E00007') . 'Global config load failed: ' . ZMConfig::$last_error . "\nPlease init first!\nSee: https://github.com/zhamao-robot/zhamao-framework/issues/37\n"); + } + + // 定义常量 + /** @noinspection PhpIncludeInspection */ + include_once DataProvider::getFrameworkRootDir() . '/src/ZM/global_defines.php'; + + Console::init( + ZMConfig::get('global', 'info_level') ?? 2, + null, + $input->getOption('log-theme'), + ($o = ZMConfig::get('console_color')) === false ? [] : $o + ); + + $timezone = ZMConfig::get('global', 'timezone') ?? 'Asia/Shanghai'; + date_default_timezone_set($timezone); + return 0; + } +} diff --git a/src/ZM/Command/Module/ModuleListCommand.php b/src/ZM/Command/Module/ModuleListCommand.php index 8c736f3f..0a9074f8 100644 --- a/src/ZM/Command/Module/ModuleListCommand.php +++ b/src/ZM/Command/Module/ModuleListCommand.php @@ -4,47 +4,31 @@ declare(strict_types=1); namespace ZM\Command\Module; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use ZM\Config\ZMConfig; use ZM\Console\Console; +use ZM\Exception\ZMException; use ZM\Utils\DataProvider; use ZM\Utils\Manager\ModuleManager; -class ModuleListCommand extends Command +class ModuleListCommand extends ModuleCommand { // the name of the command (the part after "bin/console") protected static $defaultName = 'module:list'; protected function configure() { + parent::configure(); $this->setDescription('查看所有模块信息'); $this->setHelp('此功能将会把炸毛框架的模块列举出来。'); - // ... } + /** + * @throws ZMException + */ protected function execute(InputInterface $input, OutputInterface $output): int { - ZMConfig::setDirectory(DataProvider::getSourceRootDir() . '/config'); - ZMConfig::setEnv($args['env'] ?? ''); - if (ZMConfig::get('global') === false) { - exit(zm_internal_errcode('E00007') . 'Global config load failed: ' . ZMConfig::$last_error . "\nPlease init first!\nSee: https://github.com/zhamao-robot/zhamao-framework/issues/37\n"); - } - - // 定义常量 - /** @noinspection PhpIncludeInspection */ - include_once DataProvider::getFrameworkRootDir() . '/src/ZM/global_defines.php'; - - Console::init( - ZMConfig::get('global', 'info_level') ?? 2, - null, - $args['log-theme'] ?? 'default', - ($o = ZMConfig::get('console_color')) === false ? [] : $o - ); - - $timezone = ZMConfig::get('global', 'timezone') ?? 'Asia/Shanghai'; - date_default_timezone_set($timezone); + parent::execute($input, $output); $list = ModuleManager::getConfiguredModules(); diff --git a/src/ZM/Command/Module/ModulePackCommand.php b/src/ZM/Command/Module/ModulePackCommand.php index 773f662d..71ca0fa7 100644 --- a/src/ZM/Command/Module/ModulePackCommand.php +++ b/src/ZM/Command/Module/ModulePackCommand.php @@ -4,57 +4,41 @@ declare(strict_types=1); namespace ZM\Command\Module; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use ZM\Config\ZMConfig; use ZM\Console\Console; +use ZM\Exception\ZMException; use ZM\Utils\DataProvider; use ZM\Utils\Manager\ModuleManager; -class ModulePackCommand extends Command +class ModulePackCommand extends ModuleCommand { // the name of the command (the part after "bin/console") protected static $defaultName = 'module:pack'; protected function configure() { + parent::configure(); $this->addArgument('module-name', InputArgument::REQUIRED); $this->setDescription('将配置好的模块构建一个phar包'); $this->setHelp('此功能将会把炸毛框架的模块打包为".phar",供发布和执行。'); $this->addOption('target', 'D', InputOption::VALUE_REQUIRED, 'Output Directory | 指定输出目录'); - // ... } + /** + * @throws ZMException + */ protected function execute(InputInterface $input, OutputInterface $output): int { - ZMConfig::setDirectory(DataProvider::getSourceRootDir() . '/config'); - ZMConfig::setEnv($args['env'] ?? ''); - if (ZMConfig::get('global') === false) { - exit(zm_internal_errcode('E00007') . 'Global config load failed: ' . ZMConfig::$last_error . "\nPlease init first!\nSee: https://github.com/zhamao-robot/zhamao-framework/issues/37\n"); - } - - // 定义常量 - include_once DataProvider::getFrameworkRootDir() . '/src/ZM/global_defines.php'; - - Console::init( - ZMConfig::get('global', 'info_level') ?? 2, - null, - $args['log-theme'] ?? 'default', - ($o = ZMConfig::get('console_color')) === false ? [] : $o - ); - - $timezone = ZMConfig::get('global', 'timezone') ?? 'Asia/Shanghai'; - date_default_timezone_set($timezone); - + parent::execute($input, $output); $list = ModuleManager::getConfiguredModules(); if (!isset($list[$input->getArgument('module-name')])) { $output->writeln('不存在模块 ' . $input->getArgument('module-name') . ' !'); return 1; } - $result = ModuleManager::packModule($list[$input->getArgument('module-name')], $input->getOption('target')); + $result = ModuleManager::packModule($list[$input->getArgument('module-name')], $input->getOption('target') ?? (DataProvider::getDataFolder() . '/output')); if ($result) { Console::success('打包完成!'); } else { diff --git a/src/ZM/Command/Module/ModuleUnpackCommand.php b/src/ZM/Command/Module/ModuleUnpackCommand.php index b58d1879..de4f12d2 100644 --- a/src/ZM/Command/Module/ModuleUnpackCommand.php +++ b/src/ZM/Command/Module/ModuleUnpackCommand.php @@ -4,56 +4,32 @@ declare(strict_types=1); namespace ZM\Command\Module; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use ZM\Config\ZMConfig; use ZM\Console\Console; -use ZM\Utils\DataProvider; use ZM\Utils\Manager\ModuleManager; -class ModuleUnpackCommand extends Command +class ModuleUnpackCommand extends ModuleCommand { // the name of the command (the part after "bin/console") protected static $defaultName = 'module:unpack'; protected function configure() { - $this->setDefinition([ - new InputArgument('module-name', InputArgument::REQUIRED), - new InputOption('overwrite-light-cache', null, null, '覆盖现有的LightCache项目'), - new InputOption('overwrite-zm-data', null, null, '覆盖现有的zm_data文件'), - new InputOption('overwrite-source', null, null, '覆盖现有的源码文件'), - new InputOption('ignore-depends', null, null, '解包时忽略检查依赖'), - ]); + parent::configure(); + $this->addOption('overwrite-light-cache', null, null, '覆盖现有的LightCache项目'); + $this->addOption('overwrite-zm-data', null, null, '覆盖现有的zm_data文件'); + $this->addOption('overwrite-source', null, null, '覆盖现有的源码文件'); + $this->addOption('ignore-depends', null, null, '解包时忽略检查依赖'); + $this->addArgument('module-name', InputArgument::REQUIRED, '模块名称'); $this->setDescription('解包一个phar模块到src目录'); $this->setHelp('此功能将phar格式的模块包解包到src目录下。'); - // ... } protected function execute(InputInterface $input, OutputInterface $output): int { - ZMConfig::setDirectory(DataProvider::getSourceRootDir() . '/config'); - ZMConfig::setEnv($args['env'] ?? ''); - if (ZMConfig::get('global') === false) { - exit(zm_internal_errcode('E00007') . 'Global config load failed: ' . ZMConfig::$last_error . "\nPlease init first!\nSee: https://github.com/zhamao-robot/zhamao-framework/issues/37\n"); - } - - // 定义常量 - /** @noinspection PhpIncludeInspection */ - include_once DataProvider::getFrameworkRootDir() . '/src/ZM/global_defines.php'; - - Console::init( - ZMConfig::get('global', 'info_level') ?? 4, - null, - $args['log-theme'] ?? 'default', - ($o = ZMConfig::get('console_color')) === false ? [] : $o - ); - - $timezone = ZMConfig::get('global', 'timezone') ?? 'Asia/Shanghai'; - date_default_timezone_set($timezone); + parent::execute($input, $output); $list = ModuleManager::getPackedModules(); if (!isset($list[$input->getArgument('module-name')])) { diff --git a/src/ZM/Utils/Manager/ModuleManager.php b/src/ZM/Utils/Manager/ModuleManager.php index 386ea197..8da20af6 100644 --- a/src/ZM/Utils/Manager/ModuleManager.php +++ b/src/ZM/Utils/Manager/ModuleManager.php @@ -141,19 +141,20 @@ class ModuleManager /** * 打包模块 - * @param $module + * @param array $module 模块信息 + * @param string $target 目标路径 * @throws ZMException */ - public static function packModule($module): bool + public static function packModule(array $module, string $target): bool { try { $packer = new ModulePacker($module); if (!is_dir(DataProvider::getDataFolder())) { throw new ModulePackException(zm_internal_errcode('E00070') . 'zm_data dir not found!'); } - $path = realpath(DataProvider::getDataFolder() . '/output'); + $path = realpath($target); if ($path === false) { - mkdir($path = DataProvider::getDataFolder() . '/output'); + mkdir($path = $target, 0755, true); } $packer->setOutputPath($path); $packer->setOverride();