Skip to content

Commit 3d78c33

Browse files
authored
Merge pull request #4197 from paulbalandan/generator-finishing-touches
Finishing touches to generator refactor
2 parents 52ebb0d + 423ef5e commit 3d78c33

File tree

14 files changed

+127
-204
lines changed

14 files changed

+127
-204
lines changed

app/Config/Generators.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Generators extends BaseConfig
3333
'make:migration' => 'CodeIgniter\Commands\Generators\Views\migration.tpl.php',
3434
'make:model' => 'CodeIgniter\Commands\Generators\Views\model.tpl.php',
3535
'make:seeder' => 'CodeIgniter\Commands\Generators\Views\seeder.tpl.php',
36+
'make:validation' => 'CodeIgniter\Commands\Generators\Views\validation.tpl.php',
3637
'session:migration' => 'CodeIgniter\Commands\Generators\Views\migration.tpl.php',
3738
];
3839
}

system/CLI/GeneratorTrait.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function execute(array $params): void
116116
// we are duplicating things, If 'force' option is not supplied, we bail.
117117
if (! $this->getOption('force') && $isFile)
118118
{
119-
CLI::write(lang('CLI.generator.fileExist', [clean_path($path)]), 'red');
119+
CLI::error(lang('CLI.generator.fileExist', [clean_path($path)]), 'light_gray', 'red');
120120
CLI::newLine();
121121

122122
return;
@@ -131,7 +131,7 @@ protected function execute(array $params): void
131131
}
132132

133133
helper('filesystem');
134-
134+
135135
// Build the class based on the details we have, We'll be getting our file
136136
// contents from the template, and then we'll do the necessary replacements.
137137
if (! write_file($path, $this->buildContent($class)))
@@ -170,7 +170,7 @@ protected function prepare(string $class): string
170170

171171
/**
172172
* Change file basename before saving.
173-
*
173+
*
174174
* Useful for components where the file name has a date.
175175
*
176176
* @param string $filename
@@ -219,7 +219,7 @@ protected function qualifyClassName(): string
219219

220220
if (strncmp($class, $namespace, strlen($namespace)) === 0)
221221
{
222-
return $class;
222+
return $class; // @codeCoverageIgnore
223223
}
224224

225225
return $namespace . '\\' . $this->directory . '\\' . str_replace('/', '\\', $class);
@@ -326,20 +326,28 @@ protected function buildPath(string $class): string
326326
* Allows child generators to modify the internal `$hasClassName` flag.
327327
*
328328
* @param boolean $hasClassName
329+
*
330+
* @return $this
329331
*/
330332
protected function setHasClassName(bool $hasClassName)
331333
{
332334
$this->hasClassName = $hasClassName;
335+
336+
return $this;
333337
}
334338

335339
/**
336340
* Allows child generators to modify the internal `$sortImports` flag.
337341
*
338342
* @param boolean $sortImports
343+
*
344+
* @return $this
339345
*/
340346
protected function setSortImports(bool $sortImports)
341347
{
342348
$this->sortImports = $sortImports;
349+
350+
return $this;
343351
}
344352

345353
/**

system/Commands/Generators/ConfigGenerator.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
namespace CodeIgniter\Commands\Generators;
1313

1414
use CodeIgniter\CLI\BaseCommand;
15-
use CodeIgniter\CLI\CLI;
1615
use CodeIgniter\CLI\GeneratorTrait;
1716

1817
/**
19-
* Generates a skeleton Config file.
18+
* Generates a skeleton config file.
2019
*/
2120
class ConfigGenerator extends BaseCommand
2221
{
@@ -93,9 +92,13 @@ public function run(array $params)
9392
*/
9493
protected function prepare(string $class): string
9594
{
96-
$namespace = $this->getOption('namespace');
97-
$namespace = is_string($namespace) ? $namespace . '\\' . $this->directory : $this->directory;
95+
$namespace = $this->getOption('namespace') ?? APP_NAMESPACE;
9896

99-
return $this->parseTemplate($class, ['{namespace}'], [$namespace]);
97+
if ($namespace === APP_NAMESPACE)
98+
{
99+
$class = substr($class, strlen($namespace . '\\'));
100+
}
101+
102+
return $this->parseTemplate($class);
100103
}
101104
}

system/Commands/Generators/ControllerGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ControllerGenerator extends BaseCommand
7272
'--force' => 'Force overwrite existing file.',
7373
];
7474

