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

new make:cell command #6864

Merged
merged 5 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
71 changes: 53 additions & 18 deletions system/CLI/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,57 @@ trait GeneratorTrait

/**
* Execute the command.
*
* @deprecated use generateClass() instead
*/
protected function execute(array $params): void
{
$this->generateClass($params);
}

/**
* Generates a class file from an existing template.
*/
protected function generateClass(array $params)
{
$this->params = $params;

// Get the fully qualified class name from the input.
$class = $this->qualifyClassName();

// Get the file path from class name.
$target = $this->buildPath($class);

// Check if path is empty.
if (empty($target)) {
return;
}

$this->generateFile($target, $this->buildContent($class));
}

/**
* Generate a view file from an existing template.
*/
protected function generateView(string $view, array $params)
{
$this->params = $params;

$target = $this->buildPath($view);

// Check if path is empty.
if (empty($target)) {
return;
}

$this->generateFile($target, $this->buildContent($view));
}

/**
* Handles writing the file to disk, and all of the safety checks around that.
*/
private function generateFile(string $target, string $content): void
{
if ($this->getOption('namespace') === 'CodeIgniter') {
// @codeCoverageIgnoreStart
CLI::write(lang('CLI.generator.usingCINamespace'), 'yellow');
Expand All @@ -108,30 +154,19 @@ protected function execute(array $params): void
// @codeCoverageIgnoreEnd
}

// Get the fully qualified class name from the input.
$class = $this->qualifyClassName();

// Get the file path from class name.
$path = $this->buildPath($class);

// Check if path is empty.
if (empty($path)) {
return;
}

$isFile = is_file($path);
$isFile = is_file($target);

// Overwriting files unknowingly is a serious annoyance, So we'll check if
// we are duplicating things, If 'force' option is not supplied, we bail.
if (! $this->getOption('force') && $isFile) {
CLI::error(lang('CLI.generator.fileExist', [clean_path($path)]), 'light_gray', 'red');
CLI::error(lang('CLI.generator.fileExist', [clean_path($target)]), 'light_gray', 'red');
CLI::newLine();

return;
}

// Check if the directory to save the file is existing.
$dir = dirname($path);
$dir = dirname($target);

if (! is_dir($dir)) {
mkdir($dir, 0755, true);
Expand All @@ -141,23 +176,23 @@ protected function execute(array $params): void

// 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))) {
if (! write_file($target, $content)) {
// @codeCoverageIgnoreStart
CLI::error(lang('CLI.generator.fileError', [clean_path($path)]), 'light_gray', 'red');
CLI::error(lang('CLI.generator.fileError', [clean_path($target)]), 'light_gray', 'red');
CLI::newLine();

return;
// @codeCoverageIgnoreEnd
}

if ($this->getOption('force') && $isFile) {
CLI::write(lang('CLI.generator.fileOverwrite', [clean_path($path)]), 'yellow');
CLI::write(lang('CLI.generator.fileOverwrite', [clean_path($target)]), 'yellow');
CLI::newLine();

return;
}

CLI::write(lang('CLI.generator.fileCreate', [clean_path($path)]), 'green');
CLI::write(lang('CLI.generator.fileCreate', [clean_path($target)]), 'green');
CLI::newLine();
}

Expand Down
102 changes: 102 additions & 0 deletions system/Commands/Generators/CellGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands\Generators;

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

/**
* Generates a skeleton Cell and its view.
*/
class CellGenerator extends BaseCommand
{
use GeneratorTrait;

/**
* The Command's Group
*
* @var string
*/
protected $group = 'Generators';

/**
* The Command's Name
*
* @var string
*/
protected $name = 'make:cell';

/**
* The Command's Description
*
* @var string
*/
protected $description = 'Generates a new Cell file and its view.';

/**
* The Command's Usage
*
* @var string
*/
protected $usage = 'make:cell <name> [options]';

/**
* The Command's Arguments
*
* @var array
*/
protected $arguments = [
'name' => 'The entity class name.',
];

/**
* The Command's Options
*
* @var array
*/
protected $options = [
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
'--suffix' => 'Append the component title to the class name (e.g. User => UserCell).',
'--force' => 'Force overwrite existing file.',
];

/**
* Actually execute a command.
*/
public function run(array $params)
{
// Generate the Class first
$this->component = 'Cell';
$this->directory = 'Cells';
$this->template = 'cell.tpl.php';
$this->classNameLang = 'CLI.generator.className.cell';

$this->generateClass($params);

// Generate the View

// Form the view name
$segments = explode('\\', $this->qualifyClassName());

$view = array_pop($segments);
$view = str_replace('Cell', '', decamelize($view));
if (strpos($view, '_cell') === false) {
$view .= '_cell';
}
$segments[] = $view;
$view = implode('\\', $segments);

$this->template = 'cell_view.tpl.php';

$this->generateView($view, $params);
}
}
10 changes: 10 additions & 0 deletions system/Commands/Generators/Views/cell.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<@php

namespace {namespace};

use CodeIgniter\View\Cells\Cell;

class {class} extends Cell
{
//
}
3 changes: 3 additions & 0 deletions system/Commands/Generators/Views/cell_view.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<!-- Your HTML here -->
</div>
98 changes: 98 additions & 0 deletions tests/system/Commands/CellGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;

/**
* @internal
lonnieezell marked this conversation as resolved.
Show resolved Hide resolved
*/
final class CellGeneratorTest extends CIUnitTestCase
{
use StreamFilterTrait;

protected function tearDown(): void
{
$dirName = APPPATH . DIRECTORY_SEPARATOR . 'Cells';
// remove dir
if (is_dir($dirName)) {
$files = array_diff(scandir($dirName), ['.', '..']);

foreach ($files as $file) {
(is_dir("{$dirName}/{$file}")) ? rmdir("{$dirName}/{$file}") : unlink("{$dirName}/{$file}");
}
rmdir($dirName);
}
}

protected function getFileContents(string $filepath): string
{
if (! is_file($filepath)) {
return '';
}

return file_get_contents($filepath) ?: '';
}

/**
* @group Others
*/
lonnieezell marked this conversation as resolved.
Show resolved Hide resolved
public function testGenerateCell()
{
command('make:cell RecentCell');

// Check the class was generated
$file = APPPATH . 'Cells/RecentCell.php';
$this->assertFileExists($file);
$contents = $this->getFileContents($file);
$this->assertStringContainsString('class RecentCell extends Cell', $contents);

// Check the view was generated
$file = APPPATH . 'Cells/recent_cell.php';
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$this->assertFileExists(APPPATH . 'Cells/recent_cell.php');
lonnieezell marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @group Others
*/
lonnieezell marked this conversation as resolved.
Show resolved Hide resolved
public function testGenerateCellSimpleName()
{
command('make:cell Another');

// Check the class was generated
$file = APPPATH . 'Cells/Another.php';
$this->assertFileExists($file);
$contents = $this->getFileContents($file);
$this->assertStringContainsString('class Another extends Cell', $contents);

// Check the view was generated
$file = APPPATH . 'Cells/another_cell.php';
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$this->assertFileExists(APPPATH . 'Cells/another_cell.php');
lonnieezell marked this conversation as resolved.
Show resolved Hide resolved
}

private function cleanUp()
lonnieezell marked this conversation as resolved.
Show resolved Hide resolved
{
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer());
$file = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14)));
$dir = dirname($file);

if (is_file($file)) {
unlink($file);
}
if (is_dir($dir) && strpos($dir, 'Cells') !== false) {
rmdir($dir);
}
}
}
9 changes: 9 additions & 0 deletions user_guide_src/source/outgoing/view_cells.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ At the most basic level, all you need to implement within the class are public p
<?= $message; ?>
</div>

Generating Cell via Command
===========================

You can also create a controlled cell via a built in command from the CLI. The command is ``php spark make:cell``. It takes one argument, the name of the cell to create. The name should be in PascalCase, and the class will be created in the ``app/Cells`` directory. The view file will also be created in the ``app/Views/cells`` directory.

```console
php spark make:cell AlertMessage
```

Using a Different View
======================

Expand Down