Skip to content

Commit

Permalink
♻️ refactor: refactoring config manage logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 12, 2022
1 parent 72c3646 commit 348fdef
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 60 deletions.
21 changes: 16 additions & 5 deletions app/Concern/InitApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));
}

/**
Expand All @@ -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']);
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion app/Console/Attach/Golang/GenerateStructCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 9 additions & 9 deletions app/Console/CliApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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);
// });
}
Expand Down
5 changes: 3 additions & 2 deletions app/Console/Command/DocCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'];
}
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions app/Console/Command/ToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -30,6 +31,7 @@ protected function subCommands(): array
{
return [
OpenCmd::class,
LnCommand::class,
HashHmacCommand::class,
InstallCommand::class,
UpdateCommand::class,
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Controller/Gitx/GitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Controller/Gitx/GitFlowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Controller/Gitx/GitHubController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/Console/Controller/Gitx/GitLabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 2 additions & 4 deletions app/Console/Controller/PhpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down
25 changes: 11 additions & 14 deletions app/Console/Controller/SelfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
Expand Down Expand Up @@ -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']);
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Listener/NotFoundListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
2 changes: 1 addition & 1 deletion app/Console/SubCmd/Gitflow/BranchCreateCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
10 changes: 0 additions & 10 deletions app/Console/simple-cmds.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 348fdef

Please sign in to comment.