refactor module command to phpstan-level-1

This commit is contained in:
crazywhalecc
2022-04-02 19:15:03 +08:00
committed by Jerry Ma
parent 182a4e7b06
commit d490892092
5 changed files with 74 additions and 83 deletions

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace ZM\Command\Module;
use Symfony\Component\Console\Command\Command;
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;
abstract class ModuleCommand extends Command
{
protected function configure()
{
$this->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;
}
}

View File

@@ -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();

View File

@@ -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('<error>不存在模块 ' . $input->getArgument('module-name') . ' !</error>');
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 {

View File

@@ -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')])) {

View File

@@ -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();