Skip to content

Commit

Permalink
feat: one command can have more than one view file
Browse files Browse the repository at this point in the history
Add a property $templatePath to specify a template.
  • Loading branch information
kenjis committed Oct 29, 2023
1 parent 1b6818f commit 2534fdc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
8 changes: 5 additions & 3 deletions app/Config/Generators.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ class Generators extends BaseConfig
*
* YOU HAVE BEEN WARNED!
*
* @var array<string, string>
* @var array<string, array<string, string>|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',
Expand Down
32 changes: 26 additions & 6 deletions system/CLI/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand Down Expand Up @@ -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
{
Expand Down
5 changes: 4 additions & 1 deletion system/Commands/Generators/CellGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\GeneratorTrait;
use Config\Generators;

/**
* Generates a skeleton Cell and its view.
Expand Down Expand Up @@ -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';

Expand Down

0 comments on commit 2534fdc

Please sign in to comment.