75-
/**
75+
/**
7676
* Actually execute a command.
7777
*
7878
* @param array $params
@@ -100,7 +100,7 @@ protected function prepare(string $class): string
100100

101101
$useStatement = trim(APP_NAMESPACE, '\\') . '\Controllers\BaseController';
102102
$extends = 'BaseController';
103-
103+
104104
// Gets the appropriate parent class to extend.
105105
if ($bare || $rest)
106106
{
@@ -112,7 +112,7 @@ protected function prepare(string $class): string
112112
elseif ($rest)
113113
{
114114
$rest = is_string($rest) ? $rest : 'controller';
115-
115+
116116
if (! in_array($rest, ['controller', 'presenter'], true))
117117
{
118118
// @codeCoverageIgnoreStart

system/Commands/Generators/MigrateCreate.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
use CodeIgniter\CLI\CLI;
1616

1717
/**
18-
* Deprecated class for the migration
19-
* creation command.
18+
* Deprecated class for the migration creation command.
2019
*
2120
* @deprecated Use make:command instead.
2221
*
@@ -80,8 +79,9 @@ class MigrateCreate extends BaseCommand
8079
public function run(array $params)
8180
{
8281
// Resolve arguments before passing to make:migration
83-
$params[0] = $params[0] ?? CLI::getSegment(2);
84-
$params['n'] = $params['n'] ?? CLI::getOption('n') ?? APP_NAMESPACE;
82+
$params[0] = $params[0] ?? CLI::getSegment(2);
83+
84+
$params['namespace'] = $params['namespace'] ?? CLI::getOption('namespace') ?? APP_NAMESPACE;
8585

8686
if (array_key_exists('force', $params) || CLI::getOption('force'))
8787
{

system/Commands/Generators/ComponentGenerator.php renamed to system/Commands/Generators/ScaffoldGenerator.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Generates a complete set of scaffold files.
2020
*/
21-
class ComponentGenerator extends BaseCommand
21+
class ScaffoldGenerator extends BaseCommand
2222
{
2323
use GeneratorTrait;
2424

@@ -34,7 +34,7 @@ class ComponentGenerator extends BaseCommand
3434
*
3535
* @var string
3636
*/
37-
protected $name = 'make:component';
37+
protected $name = 'make:scaffold';
3838

3939
/**
4040
* The Command's Description
@@ -48,7 +48,7 @@ class ComponentGenerator extends BaseCommand
4848
*
4949
* @var string
5050
*/
51-
protected $usage = 'make:component <name> [options]';
51+
protected $usage = 'make:scaffold <name> [options]';
5252

5353
/**
5454
* The Command's Arguments
@@ -65,13 +65,13 @@ class ComponentGenerator extends BaseCommand
6565
* @var array
6666
*/
6767
protected $options = [
68-
'--bare' => 'Add the \'--bare\' option to controller component.',
69-
'--restful' => 'Add the \'--restful\' option to controller component.',
70-
'--table' => 'Add the \'--table\' option to the model component.',
71-
'--dbgroup' => 'Add the \'--dbgroup\' option to model component.',
72-
'--return' => 'Add the \'--return\' option to the model component.',
68+
'--bare' => 'Add the "--bare" option to controller component.',
69+
'--restful' => 'Add the "--restful" option to controller component.',
70+
'--table' => 'Add the "--table" option to the model component.',
71+
'--dbgroup' => 'Add the "--dbgroup" option to model component.',
72+
'--return' => 'Add the "--return" option to the model component.',
7373
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
74-
'--suffix' => 'Append the component title to the class name (e.g. User => UserComponent).',
74+
'--suffix' => 'Append the component title to the class name.',
7575
'--force' => 'Force overwrite existing file.',
7676
];
7777

@@ -83,7 +83,7 @@ class ComponentGenerator extends BaseCommand
8383
public function run(array $params)
8484
{
8585
$this->params = $params;
86-
86+
8787
$options = [];
8888

8989
if ($this->getOption('namespace'))

system/Commands/Generators/SeederGenerator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace CodeIgniter\Commands\Generators;
1313

1414
use CodeIgniter\CLI\BaseCommand;
15-
use CodeIgniter\CLI\CLI;
1615
use CodeIgniter\CLI\GeneratorTrait;
1716

1817
/**

system/Commands/Generators/SessionMigrationGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
/**
1919
* Generates a migration file for database sessions.
2020
*
21-
* @deprecated Use make:migration instead.
22-
*
21+
* @deprecated Use make:migration --session instead.
22+
*
2323
* @codeCoverageIgnore
2424
*/
2525
class SessionMigrationGenerator extends BaseCommand
@@ -76,12 +76,12 @@ public function run(array $params)
7676
$this->template = 'migration.tpl.php';
7777

7878
$table = 'ci_sessions';
79-
79+
8080
if (array_key_exists('t', $params) || CLI::getOption('t'))
8181
{
8282
$table = $params['t'] ?? CLI::getOption('t');
8383
}
84-
84+
8585
$params[0] = "_create_{$table}_table";
8686

8787
$this->execute($params);

system/Commands/Generators/Views/controller.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function index()
1616
{
1717
//
1818
}
19-
19+
2020
/**
2121
* Return the properties of a resource object
2222
*

system/Commands/Generators/Views/migration.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class {class} extends Migration
88
{
99
<?php if ($session): ?>
1010
protected $DBGroup = '<?= $DBGroup ?>';
11-
11+
1212
public function up()
1313
{
1414
$this->forge->addField([

0 commit comments

Comments
 (0)