From 2534fdc3fc2f0e928850456883bdc5741859a603 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 29 Oct 2023 14:37:41 +0900 Subject: [PATCH] feat: one command can have more than one view file Add a property $templatePath to specify a template. --- app/Config/Generators.php | 8 +++-- system/CLI/GeneratorTrait.php | 32 ++++++++++++++++---- system/Commands/Generators/CellGenerator.php | 5 ++- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/app/Config/Generators.php b/app/Config/Generators.php index 6566a31e851e..cc92c7aa432f 100644 --- a/app/Config/Generators.php +++ b/app/Config/Generators.php @@ -23,11 +23,13 @@ class Generators extends BaseConfig * * YOU HAVE BEEN WARNED! * - * @var array + * @var array|string> */ public array $views = [ - 'make:cell' => 'CodeIgniter\Commands\Generators\Views\cell.tpl.php', - 'make:cell_view' => 'CodeIgniter\Commands\Generators\Views\cell_view.tpl.php', + 'make:cell' => [ + 'class' => 'CodeIgniter\Commands\Generators\Views\cell.tpl.php', + 'view' => 'CodeIgniter\Commands\Generators\Views\cell_view.tpl.php', + ], 'make:command' => 'CodeIgniter\Commands\Generators\Views\command.tpl.php', 'make:config' => 'CodeIgniter\Commands\Generators\Views\config.tpl.php', 'make:controller' => 'CodeIgniter\Commands\Generators\Views\controller.tpl.php', diff --git a/system/CLI/GeneratorTrait.php b/system/CLI/GeneratorTrait.php index 12f1168e1354..c1112ca63481 100644 --- a/system/CLI/GeneratorTrait.php +++ b/system/CLI/GeneratorTrait.php @@ -36,7 +36,15 @@ trait GeneratorTrait protected $directory; /** - * View template name + * (Optional) View template path + * + * We use special namespaced paths like: + * `CodeIgniter\Commands\Generators\Views\cell.tpl.php`. + */ + protected ?string $templatePath = null; + + /** + * View template name for fallback * * @var string */ @@ -118,6 +126,8 @@ protected function generateClass(array $params) /** * Generate a view file from an existing template. + * + * @param string $view namespaced view name that is generated */ protected function generateView(string $view, array $params) { @@ -135,6 +145,8 @@ protected function generateView(string $view, array $params) /** * Handles writing the file to disk, and all of the safety checks around that. + * + * @param string $target file path */ private function generateFile(string $target, string $content): void { @@ -219,6 +231,10 @@ private function generateFile(string $target, string $content): void /** * Prepare options and do the necessary replacements. + * + * @param string $class namespaced classname or namespaced view. + * + * @return string generated file content */ protected function prepare(string $class): string { @@ -307,11 +323,9 @@ protected function qualifyClassName(): string protected function renderTemplate(array $data = []): string { try { - return view( - config(Generators::class)->views[$this->name], - $data, - ['debug' => false] - ); + $template = $this->templatePath ?? config(Generators::class)->views[$this->name]; + + return view($template, $data, ['debug' => false]); } catch (Throwable $e) { log_message('error', (string) $e); @@ -325,6 +339,10 @@ protected function renderTemplate(array $data = []): string /** * Performs pseudo-variables contained within view file. + * + * @param string $class namespaced classname or namespaced view. + * + * @return string generated file content */ protected function parseTemplate( string $class, @@ -378,6 +396,8 @@ protected function buildContent(string $class): string /** * Builds the file path from the class name. + * + * @param string $class namespaced classname or namespaced view. */ protected function buildPath(string $class): string { diff --git a/system/Commands/Generators/CellGenerator.php b/system/Commands/Generators/CellGenerator.php index 737b713b156c..78f012ec10c7 100644 --- a/system/Commands/Generators/CellGenerator.php +++ b/system/Commands/Generators/CellGenerator.php @@ -13,6 +13,7 @@ use CodeIgniter\CLI\BaseCommand; use CodeIgniter\CLI\GeneratorTrait; +use Config\Generators; /** * Generates a skeleton Cell and its view. @@ -78,11 +79,13 @@ public function run(array $params) $params = array_merge($params, ['suffix' => null]); + $this->templatePath = config(Generators::class)->views[$this->name]['class']; $this->template = 'cell.tpl.php'; $this->classNameLang = 'CLI.generator.className.cell'; + $this->generateClass($params); - $this->name = 'make:cell_view'; + $this->templatePath = config(Generators::class)->views[$this->name]['view']; $this->template = 'cell_view.tpl.php'; $this->classNameLang = 'CLI.generator.viewName.cell';