Skip to content

Commit c929fc8

Browse files
authored
Merge pull request #7392 from sammyskills/fix-view-cells
fix: view cell cannot locate the auto-generated view file
2 parents 2cf4336 + d51827f commit c929fc8

File tree

6 files changed

+38
-8
lines changed

6 files changed

+38
-8
lines changed

system/Commands/Generators/CellGenerator.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,8 @@ public function run(array $params)
8888
// Form the view name
8989
$segments = explode('\\', $this->qualifyClassName());
9090

91-
$view = array_pop($segments);
92-
$view = str_replace('Cell', '', decamelize($view));
93-
if (strpos($view, '_cell') === false) {
94-
$view .= '_cell';
95-
}
91+
$view = array_pop($segments);
92+
$view = decamelize($view);
9693
$segments[] = $view;
9794
$view = implode('\\', $segments);
9895

system/View/Cells/Cell.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ final protected function view(?string $view, array $data = []): string
7373

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

8085
// Locate our view, preferring the directory of the class.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support\View\Cells;
13+
14+
use CodeIgniter\View\Cells\Cell;
15+
16+
class AwesomeCell extends Cell
17+
{
18+
public string $message = 'Found!';
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div><?= $message ?></div>

tests/system/Commands/CellGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function testGenerateCellSimpleName()
7373
$this->assertStringContainsString('class Another extends Cell', $contents);
7474

7575
// Check the view was generated
76-
$file = APPPATH . 'Cells/another_cell.php';
76+
$file = APPPATH . 'Cells/another.php';
7777
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
7878
$this->assertFileExists($file);
7979
}

tests/system/View/ControlledCellTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\Test\CIUnitTestCase;
1515
use CodeIgniter\View\Exceptions\ViewException;
1616
use Tests\Support\View\Cells\AdditionCell;
17+
use Tests\Support\View\Cells\AwesomeCell;
1718
use Tests\Support\View\Cells\ColorsCell;
1819
use Tests\Support\View\Cells\GreetingCell;
1920
use Tests\Support\View\Cells\ListerCell;
@@ -36,6 +37,13 @@ public function testCellRendersDefaultValues()
3637
$this->assertStringContainsString('Hello World', $result);
3738
}
3839

40+
public function testCellRendersViewWithActualClassName()
41+
{
42+
$result = view_cell(AwesomeCell::class);
43+
44+
$this->assertStringContainsString('Found!', $result);
45+
}
46+
3947
public function testCellWithNamedView()
4048
{
4149
$result = view_cell(SimpleNotice::class);

0 commit comments

Comments
 (0)