Skip to content

Commit d070432

Browse files
daniserSergey Danilchenko
andauthored
[12.x] Fix Blade nested default component resolution for custom namespaces (#55874)
Co-authored-by: Sergey Danilchenko <s.danilchenko@ttbooking.ru>
1 parent ca97e36 commit d070432

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/Illuminate/View/Compilers/ComponentTagCompiler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ public function findClassByComponent(string $component)
409409
if (class_exists($class = $this->namespaces[$prefix].'\\'.$this->formatClassName($segments[1]))) {
410410
return $class;
411411
}
412+
413+
if (class_exists($class = $class.'\\'.Str::afterLast($class, '\\'))) {
414+
return $class;
415+
}
412416
}
413417

414418
/**

tests/View/Blade/BladeComponentTagCompilerTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ public function testNestedDefaultComponentParsing()
161161
'@endComponentClass##END-COMPONENT-CLASS##</div>', trim($result));
162162
}
163163

164+
public function testCustomNamespaceNestedDefaultComponentParsing()
165+
{
166+
$this->mockViewFactory();
167+
$result = $this->compiler(namespaces: ['nightshade' => 'Nightshade\\View\\Components'])->compileTags('<div><x-nightshade::accordion /></div>');
168+
169+
$this->assertSame("<div>##BEGIN-COMPONENT-CLASS##@component('Nightshade\View\Components\Accordion\Accordion', 'nightshade::accordion', [])
170+
<?php if (isset(\$attributes) && \$attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
171+
<?php \$attributes = \$attributes->except(\Nightshade\View\Components\Accordion\Accordion::ignoredParameterNames()); ?>
172+
<?php endif; ?>
173+
<?php \$component->withAttributes([]); ?>\n".
174+
'@endComponentClass##END-COMPONENT-CLASS##</div>', trim($result));
175+
}
176+
164177
public function testBasicComponentWithEmptyAttributesParsing()
165178
{
166179
$this->mockViewFactory();
@@ -375,6 +388,18 @@ public function testSelfClosingComponentsCanBeCompiled()
375388
'@endComponentClass##END-COMPONENT-CLASS##</div>', trim($result));
376389
}
377390

391+
public function testClassesCanBeFoundByComponents()
392+
{
393+
$this->mockViewFactory();
394+
$compiler = $this->compiler(namespaces: ['nightshade' => 'Nightshade\\View\\Components']);
395+
396+
$result = $compiler->findClassByComponent('nightshade::calendar');
397+
$this->assertSame('Nightshade\\View\\Components\\Calendar', trim($result));
398+
399+
$result = $compiler->findClassByComponent('nightshade::accordion');
400+
$this->assertSame('Nightshade\\View\\Components\\Accordion\\Accordion', trim($result));
401+
}
402+
378403
public function testClassNamesCanBeGuessed()
379404
{
380405
$container = new Container;
@@ -1004,3 +1029,27 @@ public function render()
10041029
return 'card';
10051030
}
10061031
}
1032+
1033+
namespace Nightshade\View\Components;
1034+
1035+
use Illuminate\View\Component;
1036+
1037+
class Calendar extends Component
1038+
{
1039+
public function render()
1040+
{
1041+
return 'calendar';
1042+
}
1043+
}
1044+
1045+
namespace Nightshade\View\Components\Accordion;
1046+
1047+
use Illuminate\View\Component;
1048+
1049+
class Accordion extends Component
1050+
{
1051+
public function render()
1052+
{
1053+
return 'accordion';
1054+
}
1055+
}

0 commit comments

Comments
 (0)