From f04cf0754f1bc3cbdf38e74fd7429b09a2a3e12d Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" <51850998+paulbalandan@users.noreply.github.com> Date: Sun, 31 Jan 2021 23:18:35 +0800 Subject: [PATCH 1/4] Finishing touches to generator refactor --- app/Config/Generators.php | 1 + system/CLI/GeneratorTrait.php | 16 +++++++++++---- .../Generators/ControllerGenerator.php | 6 +++--- system/Commands/Generators/MigrateCreate.php | 8 ++++---- ...entGenerator.php => ScaffoldGenerator.php} | 20 +++++++++---------- .../Commands/Generators/SeederGenerator.php | 1 - .../Generators/SessionMigrationGenerator.php | 8 ++++---- .../Generators/Views/controller.tpl.php | 2 +- .../Generators/Views/migration.tpl.php | 2 +- .../Generators/Views/validation.tpl.php | 8 ++++---- tests/system/Commands/ModelGeneratorTest.php | 12 +++++++++-- ...atorTest.php => ScaffoldGeneratorTest.php} | 4 ++-- 12 files changed, 52 insertions(+), 36 deletions(-) rename system/Commands/Generators/{ComponentGenerator.php => ScaffoldGenerator.php} (81%) rename tests/system/Commands/{ComponentGeneratorTest.php => ScaffoldGeneratorTest.php} (98%) diff --git a/app/Config/Generators.php b/app/Config/Generators.php index 301e889b377a..2c32587a1788 100644 --- a/app/Config/Generators.php +++ b/app/Config/Generators.php @@ -33,6 +33,7 @@ class Generators extends BaseConfig 'make:migration' => 'CodeIgniter\Commands\Generators\Views\migration.tpl.php', 'make:model' => 'CodeIgniter\Commands\Generators\Views\model.tpl.php', 'make:seeder' => 'CodeIgniter\Commands\Generators\Views\seeder.tpl.php', + 'make:validation' => 'CodeIgniter\Commands\Generators\Views\validation.tpl.php', 'session:migration' => 'CodeIgniter\Commands\Generators\Views\migration.tpl.php', ]; } diff --git a/system/CLI/GeneratorTrait.php b/system/CLI/GeneratorTrait.php index 9c7441d6babf..df484d885367 100644 --- a/system/CLI/GeneratorTrait.php +++ b/system/CLI/GeneratorTrait.php @@ -116,7 +116,7 @@ protected function execute(array $params): void // we are duplicating things, If 'force' option is not supplied, we bail. if (! $this->getOption('force') && $isFile) { - CLI::write(lang('CLI.generator.fileExist', [clean_path($path)]), 'red'); + CLI::error(lang('CLI.generator.fileExist', [clean_path($path)]), 'light_gray', 'red'); CLI::newLine(); return; @@ -131,7 +131,7 @@ protected function execute(array $params): void } helper('filesystem'); - + // Build the class based on the details we have, We'll be getting our file // contents from the template, and then we'll do the necessary replacements. if (! write_file($path, $this->buildContent($class))) @@ -170,7 +170,7 @@ protected function prepare(string $class): string /** * Change file basename before saving. - * + * * Useful for components where the file name has a date. * * @param string $filename @@ -219,7 +219,7 @@ protected function qualifyClassName(): string if (strncmp($class, $namespace, strlen($namespace)) === 0) { - return $class; + return $class; // @codeCoverageIgnore } return $namespace . '\\' . $this->directory . '\\' . str_replace('/', '\\', $class); @@ -326,20 +326,28 @@ protected function buildPath(string $class): string * Allows child generators to modify the internal `$hasClassName` flag. * * @param boolean $hasClassName + * + * @return $this */ protected function setHasClassName(bool $hasClassName) { $this->hasClassName = $hasClassName; + + return $this; } /** * Allows child generators to modify the internal `$sortImports` flag. * * @param boolean $sortImports + * + * @return $this */ protected function setSortImports(bool $sortImports) { $this->sortImports = $sortImports; + + return $this; } /** diff --git a/system/Commands/Generators/ControllerGenerator.php b/system/Commands/Generators/ControllerGenerator.php index f5f57ae9df6e..ff56853cc756 100644 --- a/system/Commands/Generators/ControllerGenerator.php +++ b/system/Commands/Generators/ControllerGenerator.php @@ -72,7 +72,7 @@ class ControllerGenerator extends BaseCommand '--force' => 'Force overwrite existing file.', ]; - /** + /** * Actually execute a command. * * @param array $params @@ -100,7 +100,7 @@ protected function prepare(string $class): string $useStatement = trim(APP_NAMESPACE, '\\') . '\Controllers\BaseController'; $extends = 'BaseController'; - + // Gets the appropriate parent class to extend. if ($bare || $rest) { @@ -112,7 +112,7 @@ protected function prepare(string $class): string elseif ($rest) { $rest = is_string($rest) ? $rest : 'controller'; - + if (! in_array($rest, ['controller', 'presenter'], true)) { // @codeCoverageIgnoreStart diff --git a/system/Commands/Generators/MigrateCreate.php b/system/Commands/Generators/MigrateCreate.php index ac20d1dcc69c..546e3437c78d 100644 --- a/system/Commands/Generators/MigrateCreate.php +++ b/system/Commands/Generators/MigrateCreate.php @@ -15,8 +15,7 @@ use CodeIgniter\CLI\CLI; /** - * Deprecated class for the migration - * creation command. + * Deprecated class for the migration creation command. * * @deprecated Use make:command instead. * @@ -80,8 +79,9 @@ class MigrateCreate extends BaseCommand public function run(array $params) { // Resolve arguments before passing to make:migration - $params[0] = $params[0] ?? CLI::getSegment(2); - $params['n'] = $params['n'] ?? CLI::getOption('n') ?? APP_NAMESPACE; + $params[0] = $params[0] ?? CLI::getSegment(2); + + $params['namespace'] = $params['namespace'] ?? CLI::getOption('namespace') ?? APP_NAMESPACE; if (array_key_exists('force', $params) || CLI::getOption('force')) { diff --git a/system/Commands/Generators/ComponentGenerator.php b/system/Commands/Generators/ScaffoldGenerator.php similarity index 81% rename from system/Commands/Generators/ComponentGenerator.php rename to system/Commands/Generators/ScaffoldGenerator.php index 2b4e13bc53c9..6750b46f7cf4 100644 --- a/system/Commands/Generators/ComponentGenerator.php +++ b/system/Commands/Generators/ScaffoldGenerator.php @@ -18,7 +18,7 @@ /** * Generates a complete set of scaffold files. */ -class ComponentGenerator extends BaseCommand +class ScaffoldGenerator extends BaseCommand { use GeneratorTrait; @@ -34,7 +34,7 @@ class ComponentGenerator extends BaseCommand * * @var string */ - protected $name = 'make:component'; + protected $name = 'make:scaffold'; /** * The Command's Description @@ -48,7 +48,7 @@ class ComponentGenerator extends BaseCommand * * @var string */ - protected $usage = 'make:component [options]'; + protected $usage = 'make:scaffold [options]'; /** * The Command's Arguments @@ -65,13 +65,13 @@ class ComponentGenerator extends BaseCommand * @var array */ protected $options = [ - '--bare' => 'Add the \'--bare\' option to controller component.', - '--restful' => 'Add the \'--restful\' option to controller component.', - '--table' => 'Add the \'--table\' option to the model component.', - '--dbgroup' => 'Add the \'--dbgroup\' option to model component.', - '--return' => 'Add the \'--return\' option to the model component.', + '--bare' => 'Add the "--bare" option to controller component.', + '--restful' => 'Add the "--restful" option to controller component.', + '--table' => 'Add the "--table" option to the model component.', + '--dbgroup' => 'Add the "--dbgroup" option to model component.', + '--return' => 'Add the "--return" option to the model component.', '--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".', - '--suffix' => 'Append the component title to the class name (e.g. User => UserComponent).', + '--suffix' => 'Append the component title to the class name.', '--force' => 'Force overwrite existing file.', ]; @@ -83,7 +83,7 @@ class ComponentGenerator extends BaseCommand public function run(array $params) { $this->params = $params; - + $options = []; if ($this->getOption('namespace')) diff --git a/system/Commands/Generators/SeederGenerator.php b/system/Commands/Generators/SeederGenerator.php index 0b67a0034c73..d9c34ef00f93 100644 --- a/system/Commands/Generators/SeederGenerator.php +++ b/system/Commands/Generators/SeederGenerator.php @@ -12,7 +12,6 @@ namespace CodeIgniter\Commands\Generators; use CodeIgniter\CLI\BaseCommand; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\GeneratorTrait; /** diff --git a/system/Commands/Generators/SessionMigrationGenerator.php b/system/Commands/Generators/SessionMigrationGenerator.php index 2e111f4c7f6b..10b4a72bf15a 100644 --- a/system/Commands/Generators/SessionMigrationGenerator.php +++ b/system/Commands/Generators/SessionMigrationGenerator.php @@ -18,8 +18,8 @@ /** * Generates a migration file for database sessions. * - * @deprecated Use make:migration instead. - * + * @deprecated Use make:migration --session instead. + * * @codeCoverageIgnore */ class SessionMigrationGenerator extends BaseCommand @@ -76,12 +76,12 @@ public function run(array $params) $this->template = 'migration.tpl.php'; $table = 'ci_sessions'; - + if (array_key_exists('t', $params) || CLI::getOption('t')) { $table = $params['t'] ?? CLI::getOption('t'); } - + $params[0] = "_create_{$table}_table"; $this->execute($params); diff --git a/system/Commands/Generators/Views/controller.tpl.php b/system/Commands/Generators/Views/controller.tpl.php index d389d3d5b34d..f7859f2ed6ff 100644 --- a/system/Commands/Generators/Views/controller.tpl.php +++ b/system/Commands/Generators/Views/controller.tpl.php @@ -16,7 +16,7 @@ public function index() { // } - + /** * Return the properties of a resource object * diff --git a/system/Commands/Generators/Views/migration.tpl.php b/system/Commands/Generators/Views/migration.tpl.php index 0fecad5888f2..42bb77effe34 100644 --- a/system/Commands/Generators/Views/migration.tpl.php +++ b/system/Commands/Generators/Views/migration.tpl.php @@ -8,7 +8,7 @@ class {class} extends Migration { protected $DBGroup = ''; - + public function up() { $this->forge->addField([ diff --git a/system/Commands/Generators/Views/validation.tpl.php b/system/Commands/Generators/Views/validation.tpl.php index 56bbb9c877fb..60d78a3d63fe 100644 --- a/system/Commands/Generators/Views/validation.tpl.php +++ b/system/Commands/Generators/Views/validation.tpl.php @@ -4,8 +4,8 @@ class {class} { - public function custom_rule(): bool - { - return true; - } + // public function custom_rule(): bool + // { + // return true; + // } } diff --git a/tests/system/Commands/ModelGeneratorTest.php b/tests/system/Commands/ModelGeneratorTest.php index 85d089060f37..3cc908aa0a11 100644 --- a/tests/system/Commands/ModelGeneratorTest.php +++ b/tests/system/Commands/ModelGeneratorTest.php @@ -101,7 +101,15 @@ public function testGenerateModelWithOptionReturnEntity() public function testGenerateModelWithOptionSuffix() { - command('make:model user -suffix'); - $this->assertFileExists(APPPATH . 'Models/UserModel.php'); + command('make:model user -suffix -return entity'); + + $model = APPPATH . 'Models/UserModel.php'; + $entity = APPPATH . 'Entities/UserEntity.php'; + + $this->assertFileExists($model); + $this->assertFileExists($entity); + unlink($model); + unlink($entity); + rmdir(dirname($entity)); } } diff --git a/tests/system/Commands/ComponentGeneratorTest.php b/tests/system/Commands/ScaffoldGeneratorTest.php similarity index 98% rename from tests/system/Commands/ComponentGeneratorTest.php rename to tests/system/Commands/ScaffoldGeneratorTest.php index 30b851fce239..d4fcce9fc6b1 100644 --- a/tests/system/Commands/ComponentGeneratorTest.php +++ b/tests/system/Commands/ScaffoldGeneratorTest.php @@ -5,7 +5,7 @@ use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Filters\CITestStreamFilter; -class ComponentGeneratorTest extends CIUnitTestCase +class ScaffoldGeneratorTest extends CIUnitTestCase { protected $streamFilter; @@ -19,7 +19,7 @@ protected function setUp(): void protected function tearDown(): void { - stream_filter_remove($this->streamFilter); + stream_filter_remove($this->streamFilter); } protected function getFileContents(string $filepath): string From 23785c6dece8b4205d136ace5241de892e0d716a Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sun, 31 Jan 2021 23:33:34 +0800 Subject: [PATCH 2/4] Fix wrong config namespace in subfolder --- system/Commands/Generators/ConfigGenerator.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/system/Commands/Generators/ConfigGenerator.php b/system/Commands/Generators/ConfigGenerator.php index efd9d7e9d6be..af6f3439dda6 100644 --- a/system/Commands/Generators/ConfigGenerator.php +++ b/system/Commands/Generators/ConfigGenerator.php @@ -12,11 +12,10 @@ namespace CodeIgniter\Commands\Generators; use CodeIgniter\CLI\BaseCommand; -use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\GeneratorTrait; /** - * Generates a skeleton Config file. + * Generates a skeleton config file. */ class ConfigGenerator extends BaseCommand { @@ -93,9 +92,13 @@ public function run(array $params) */ protected function prepare(string $class): string { - $namespace = $this->getOption('namespace'); - $namespace = is_string($namespace) ? $namespace . '\\' . $this->directory : $this->directory; + $namespace = $this->getOption('namespace') ?? APP_NAMESPACE; - return $this->parseTemplate($class, ['{namespace}'], [$namespace]); + if ($namespace === APP_NAMESPACE) + { + $class = substr($class, strlen($namespace . '\\')); + } + + return $this->parseTemplate($class); } } From f1819486d63872dd043b3dc212eb456b04a9ba7c Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sun, 31 Jan 2021 23:41:00 +0800 Subject: [PATCH 3/4] Fix wrong command name --- tests/system/Commands/ScaffoldGeneratorTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/system/Commands/ScaffoldGeneratorTest.php b/tests/system/Commands/ScaffoldGeneratorTest.php index d4fcce9fc6b1..79e77fd3c509 100644 --- a/tests/system/Commands/ScaffoldGeneratorTest.php +++ b/tests/system/Commands/ScaffoldGeneratorTest.php @@ -34,7 +34,7 @@ protected function getFileContents(string $filepath): string public function testCreateComponentProducesManyFiles() { - command('make:component people'); + command('make:scaffold people'); $dir = '\\' . DIRECTORY_SEPARATOR; $migration = "APPPATH{$dir}Database{$dir}Migrations{$dir}(.*)\.php"; @@ -57,7 +57,7 @@ public function testCreateComponentProducesManyFiles() public function testCreateComponentWithManyOptions() { - command('make:component user -restful -return entity'); + command('make:scaffold user -restful -return entity'); $dir = '\\' . DIRECTORY_SEPARATOR; $migration = "APPPATH{$dir}Database{$dir}Migrations{$dir}(.*)\.php"; @@ -86,7 +86,7 @@ public function testCreateComponentWithManyOptions() public function testCreateComponentWithOptionSuffix() { - command('make:component order -suffix'); + command('make:scaffold order -suffix'); $dir = '\\' . DIRECTORY_SEPARATOR; $migration = "APPPATH{$dir}Database{$dir}Migrations{$dir}(.*)\.php"; @@ -115,7 +115,7 @@ public function testCreateComponentWithOptionForce() $this->assertFileExists(APPPATH . 'Controllers/Fixer.php'); CITestStreamFilter::$buffer = ''; - command('make:component fixer -bare -force'); + command('make:scaffold fixer -bare -force'); $dir = '\\' . DIRECTORY_SEPARATOR; $migration = "APPPATH{$dir}Database{$dir}Migrations{$dir}(.*)\.php"; @@ -142,7 +142,7 @@ public function testCreateComponentWithOptionForce() public function testCreateComponentWithOptionNamespace() { - command('make:component product -namespace App'); + command('make:scaffold product -namespace App'); $dir = '\\' . DIRECTORY_SEPARATOR; $migration = "APPPATH{$dir}Database{$dir}Migrations{$dir}(.*)\.php"; From 423ef5ee18a8ec124995c34ad044406276b4d34e Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Mon, 1 Feb 2021 00:05:48 +0800 Subject: [PATCH 4/4] Update CLI generator documentation --- user_guide_src/source/cli/cli_generators.rst | 220 ++++++------------- 1 file changed, 62 insertions(+), 158 deletions(-) diff --git a/user_guide_src/source/cli/cli_generators.rst b/user_guide_src/source/cli/cli_generators.rst index a03496fcef22..f7d0ea380fb3 100644 --- a/user_guide_src/source/cli/cli_generators.rst +++ b/user_guide_src/source/cli/cli_generators.rst @@ -33,7 +33,7 @@ Creates a new spark command. Usage: ====== -.. code-block:: none +:: make:command [options] @@ -46,7 +46,29 @@ Options: * ``--command``: The command name to run in spark. Defaults to ``command:name``. * ``--group``: The group/namespace of the command. Defaults to ``CodeIgniter`` for basic commands, and ``Generators`` for generator commands. * ``--type``: The type of command, whether a ``basic`` command or a ``generator`` command. Defaults to ``basic``. -* ``-n``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--namespace``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--suffix``: Append the component suffix to the generated class name. +* ``--force``: Set this flag to overwrite existing files on destination. + +make:config +----------- + +Creates a new config file. + +Usage: +====== +:: + + make:config [options] + +Argument: +========= +* ``name``: The name of the config class. **[REQUIRED]** + +Options: +======== +* ``--namespace``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--suffix``: Append the component suffix to the generated class name. * ``--force``: Set this flag to overwrite existing files on destination. make:controller @@ -56,7 +78,7 @@ Creates a new controller file. Usage: ====== -.. code-block:: none +:: make:controller [options] @@ -68,7 +90,8 @@ Options: ======== * ``--bare``: Extends from ``CodeIgniter\Controller`` instead of ``BaseController``. * ``--restful``: Extends from a RESTful resource. Choices are ``controller`` and ``presenter``. Defaults to ``controller``. -* ``-n``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--namespace``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--suffix``: Append the component suffix to the generated class name. * ``--force``: Set this flag to overwrite existing files on destination. make:entity @@ -78,7 +101,7 @@ Creates a new entity file. Usage: ====== -.. code-block:: none +:: make:entity [options] @@ -88,8 +111,9 @@ Argument: Options: ======== -* ``-n``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. -* ``-force``: Set this flag to overwrite existing files on destination. +* ``--namespace``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--suffix``: Append the component suffix to the generated class name. +* ``--force``: Set this flag to overwrite existing files on destination. make:filter ----------- @@ -98,7 +122,7 @@ Creates a new filter file. Usage: ====== -.. code-block:: none +:: make:filter [options] @@ -108,7 +132,8 @@ Argument: Options: ======== -* ``-n``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--namespace``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--suffix``: Append the component suffix to the generated class name. * ``--force``: Set this flag to overwrite existing files on destination. make:model @@ -118,7 +143,7 @@ Creates a new model file. Usage: ====== -.. code-block:: none +:: make:model [options] @@ -129,9 +154,10 @@ Argument: Options: ======== * ``--dbgroup``: Database group to use. Defaults to ``default``. -* ``--entity``: Set this flag to use an entity class as the return type. +* ``--return``: Set the return type from ``array``, ``object``, or ``entity``. Defaults to ``array``. * ``--table``: Supply a different table name. Defaults to the pluralized class name. -* ``-n``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--namespace``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--suffix``: Append the component suffix to the generated class name. * ``--force``: Set this flag to overwrite existing files on destination. make:seeder @@ -141,7 +167,7 @@ Creates a new seeder file. Usage: ====== -.. code-block:: none +:: make:seeder [options] @@ -151,7 +177,8 @@ Argument: Options: ======== -* ``-n``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--namespace``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--suffix``: Append the component suffix to the generated class name. * ``--force``: Set this flag to overwrite existing files on destination. make:migration @@ -161,7 +188,7 @@ Creates a new migration file. Usage: ====== -.. code-block:: none +:: make:migration [options] @@ -171,30 +198,13 @@ Argument: Options: ======== -* ``-n``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--session``: Generate a migration file for database sessions. +* ``--table``: Set the table name to use for database sessions. Defaults to ``ci_sessions``. +* ``--dbgroup``: Set the database group for database sessions. Defaults to ``default`` group. +* ``--namespace``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. +* ``--suffix``: Append the component suffix to the generated class name. * ``--force``: Set this flag to overwrite existing files on destination. -session:migration ------------------ - -Generates the migration file for database sessions. - -Usage: -====== -.. code-block:: none - - session:migration [options] - -Options: -======== -* ``-g``: Set the database group. -* ``-t``: Set the table name. Defaults to ``ci_sessions``. -* ``-n``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``. -* ``--force``: Set this flag to overwrite existing files on destination. - -.. note:: When running ``php spark help session:migration``, you will see that it has the argument ``name`` listed. - This argument is not used as the class name is derived from the table name passed to the ``-t`` option. - .. note:: Do you need to have the generated code in a subfolder? Let's say if you want to create a controller class to reside in the ``Admin`` subfolder of the main ``Controllers`` folder, you will just need to prepend the subfolder to the class name, like this: ``php spark make:controller admin/login``. This @@ -203,14 +213,15 @@ Options: .. note:: Working on modules? Code generation will set the root namespace to a default of ``APP_NAMESPACE``. Should you need to have the generated code elsewhere in your module namespace, make sure to set - the ``-n`` option in your command, e.g., ``php spark make:model blog -n Acme\Blog``. + the ``--namespace`` option in your command, e.g., ``php spark make:model blog --namespace Acme\Blog``. -.. warning:: Make sure when setting the ``-n`` option that the supplied namespace is a valid namespace - defined in your ``$psr4`` array in ``Config\Autoload`` or defined in your composer autoload file. - Otherwise, a ``RuntimeException`` will be thrown. +.. warning:: Make sure when setting the ``--namespace`` option that the supplied namespace is a valid + namespace defined in your ``$psr4`` array in ``Config\Autoload`` or defined in your composer autoload + file. Otherwise, code generation will be interrupted. .. warning:: Use of ``migrate:create`` to create migration files is now deprecated. It will be removed in - future releases. Please use ``make:migration`` as replacement. + future releases. Please use ``make:migration`` as replacement. Also, please use ``make:migration --session`` + to use instead of the deprecated ``session:migration``. **************************************** Scaffolding a Complete Set of Stock Code @@ -234,122 +245,15 @@ will create the following classes: (1) ``App\Controllers\User``; (2) ``App\Models\User``; -(3) ``App\Entities\User``; -(4) ``App\Database\Migrations\_User``; and -(5) ``App\Database\Seeds\User``. - -**************** -GeneratorCommand -**************** - -All generator commands must extend ``GeneratorCommand`` to fully utilize its methods that are used in code -generation. While some of the methods are already functional, you may have the need to customize or upgrade -what each method does. You can do so as all methods have protected visibility, except for the ``run()`` method -which is public and need not be overridden as it is essentially complete. - -.. php:class:: CodeIgniter\\CLI\\GeneratorCommand - - .. php:method:: getClassName() - - :rtype: string - - Gets the class name from input. This can be overridden if name is really - required by providing a prompt. - - .. php:method:: sanitizeClassName(string $class) - - :param string $class: Class name. - :rtype: string - - Trims input, normalize separators, and ensures all paths are in Pascal case. - - .. php:method:: qualifyClassName(string $class) - - :param string $class: Class name. - :rtype: string - - Parses the class name and checks if it is already fully qualified. - - .. php:method:: getRootNamespace() - - :rtype: string - - Gets the root namespace from input. Defaults to value of ``APP_NAMESPACE``. - - .. php:method:: getNamespacedClass(string $rootNamespace, string $class) - - :param string $rootNamespace: The root namespace of the class. - :param string $class: Class name - :returns: The fully qualified class name - :rtype: string - - Gets the qualified class name. This should be implemented. - - .. php:method:: buildPath(string $class) - - :param string $class: The fully qualified class name - :returns: The absolute path to where the class will be saved. - :rtype: string - :throws: RuntimeException - - Builds the file path from the class name. - - .. php:method:: modifyBasename(string $filename) - - :param string $filename: The basename of the file path. - :returns: A modified basename for the file. - :rtype: string - - Provides last chance for child generators to change the file's basename before saving. - This is useful for migration files where the basename has a date component. - - .. php:method:: buildClassContents(string $class) - - :param string $class: The fully qualified class name. - :rtype: string - - Builds the contents for class being generated, doing all the replacements necessary in the template. - - .. php:method:: getTemplate() - - :rtype: string - - Gets the template for the class being generated. This must be implemented. - - .. php:method:: getNamespace(string $class) - - :param string $class: The fully qualified class name. - :rtype: string - - Retrieves the namespace part from the fully qualified class name. - - .. php:method:: setReplacements(string $template, string $class) - - :param string $template: The template string to use. - :param string $class: The fully qualified class name. - :returns: The template string with all annotations replaced. - :rtype: string - - Performs all the necessary replacements. - - .. php:method:: sortImports(string $template) - - :param string $template: The template file. - :returns: The template file with all imports already sorted. - :rtype: string - - Alphabetically sorts the imports for a given template. - -.. warning:: Child generators should make sure to implement ``GeneratorCommand``'s two abstract methods: - ``getNamespacedClass`` and ``getTemplate``, or else you will get a PHP fatal error. +(3) ``App\Database\Migrations\_User``; and +(4) ``App\Database\Seeds\User``. -.. note:: ``GeneratorCommand`` has the default argument of ``['name' => 'Class name']``. You can - override the description by supplying the name in your ``$arguments`` property, e.g., ``['name' => 'Module class name']``. +To include an ``Entity`` class in the scaffolded files, just include the ``--return entity`` to the command +and it will be passed to the model generator. -.. note:: ``GeneratorCommand`` has the default options of ``-n`` and ``--force``. Child classes cannot override - these two properties as they are crucial in the implementation of the code generation. +************** +GeneratorTrait +************** -.. note:: Generators are default listed under the ``Generators`` namespace because it is the default group - name in ``GeneratorCommand``. If you want to have your own generator listed elsewhere under a different - namespace, you will just need to provide the ``$group`` property in your child generator, - e.g., ``protected $group = 'CodeIgniter';``. +All generator commands must use the ``GeneratorTrait`` to fully utilize its methods that are used in code +generation.