Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finishing touches to generator refactor #4197

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Config/Generators.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}
16 changes: 12 additions & 4 deletions system/CLI/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

/**
Expand Down
13 changes: 8 additions & 5 deletions system/Commands/Generators/ConfigGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}
6 changes: 3 additions & 3 deletions system/Commands/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ControllerGenerator extends BaseCommand
'--force' => 'Force overwrite existing file.',
];

/**
/**
* Actually execute a command.
*
* @param array $params
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions system/Commands/Generators/MigrateCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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'))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Generates a complete set of scaffold files.
*/
class ComponentGenerator extends BaseCommand
class ScaffoldGenerator extends BaseCommand
{
use GeneratorTrait;

Expand All @@ -34,7 +34,7 @@ class ComponentGenerator extends BaseCommand
*
* @var string
*/
protected $name = 'make:component';
protected $name = 'make:scaffold';

/**
* The Command's Description
Expand All @@ -48,7 +48,7 @@ class ComponentGenerator extends BaseCommand
*
* @var string
*/
protected $usage = 'make:component <name> [options]';
protected $usage = 'make:scaffold <name> [options]';

/**
* The Command's Arguments
Expand All @@ -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.',
];

Expand All @@ -83,7 +83,7 @@ class ComponentGenerator extends BaseCommand
public function run(array $params)
{
$this->params = $params;

$options = [];

if ($this->getOption('namespace'))
Expand Down
1 change: 0 additions & 1 deletion system/Commands/Generators/SeederGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace CodeIgniter\Commands\Generators;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\GeneratorTrait;

/**
Expand Down
8 changes: 4 additions & 4 deletions system/Commands/Generators/SessionMigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion system/Commands/Generators/Views/controller.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function index()
{
//
}

/**
* Return the properties of a resource object
*
Expand Down
2 changes: 1 addition & 1 deletion system/Commands/Generators/Views/migration.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class {class} extends Migration
{
<?php if ($session): ?>
protected $DBGroup = '<?= $DBGroup ?>';

public function up()
{
$this->forge->addField([
Expand Down
8 changes: 4 additions & 4 deletions system/Commands/Generators/Views/validation.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class {class}
{
public function custom_rule(): bool
{
return true;
}
// public function custom_rule(): bool
// {
// return true;
// }
}
12 changes: 10 additions & 2 deletions tests/system/Commands/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;

class ComponentGeneratorTest extends CIUnitTestCase
class ScaffoldGeneratorTest extends CIUnitTestCase
{
protected $streamFilter;

Expand All @@ -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
Expand All @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand Down
Loading