From bc578787550e816bdb823e2b49e7c960c1385901 Mon Sep 17 00:00:00 2001 From: Inhere Date: Mon, 8 Nov 2021 13:03:02 +0800 Subject: [PATCH] update: update some plugin logic --- app/Common/IdeaHttp/BodyData.php | 24 ---------------- app/Console/Controller/PhpController.php | 2 +- app/Console/Plugin/AbstractPlugin.php | 35 ++++++++++++++++++++++++ app/Kite.php | 8 +++++- 4 files changed, 43 insertions(+), 26 deletions(-) diff --git a/app/Common/IdeaHttp/BodyData.php b/app/Common/IdeaHttp/BodyData.php index 912e333..1503209 100644 --- a/app/Common/IdeaHttp/BodyData.php +++ b/app/Common/IdeaHttp/BodyData.php @@ -52,30 +52,6 @@ public function getValType(mixed $val): string return Type::get($val); } - /** - * @param array $data - */ - public function override(array $data): void - { - $this->exchangeArray($data); - } - - /** - * @param array $data - * @param bool $override - */ - public function load(array $data, bool $override = false): void - { - if ($override) { - $this->override($data); - return; - } - - foreach ($data as $key => $val) { - $this->offsetSet($key, $val); - } - } - /** * @param int $limit * diff --git a/app/Console/Controller/PhpController.php b/app/Console/Controller/PhpController.php index d539163..4b73f30 100644 --- a/app/Console/Controller/PhpController.php +++ b/app/Console/Controller/PhpController.php @@ -287,7 +287,7 @@ public function csFixCommand(FlagsParser $fs, Output $output): void */ public function serveCommand(FlagsParser $fs, Output $output): void { - $conf = $this->app->getArrayParam('php:serve'); + $conf = $this->app->getArrayParam('php_serve'); if ($conf) { $conf = array_merge(self::DEF_SERVE_CONF, $conf); diff --git a/app/Console/Plugin/AbstractPlugin.php b/app/Console/Plugin/AbstractPlugin.php index 1cb3955..10accf5 100644 --- a/app/Console/Plugin/AbstractPlugin.php +++ b/app/Console/Plugin/AbstractPlugin.php @@ -5,6 +5,7 @@ use Inhere\Console\Application; use Inhere\Console\GlobalOption; use Inhere\Console\IO\Output; +use Inhere\Kite\Lib\Template\SimpleTemplate; use Toolkit\PFlag\Flags; use Toolkit\PFlag\FlagsParser; use Toolkit\Stdlib\Helper\DataHelper; @@ -42,6 +43,11 @@ abstract class AbstractPlugin implements PluginInterface */ protected array $metadata = []; + public function __construct() + { + $this->createFlags(); + } + public function init(): void { $this->metadata = array_merge([ @@ -64,6 +70,11 @@ protected function createAndInitFlags(): void $fs->setExample($this->metadata['example']); $fs->addOptsByRules(GlobalOption::getCommonOptions()); + // replace help tpl vars + $fs->setBeforePrintHelp(function (string $help): string { + return $this->applyHelpVars($help); + }); + $loaded = false; if ($optRules = $this->options()) { $loaded = true; @@ -132,6 +143,16 @@ protected function arguments(): array ]; } + /** + * @return array + */ + protected function helpTplVars(): array + { + return [ + 'plugName' => $this->name, + ]; + } + /** * @return array */ @@ -208,6 +229,20 @@ public function run(Application $app, array $args = []): void */ abstract public function exec(Application $app, Output $output): void; + /** + * replace help tpl vars + * + * @param string $help + * + * @return string + */ + protected function applyHelpVars(string $help): string + { + $tpl = SimpleTemplate::new(['format' => '${%s}']); + + return $tpl->renderString($help, $this->helpTplVars()); + } + /** * @return string */ diff --git a/app/Kite.php b/app/Kite.php index c236743..4c74b78 100644 --- a/app/Kite.php +++ b/app/Kite.php @@ -22,7 +22,9 @@ use Monolog\Logger; use Toolkit\FsUtil\Dir; use Toolkit\Stdlib\Obj\ObjectBox; +use Toolkit\Stdlib\OS; use const BASE_PATH; +use const IN_PHAR; /** * Class Kite @@ -132,7 +134,11 @@ public static function basePath(bool $rmPharMark = true): string */ public static function getTmpPath(string $path): string { - // TODO tmp path 不该跟着 kite 执行文件 + if (IN_PHAR) { + // see app/boot.php + return self::resolve('@user-tmp'); + } + return self::getPath("tmp/$path"); }