add plugin command bootstrappers

This commit is contained in:
crazywhalecc
2023-03-29 21:31:53 +08:00
committed by Jerry
parent eeb3481c5a
commit 5ac92ae210
4 changed files with 25 additions and 11 deletions

View File

@@ -7,6 +7,7 @@ namespace ZM\Command;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use ZM\Framework;
abstract class Command extends \Symfony\Component\Console\Command\Command implements LoggerInterface abstract class Command extends \Symfony\Component\Console\Command\Command implements LoggerInterface
{ {
@@ -62,4 +63,9 @@ abstract class Command extends \Symfony\Component\Console\Command\Command implem
* @return int 命令执行结果 {@see self::SUCCESS} {@see self::FAILURE} {@see self::INVALID} * @return int 命令执行结果 {@see self::SUCCESS} {@see self::FAILURE} {@see self::INVALID}
*/ */
abstract protected function handle(): int; abstract protected function handle(): int;
protected function emitBootstrap(string $class): void
{
Framework::getInstance()->bootstrap($class);
}
} }

View File

@@ -13,16 +13,15 @@ use ZM\Plugin\PluginManager;
abstract class PluginCommand extends Command abstract class PluginCommand extends Command
{ {
protected array $bootstrappers = [
BootStrap\RegisterLogger::class,
Bootstrap\SetInternalTimezone::class,
Bootstrap\LoadConfiguration::class,
Bootstrap\LoadPlugins::class,
];
/** @var null|string 动态插件和 Phar 插件的加载目录 */ /** @var null|string 动态插件和 Phar 插件的加载目录 */
protected ?string $plugin_dir = null; protected ?string $plugin_dir = null;
public function __construct(string $name = null)
{
parent::__construct($name);
$this->emitBootstrap(Bootstrap\LoadPlugins::class);
}
/** /**
* 插件名称合规验证器 * 插件名称合规验证器
*/ */

View File

@@ -79,13 +79,13 @@ final class ConsoleApplication extends Application
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output): int protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output): int
{ {
// 初始化内核 // 初始化内核
$kernel = Framework::getInstance(); $framework = Framework::getInstance();
$kernel->runtime_preferences = $kernel->runtime_preferences $framework->runtime_preferences = $framework->runtime_preferences
->withConfigDir($input->getOption('config-dir')) ->withConfigDir($input->getOption('config-dir'))
->withEnvironment($input->getOption('env')) ->withEnvironment($input->getOption('env'))
->enableDebugMode($input->getOption('debug')) ->enableDebugMode($input->getOption('debug'))
->withLogLevel($input->getOption('log-level')); ->withLogLevel($input->getOption('log-level'));
$kernel->bootstrap(); $framework->bootstrap();
return parent::doRunCommand($command, $input, $output); return parent::doRunCommand($command, $input, $output);
} }

View File

@@ -262,8 +262,17 @@ class Framework
} }
} }
public function bootstrap(): void /**
* 执行初始化的函数列表
*
* @param null|string $bootstrapper 要运行的 bootstrapper
*/
public function bootstrap(?string $bootstrapper = null): void
{ {
if ($bootstrapper !== null) {
(new $bootstrapper())->bootstrap($this->runtime_preferences);
return;
}
foreach ($this->bootstrappers as $bootstrapper) { foreach ($this->bootstrappers as $bootstrapper) {
/* @var Bootstrapper $bootstrapper */ /* @var Bootstrapper $bootstrapper */
(new $bootstrapper())->bootstrap($this->runtime_preferences); (new $bootstrapper())->bootstrap($this->runtime_preferences);