Skip to content
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
7 changes: 2 additions & 5 deletions system/Commands/Generators/CellGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ public function run(array $params)
// 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';
}
$view = array_pop($segments);
$view = decamelize($view);
$segments[] = $view;
$view = implode('\\', $segments);

Expand Down
9 changes: 7 additions & 2 deletions system/View/Cells/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ final protected function view(?string $view, array $data = []): string

// If no view is specified, we'll try to guess it based on the class name.
if (empty($view)) {
$view = decamelize((new ReflectionClass($this))->getShortName());
$view = str_replace('_cell', '', $view);
// According to the docs, the name of the view file should be the
// snake_cased version of the cell's class name, but for backward
// compatibility, the name also accepts '_cell' being omitted.
$ref = new ReflectionClass($this);
$view = decamelize($ref->getShortName());
$viewPath = dirname($ref->getFileName()) . DIRECTORY_SEPARATOR . $view . '.php';
$view = is_file($viewPath) ? $viewPath : str_replace('_cell', '', $view);
}

// Locate our view, preferring the directory of the class.
Expand Down
19 changes: 19 additions & 0 deletions tests/_support/View/Cells/AwesomeCell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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 Tests\Support\View\Cells;

use CodeIgniter\View\Cells\Cell;

class AwesomeCell extends Cell
{
public string $message = 'Found!';
}
1 change: 1 addition & 0 deletions tests/_support/View/Cells/awesome_cell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><?= $message ?></div>
2 changes: 1 addition & 1 deletion tests/system/Commands/CellGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testGenerateCellSimpleName()
$this->assertStringContainsString('class Another extends Cell', $contents);

// Check the view was generated
$file = APPPATH . 'Cells/another_cell.php';
$file = APPPATH . 'Cells/another.php';
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$this->assertFileExists($file);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/system/View/ControlledCellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\View\Exceptions\ViewException;
use Tests\Support\View\Cells\AdditionCell;
use Tests\Support\View\Cells\AwesomeCell;
use Tests\Support\View\Cells\ColorsCell;
use Tests\Support\View\Cells\GreetingCell;
use Tests\Support\View\Cells\ListerCell;
Expand All @@ -36,6 +37,13 @@ public function testCellRendersDefaultValues()
$this->assertStringContainsString('Hello World', $result);
}

public function testCellRendersViewWithActualClassName()
{
$result = view_cell(AwesomeCell::class);

$this->assertStringContainsString('Found!', $result);
}

public function testCellWithNamedView()
{
$result = view_cell(SimpleNotice::class);
Expand Down