From 348fdef947efc16510902e16ee58933a063e41af Mon Sep 17 00:00:00 2001 From: Inhere Date: Thu, 12 May 2022 12:45:42 +0800 Subject: [PATCH] :recycle: refactor: refactoring config manage logic --- app/Concern/InitApplicationTrait.php | 21 +++++++++++---- .../Attach/Golang/GenerateStructCmd.php | 2 +- app/Console/CliApplication.php | 18 ++++++------- app/Console/Command/DocCommand.php | 5 ++-- app/Console/Command/RunCommand.php | 2 +- app/Console/Command/ToolCommand.php | 2 ++ app/Console/Controller/Gitx/GitController.php | 3 ++- .../Controller/Gitx/GitFlowController.php | 3 ++- .../Controller/Gitx/GitHubController.php | 3 ++- .../Controller/Gitx/GitLabController.php | 3 ++- app/Console/Controller/JsonController.php | 2 +- app/Console/Controller/PhpController.php | 6 ++--- app/Console/Controller/SelfController.php | 25 ++++++++--------- app/Console/Listener/NotFoundListener.php | 2 +- .../SubCmd/Gitflow/BranchCreateCmd.php | 2 +- app/Console/simple-cmds.php | 10 ------- app/Http/WebApplication.php | 16 ++++++----- app/Kite.php | 27 +++++++++++++++++++ 18 files changed, 92 insertions(+), 60 deletions(-) diff --git a/app/Concern/InitApplicationTrait.php b/app/Concern/InitApplicationTrait.php index b121ade..dd4c825 100644 --- a/app/Concern/InitApplicationTrait.php +++ b/app/Concern/InitApplicationTrait.php @@ -8,6 +8,7 @@ use Inhere\Kite\Lib\Jenkins\JenkinsClient; use Monolog\Handler\RotatingFileHandler; use Monolog\Logger; +use PhpPkg\Config\ConfigBox; use PhpPkg\EasyTpl\EasyTemplate; use PhpPkg\EasyTpl\TextTemplate; use Toolkit\Stdlib\Arr\ArrayHelper; @@ -88,7 +89,9 @@ protected function loadAppConfig(string $runMode, string $workDir = ''): void } $config['__loaded_file'] = $loaded; - $this->setParams($config); + + Kite::config()->loadData($config); + $this->setParams(Kite::config()->getArray('app')); } /** @@ -97,7 +100,7 @@ protected function loadAppConfig(string $runMode, string $workDir = ''): void protected function registerComServices(ObjectBox $box): void { $box->set('logger', function () { - $config = $this->getArrayParam('logger'); + $config = $this->config()->getArray('logger'); $logger = new Logger($config['name'] ?? 'kite'); $handler = new RotatingFileHandler($config['logfile']); @@ -114,19 +117,27 @@ protected function registerComServices(ObjectBox $box): void }); $box->set('glApi', function () { - $config = $this->getArrayParam('gitlab'); + $config = $this->config()->getArray('gitlab'); return new GitLabV4API($config); }); $box->set('ghApi', function () { - $config = $this->getArrayParam('github'); + $config = $this->config()->getArray('github'); return new GitHubV3API($config); }); $box->set('jenkins', function () { - $config = $this->getArrayParam('jenkins'); + $config = $this->config()->getArray('jenkins'); return new JenkinsClient($config); }); } + + /** + * @return ConfigBox + */ + public function config(): ConfigBox + { + return Kite::config(); + } } diff --git a/app/Console/Attach/Golang/GenerateStructCmd.php b/app/Console/Attach/Golang/GenerateStructCmd.php index a18af2e..accaaf5 100644 --- a/app/Console/Attach/Golang/GenerateStructCmd.php +++ b/app/Console/Attach/Golang/GenerateStructCmd.php @@ -114,7 +114,7 @@ protected function execute(Input $input, Output $output): void $lang = GenCodeFactory::LANG_GO; - $config = Kite::cliApp()->getArrayParam('gen_code'); + $config = Kite::config()->getArray('gen_code'); $tplDir = $fs->getOpt('tpl-dir', $config['tplDir'] ?? ''); $tplDir = str_replace('{type}', $lang, $tplDir); diff --git a/app/Console/CliApplication.php b/app/Console/CliApplication.php index a516570..a5f7788 100644 --- a/app/Console/CliApplication.php +++ b/app/Console/CliApplication.php @@ -80,7 +80,7 @@ protected function registerServices(ObjectBox $box): void // override logger, add processor $box->set('logger', function () { - $config = $this->getArrayParam('logger'); + $config = $this->config()->getArray('logger'); $logger = new Logger($config['name'] ?? 'kite'); $logger->pushProcessor(new CliLogProcessor()); @@ -90,40 +90,40 @@ protected function registerServices(ObjectBox $box): void }, true); $box->set('plugManager', function () { - $config = $this->getArrayParam('pluginManager'); + $config = $this->config()->getArray('pluginManager'); return new PluginManager($config); }); $box->set('toolManager', function () { - $config = $this->getArrayParam('toolManager'); + $config = $this->config()->getArray('toolManager'); return new ToolManager($config); }); $box->set('scriptRunner', function () { - $config = $this->getArrayParam('scriptRunner'); - $scripts = $this->getArrayParam('scripts'); + $config = $this->config()->getArray('scriptRunner'); + $scripts = $this->config()->getArray('scripts'); // create object $sr = new ScriptRunner($config); $sr->setScripts($scripts); - $sr->scriptDirs = $this->getArrayParam('scriptDirs'); + $sr->scriptDirs = $this->config()->getArray('scriptDirs'); return $sr; }); $box->set('jumper', function () { - $jumpConf = $this->getArrayParam('jumper'); + $jumpConf = $this->config()->getArray('jumper'); return QuickJump::new($jumpConf); }); // auto proxy setting $box->set('autoProxy', function () { - $autoProxy = $this->getArrayParam('autoProxy'); + $autoProxy = $this->config()->getArray('autoProxy'); return AutoSetProxyEnv::new($autoProxy); }); // $box->set('envLoader', function () { - // $jumpConf = $this->getArrayParam('osEnv'); + // $jumpConf = $this->config()->getArray('osEnv'); // return QuickJump::new($jumpConf); // }); } diff --git a/app/Console/Command/DocCommand.php b/app/Console/Command/DocCommand.php index 2ec173c..103c668 100644 --- a/app/Console/Command/DocCommand.php +++ b/app/Console/Command/DocCommand.php @@ -15,6 +15,7 @@ use Inhere\Console\Util\Helper; use Inhere\Kite\Component\CliMarkdown; use Inhere\Kite\Helper\AppHelper; +use Inhere\Kite\Kite; use Inhere\Kite\Lib\ManDoc\DocTopic; use Inhere\Kite\Lib\ManDoc\Document; use Toolkit\Cli\Color; @@ -68,7 +69,7 @@ protected function configure(): void $fs = $this->getFlags(); $lang = Document::DEF_LANG; - $conf = $this->app->getArrayParam('manDocs'); + $conf = Kite::config()->getArray('manDocs'); if (!empty($conf['lang'])) { $lang = $conf['lang']; } @@ -104,7 +105,7 @@ protected function configure(): void */ private function prepareManDoc(): Document { - $info = $this->app->getArrayParam('manDocs'); + $info = Kite::config()->getArray('manDocs'); $paths = $info['paths'] ?? []; $lang = $this->flags->getOpt('lang'); diff --git a/app/Console/Command/RunCommand.php b/app/Console/Command/RunCommand.php index 7dcf4b6..6f8b2c1 100644 --- a/app/Console/Command/RunCommand.php +++ b/app/Console/Command/RunCommand.php @@ -113,7 +113,7 @@ protected function execute(Input $input, Output $output) // proxy $openProxy = $this->flags->getOpt('proxy'); - $proxyEnv = $this->app->getArrayParam('proxyEnv'); + $proxyEnv = Kite::config()->getArray('proxyEnv'); if ($openProxy && $proxyEnv) { Show::aList($proxyEnv, 'Set Proxy ENV From Config: "proxyEnv"', [ 'ucFirst' => false, diff --git a/app/Console/Command/ToolCommand.php b/app/Console/Command/ToolCommand.php index 1c7dc2f..bff8072 100644 --- a/app/Console/Command/ToolCommand.php +++ b/app/Console/Command/ToolCommand.php @@ -16,6 +16,7 @@ use Inhere\Kite\Console\SubCmd\ToolCmd\HashHmacCommand; use Inhere\Kite\Console\SubCmd\ToolCmd\InstallCommand; use Inhere\Kite\Console\SubCmd\ToolCmd\ListToolCommand; +use Inhere\Kite\Console\SubCmd\ToolCmd\LnCommand; use Inhere\Kite\Console\SubCmd\ToolCmd\UpdateCommand; /** @@ -30,6 +31,7 @@ protected function subCommands(): array { return [ OpenCmd::class, + LnCommand::class, HashHmacCommand::class, InstallCommand::class, UpdateCommand::class, diff --git a/app/Console/Controller/Gitx/GitController.php b/app/Console/Controller/Gitx/GitController.php index 88ab761..c0d613e 100644 --- a/app/Console/Controller/Gitx/GitController.php +++ b/app/Console/Controller/Gitx/GitController.php @@ -20,6 +20,7 @@ use Inhere\Kite\Console\Manager\GitBranchManager; use Inhere\Kite\Helper\AppHelper; use Inhere\Kite\Helper\GitUtil; +use Inhere\Kite\Kite; use PhpGit\Changelog\Filter\KeywordsFilter; use PhpGit\Changelog\Formatter\GithubReleaseFormatter; use PhpGit\Changelog\Formatter\SimpleFormatter; @@ -121,7 +122,7 @@ protected function getOptions(): array protected function beforeRun(): void { if ($this->app && !isset($this->settings)) { - $this->settings = DataObject::new($this->app->getArrayParam('git')); + $this->settings = DataObject::new(Kite::config()->getArray('git')); } if ($workdir = $this->flags->getOpt('workdir')) { diff --git a/app/Console/Controller/Gitx/GitFlowController.php b/app/Console/Controller/Gitx/GitFlowController.php index eb2f092..de78544 100644 --- a/app/Console/Controller/Gitx/GitFlowController.php +++ b/app/Console/Controller/Gitx/GitFlowController.php @@ -15,6 +15,7 @@ use Inhere\Kite\Common\CmdRunner; use Inhere\Kite\Console\SubCmd\Gitflow\BranchCreateCmd; use Inhere\Kite\Helper\GitUtil; +use Inhere\Kite\Kite; use Throwable; use Toolkit\PFlag\FlagsParser; use function array_keys; @@ -75,7 +76,7 @@ protected function getOptions(): array protected function configure(): void { - $this->initParams($this->app->getArrayParam('gitflow')); + $this->initParams(Kite::config()->getArray('gitflow')); $this->forkRemote = $this->params->getString('forkRemote'); $this->mainRemote = $this->params->getString('mainRemote'); diff --git a/app/Console/Controller/Gitx/GitHubController.php b/app/Console/Controller/Gitx/GitHubController.php index 905c008..ef11ba7 100644 --- a/app/Console/Controller/Gitx/GitHubController.php +++ b/app/Console/Controller/Gitx/GitHubController.php @@ -17,6 +17,7 @@ use Inhere\Kite\Common\GitLocal\GitHub; use Inhere\Kite\Console\Component\RedirectToGitGroup; use Inhere\Kite\Helper\AppHelper; +use Inhere\Kite\Kite; use PhpPkg\Http\Client\Client; use Throwable; use Toolkit\PFlag\FlagsParser; @@ -82,7 +83,7 @@ private function getGithub(): GitHub protected function beforeRun(): void { if ($this->app && !$this->settings) { - $this->settings = $this->app->getArrayParam('github'); + $this->settings = Kite::config()->getArray('github'); } } diff --git a/app/Console/Controller/Gitx/GitLabController.php b/app/Console/Controller/Gitx/GitLabController.php index 38fe5f4..49043ed 100644 --- a/app/Console/Controller/Gitx/GitLabController.php +++ b/app/Console/Controller/Gitx/GitLabController.php @@ -23,6 +23,7 @@ use Inhere\Kite\Console\SubCmd\Gitflow\BranchCreateCmd; use Inhere\Kite\Helper\AppHelper; use Inhere\Kite\Helper\GitUtil; +use Inhere\Kite\Kite; use Throwable; use Toolkit\PFlag\FlagsParser; use Toolkit\Stdlib\Str; @@ -122,7 +123,7 @@ private function getGitlab(): GitLab protected function beforeRun(): void { if ($this->app && !$this->settings) { - $this->settings = $this->app->getArrayParam('gitlab'); + $this->settings = Kite::config()->getArray('gitlab'); } if ($workdir = $this->flags->getOpt('workdir')) { diff --git a/app/Console/Controller/JsonController.php b/app/Console/Controller/JsonController.php index 5e497e2..4ca294e 100644 --- a/app/Console/Controller/JsonController.php +++ b/app/Console/Controller/JsonController.php @@ -302,7 +302,7 @@ public function toClassCommand(FlagsParser $fs, Output $output): void throw new InvalidArgumentException('empty input json(5) text for handle'); } - $config = Kite::cliApp()->getArrayParam('json_toClass'); + $config = Kite::config()->getArray('json_toClass'); $tplDir = $fs->getOpt('tpl-dir', $config['tplDir'] ?? ''); $tplDir = str_replace('{type}', $type, $tplDir); diff --git a/app/Console/Controller/PhpController.php b/app/Console/Controller/PhpController.php index b119e51..3fa4d5a 100644 --- a/app/Console/Controller/PhpController.php +++ b/app/Console/Controller/PhpController.php @@ -17,10 +17,10 @@ use Inhere\Kite\Common\Cmd; use Inhere\Kite\Common\CmdRunner; use Inhere\Kite\Common\GitLocal\GitHub; -use Inhere\Kite\Console\Component\Clipboard; use Inhere\Kite\Console\Component\ContentsAutoReader; use Inhere\Kite\Helper\AppHelper; use Inhere\Kite\Helper\KiteUtil; +use Inhere\Kite\Kite; use InvalidArgumentException; use Toolkit\PFlag\FlagsParser; use Toolkit\Stdlib\Json; @@ -36,12 +36,10 @@ use function is_numeric; use function ob_get_clean; use function ob_start; -use function preg_quote; use function sprintf; use function str_contains; use function strlen; use function trim; -use function vdump; /** * Class GitGroup @@ -302,7 +300,7 @@ public function csFixCommand(FlagsParser $fs, Output $output): void */ public function serveCommand(FlagsParser $fs, Output $output): void { - $conf = $this->app->getArrayParam('php_serve'); + $conf = Kite::config()->getArray('php_serve'); if ($conf) { $conf = array_merge(self::DEF_SERVE_CONF, $conf); diff --git a/app/Console/Controller/SelfController.php b/app/Console/Controller/SelfController.php index 03377e4..723515f 100644 --- a/app/Console/Controller/SelfController.php +++ b/app/Console/Controller/SelfController.php @@ -17,10 +17,8 @@ use Toolkit\PFlag\FlagsParser; use Toolkit\Stdlib\OS; use Toolkit\Stdlib\Php; -use Toolkit\Stdlib\Util\PhpDotEnv; use Toolkit\Sys\Sys; use Toolkit\Sys\Util\ShellUtil; -use function array_keys; use function array_merge; use function count; use function is_scalar; @@ -84,17 +82,16 @@ protected function beforeRun(): void */ public function infoCommand(Input $input, Output $output): void { - $app = $this->getApp(); - $conf = $app->getConfig(); + $cfg = Kite::config(); $output->mList([ 'kite' => [ - 'root dir' => $conf['rootPath'], + 'root dir' => $cfg->getString('app.rootPath'), 'work dir' => $input->getWorkDir(), - 'script count' => count($conf['scripts']), + 'script count' => count($cfg->getArray('scripts')), 'plugin dirs' => Kite::plugManager()->getPluginDirs(), - '.env files' => PhpDotEnv::global()->getLoadedFiles(), - 'config files' => $conf['__loaded_file'], + '.env files' => Kite::dotenv()->getLoadedFiles(), + 'config files' => $cfg['__loaded_file'], ], 'system' => [ 'OS name' => OS::name(), @@ -147,9 +144,9 @@ public function aliasesCommand(Output $output): void 'titlePos' => Title::POS_MIDDLE, ]); - $aliases = $this->getApp()->getArrayParam('aliases'); - + $aliases = Kite::config()->getArray('aliases'); $result = JSONPretty::prettyData($aliases); + $output->write($result); } @@ -169,13 +166,13 @@ public function aliasesCommand(Output $output): void */ public function configCommand(FlagsParser $fs, Output $output): void { - $app = $this->getApp(); + $cfg = Kite::config(); if ($fs->getOpt('keys')) { - $output->aList(array_keys($app->getConfig()), 'Keys of config'); + $output->aList($cfg->getKeys(), 'Keys of config'); return; } - $conf = $app->getConfig(); + $conf = $cfg->getData(); $key = $fs->getArg(0); // show all config @@ -335,7 +332,7 @@ public function updateCommand(FlagsParser $fs, Output $output): void */ public function webuiCommand(FlagsParser $fs, Output $output): void { - $this->webUi = array_merge($this->webUi, $this->app->getArrayParam('webui')); + $this->webUi = array_merge($this->webUi, Kite::config()->getArray('webui')); // vdump(BASE_PATH, $this->webUi); $svrAddr = $fs->getOpt('addr', $this->webUi['addr']); diff --git a/app/Console/Listener/NotFoundListener.php b/app/Console/Listener/NotFoundListener.php index 511ed41..cb7e3ce 100644 --- a/app/Console/Listener/NotFoundListener.php +++ b/app/Console/Listener/NotFoundListener.php @@ -28,7 +28,7 @@ final class NotFoundListener */ public function __invoke(string $cmd, CliApplication $app): bool { - $aliases = $app->getArrayParam('aliases'); + $aliases = Kite::config()->getArray('aliases'); // - is an command alias. if ($aliases && isset($aliases[$cmd])) { diff --git a/app/Console/SubCmd/Gitflow/BranchCreateCmd.php b/app/Console/SubCmd/Gitflow/BranchCreateCmd.php index 62957e8..93d0967 100644 --- a/app/Console/SubCmd/Gitflow/BranchCreateCmd.php +++ b/app/Console/SubCmd/Gitflow/BranchCreateCmd.php @@ -35,7 +35,7 @@ public static function aliases(): array protected function configure(): void { - $this->initParams(Kite::cliApp()->getArrayParam('gitflow')); + $this->initParams(Kite::config()->getArray('gitflow')); $this->forkRemote = $this->params->getString('forkRemote'); $this->mainRemote = $this->params->getString('mainRemote'); diff --git a/app/Console/simple-cmds.php b/app/Console/simple-cmds.php index 2f05b86..1145949 100644 --- a/app/Console/simple-cmds.php +++ b/app/Console/simple-cmds.php @@ -8,16 +8,6 @@ use Toolkit\PFlag\FlagsParser; use Toolkit\Sys\Sys; -$app->addCommand('ln', function ($fs) { - vdump($fs); -}, [ - 'desc' => 'run ln command', - 'options' => [ - 's, src, source' => 'the source file path', - 't, dst, target' => 'the target link file path', - ], -]); - $app->addCommand('which', function (FlagsParser $fs, Output $output) { $name = $fs->getArg('binName'); $path = Sys::findExecutable($name); diff --git a/app/Http/WebApplication.php b/app/Http/WebApplication.php index b07ac1c..5990b65 100644 --- a/app/Http/WebApplication.php +++ b/app/Http/WebApplication.php @@ -9,11 +9,11 @@ namespace Inhere\Kite\Http; -use PhpPkg\EasyTpl\HtmlTemplate; use Inhere\Kite\Concern\InitApplicationTrait; use Inhere\Kite\Kite; use Inhere\Route\Dispatcher\Dispatcher; use Inhere\Route\Router; +use PhpPkg\EasyTpl\EasyTemplate; use Throwable; use Toolkit\Stdlib\Obj\ObjectBox; use function array_merge; @@ -29,9 +29,9 @@ class WebApplication use InitApplicationTrait; /** - * @var HtmlTemplate + * @var EasyTemplate */ - private HtmlTemplate $renderer; + private EasyTemplate $renderer; /** * @var array @@ -79,10 +79,12 @@ protected function registerServices(ObjectBox $box): void $box->set('webRouter', function () { return new Router(); }); + $box->set('renderer', function () { - $config = $this->getArrayParam('renderer'); - return new HtmlTemplate($config); + $config = $this->config()->getArray('renderer'); + return new EasyTemplate($config); }); + $box->set('dispatcher', [ 'class' => Dispatcher::class, // prop settings @@ -192,9 +194,9 @@ public function getBasePath(): string } /** - * @return HtmlTemplate + * @return EasyTemplate */ - public function getRenderer(): HtmlTemplate + public function getRenderer(): EasyTemplate { return Kite::box()->get('renderer'); } diff --git a/app/Kite.php b/app/Kite.php index 2706c39..32c4a27 100644 --- a/app/Kite.php +++ b/app/Kite.php @@ -22,8 +22,10 @@ use Inhere\Route\Dispatcher\Dispatcher; use Inhere\Route\Router; use Monolog\Logger; +use PhpPkg\Config\ConfigBox; use Toolkit\FsUtil\Dir; use Toolkit\Stdlib\Obj\ObjectBox; +use Toolkit\Stdlib\Util\PhpDotEnv; use const BASE_PATH; use const IN_PHAR; @@ -56,6 +58,11 @@ class Kite */ private static ?ObjectBox $box = null; + /** + * @var ConfigBox|null + */ + private static ?ConfigBox $cfg = null; + /** * @var CliApplication */ @@ -83,6 +90,18 @@ public static function box(): ObjectBox return self::$box; } + /** + * @return ConfigBox + */ + public static function config(): ConfigBox + { + if (!self::$cfg) { + self::$cfg = new ConfigBox(); + } + + return self::$cfg; + } + /** * @param string $method * @param array $args @@ -108,6 +127,14 @@ public static function get(string $id): mixed return self::box()->get($id); } + /** + * @return PhpDotEnv + */ + public static function dotenv(): PhpDotEnv + { + return PhpDotEnv::global(); + } + /** * @return PluginManager */