Skip to content

Commit d778ec3

Browse files
authored
Merge pull request #7686 from kenjis/fix-view-cells-same-short-name
fix: [ViewCells] when there are cells with the same short name, only the first cell is loaded
2 parents f30542c + c193ce7 commit d778ec3

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

system/View/Cell.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ protected function determineClass(string $library): array
178178
}
179179

180180
// locate and return an instance of the cell
181-
$object = Factories::cells($class);
181+
// @TODO extend Factories to be able to load classes with the same short name.
182+
$object = class_exists($class) ? new $class() : Factories::cells($class);
182183

183184
if (! is_object($object)) {
184185
throw ViewException::forInvalidCellClass($class);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
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\OtherCells;
13+
14+
/**
15+
* Two classes with the same short name.
16+
*
17+
* - Tests\Support\View\SampleClass
18+
* - Tests\Support\View\OtherCells\SampleClass
19+
*/
20+
class SampleClass
21+
{
22+
public function hello()
23+
{
24+
return 'Good-bye!';
25+
}
26+
}

tests/system/View/CellTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ public function testDisplayRendersWithNamespacedClass()
113113
$this->assertSame($expected, $this->cell->render('\Tests\Support\View\SampleClass::hello'));
114114
}
115115

116+
public function testDisplayRendersTwoCellsWithSameShortName()
117+
{
118+
$output = $this->cell->render('\Tests\Support\View\SampleClass::hello');
119+
120+
$this->assertSame('Hello', $output);
121+
122+
$output = $this->cell->render('\Tests\Support\View\OtherCells\SampleClass::hello');
123+
124+
$this->assertSame('Good-bye!', $output);
125+
}
126+
116127
public function testDisplayRendersWithValidParamString()
117128
{
118129
$params = 'one=two,three=four';

0 commit comments

Comments
 (0)