mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
feat: one command can have more than one view file
Add a property $templatePath to specify a template.
This commit is contained in:
parent
1b6818f076
commit
2534fdc3fc
@ -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',
|
||||
|
@ -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 @@ trait GeneratorTrait
|
||||
|
||||
/**
|
||||
* 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 @@ trait GeneratorTrait
|
||||
|
||||
/**
|
||||
* 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 @@ trait GeneratorTrait
|
||||
|
||||
/**
|
||||
* 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 @@ trait GeneratorTrait
|
||||
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 @@ trait GeneratorTrait
|
||||
|
||||
/**
|
||||
* 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 @@ trait GeneratorTrait
|
||||
|
||||
/**
|
||||
* Builds the file path from the class name.
|
||||
*
|
||||
* @param string $class namespaced classname or namespaced view.
|
||||
*/
|
||||
protected function buildPath(string $class): string
|
||||
{
|
||||
|
@ -13,6 +13,7 @@ namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
use Config\Generators;
|
||||
|
||||
/**
|
||||
* Generates a skeleton Cell and its view.
|
||||
@ -78,11 +79,13 @@ class CellGenerator extends BaseCommand
|
||||
|
||||
$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';
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user