Skip to content

Commit 1ff0379

Browse files
committed
integration tests and custom prefixes
1 parent 5a1eb8b commit 1ff0379

File tree

8 files changed

+90
-12
lines changed

8 files changed

+90
-12
lines changed

src/Illuminate/View/Compilers/BladeCompiler.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -693,15 +693,22 @@ public function getClassComponentAliases()
693693
* Register a new anonymous component path.
694694
*
695695
* @param string $path
696+
* @param string|null $prefix
696697
* @return void
697698
*/
698-
public function anonymousComponentPath(string $path)
699+
public function anonymousComponentPath(string $path, string $prefix = null)
699700
{
700-
$this->anonymousComponentPaths[] = $path;
701+
$prefixHash = md5($prefix ?: $path);
702+
703+
$this->anonymousComponentPaths[] = [
704+
'path' => $path,
705+
'prefix' => $prefix,
706+
'prefixHash' => $prefixHash,
707+
];
701708

702709
Container::getInstance()
703710
->make(ViewFactory::class)
704-
->addNamespace(md5($path), $path);
711+
->addNamespace($prefixHash, $path);
705712
}
706713

707714
/**

src/Illuminate/View/Compilers/ComponentTagCompiler.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,22 @@ public function componentClass(string $component)
314314
*/
315315
protected function guessAnonymousComponentUsingPaths(Factory $viewFactory, string $component)
316316
{
317-
if (str_contains($component, ViewFinderInterface::HINT_PATH_DELIMITER)) {
318-
return;
319-
}
317+
$delimiter = ViewFinderInterface::HINT_PATH_DELIMITER;
320318

321319
foreach ($this->blade->getAnonymousComponentPaths() as $path) {
322320
try {
321+
if (str_contains($component, $delimiter) &&
322+
! str_starts_with($component, $path['prefix'].$delimiter)) {
323+
continue;
324+
}
325+
326+
$formattedComponent = str_starts_with($component, $path['prefix'].$delimiter)
327+
? Str::after($component, $delimiter)
328+
: $component;
329+
323330
if (! is_null($guess = match (true) {
324-
$viewFactory->exists($guess = md5($path).ViewFinderInterface::HINT_PATH_DELIMITER.$component) => $guess,
325-
$viewFactory->exists($guess = md5($path).ViewFinderInterface::HINT_PATH_DELIMITER.$component.'.index') => $guess,
331+
$viewFactory->exists($guess = $path['prefixHash'].$delimiter.$formattedComponent) => $guess,
332+
$viewFactory->exists($guess = $path['prefixHash'].$delimiter.$formattedComponent.'.index') => $guess,
326333
default => null,
327334
})) {
328335
return $guess;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\View;
4+
5+
use Illuminate\Support\Facades\Blade;
6+
use Illuminate\Support\Facades\View;
7+
use InvalidArgumentException;
8+
use Orchestra\Testbench\TestCase;
9+
10+
class BladeAnonymousComponentTest extends TestCase
11+
{
12+
public function test_anonymous_components_with_custom_paths_can_be_rendered()
13+
{
14+
Blade::anonymousComponentPath(__DIR__.'/anonymous-components-1', 'layouts');
15+
Blade::anonymousComponentPath(__DIR__.'/anonymous-components-2');
16+
17+
$view = View::make('page')->render();
18+
19+
$this->assertTrue(str_contains($view, 'Panel content.'));
20+
$this->assertTrue(str_contains($view, 'class="app-layout"'));
21+
$this->assertTrue(str_contains($view, 'class="danger-button"'));
22+
}
23+
24+
public function test_anonymous_components_with_custom_paths_cant_be_rendered_as_normal_views()
25+
{
26+
$this->expectException(InvalidArgumentException::class);
27+
28+
Blade::anonymousComponentPath(__DIR__.'/anonymous-components-1', 'layouts');
29+
Blade::anonymousComponentPath(__DIR__.'/anonymous-components-2');
30+
31+
$view = View::make('layouts::app')->render();
32+
}
33+
34+
public function test_anonymous_components_with_custom_paths_cant_be_rendered_as_normal_views_even_with_no_prefix()
35+
{
36+
$this->expectException(InvalidArgumentException::class);
37+
38+
Blade::anonymousComponentPath(__DIR__.'/anonymous-components-1', 'layouts');
39+
Blade::anonymousComponentPath(__DIR__.'/anonymous-components-2');
40+
41+
$view = View::make('panel')->render();
42+
}
43+
44+
protected function getEnvironmentSetUp($app)
45+
{
46+
$app['config']->set('view.paths', [__DIR__.'/anonymous-components-templates']);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="app-layout">
2+
{{ $slot }}
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="danger-button">
2+
Danger button.
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="panel">
2+
{{ $slot }}
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<x-layouts::app>
2+
<x-panel>
3+
Panel content.
4+
5+
<x-buttons.danger />
6+
</x-panel>
7+
</x-layouts::app>

tests/View/Blade/BladeComponentTagCompilerTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -585,14 +585,14 @@ public function testClasslessComponentsWithAnonymousComponentPath()
585585
$blade = m::mock(BladeCompiler::class)->makePartial();
586586

587587
$blade->shouldReceive('getAnonymousComponentPaths')->once()->andReturn([
588-
'test-directory',
588+
['path' => 'test-directory', 'prefix' => null, 'prefixHash' => md5('test-directory')],
589589
]);
590590

591591
$compiler = $this->compiler([], [], $blade);
592592

593593
$result = $compiler->compileTags('<x-panel />');
594594

595-
$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\View\AnonymousComponent', 'panel', ['view' => '8ee975052836fdc7da2267cf8a580b80::panel.index','data' => []])
595+
$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\View\AnonymousComponent', 'panel', ['view' => '".md5('test-directory')."::panel.index','data' => []])
596596
<?php if (isset(\$attributes) && \$attributes instanceof Illuminate\View\ComponentAttributeBag && \$constructor = (new ReflectionClass(Illuminate\View\AnonymousComponent::class))->getConstructor()): ?>
597597
<?php \$attributes = \$attributes->except(collect(\$constructor->getParameters())->map->getName()->all()); ?>
598598
<?php endif; ?>
@@ -618,14 +618,14 @@ public function testClasslessIndexComponentsWithAnonymousComponentPath()
618618
$blade = m::mock(BladeCompiler::class)->makePartial();
619619

620620
$blade->shouldReceive('getAnonymousComponentPaths')->once()->andReturn([
621-
'test-directory',
621+
['path' => 'test-directory', 'prefix' => null, 'prefixHash' => md5('test-directory')],
622622
]);
623623

624624
$compiler = $this->compiler([], [], $blade);
625625

626626
$result = $compiler->compileTags('<x-panel />');
627627

628-
$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\View\AnonymousComponent', 'panel', ['view' => '8ee975052836fdc7da2267cf8a580b80::panel','data' => []])
628+
$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\View\AnonymousComponent', 'panel', ['view' => '".md5('test-directory')."::panel','data' => []])
629629
<?php if (isset(\$attributes) && \$attributes instanceof Illuminate\View\ComponentAttributeBag && \$constructor = (new ReflectionClass(Illuminate\View\AnonymousComponent::class))->getConstructor()): ?>
630630
<?php \$attributes = \$attributes->except(collect(\$constructor->getParameters())->map->getName()->all()); ?>
631631
<?php endif; ?>

0 commit comments

Comments
 (0)