Skip to content

Commit 8d4120a

Browse files
committed
Merge branch 'packages-anonymous-components' into 7.x
2 parents 455b343 + 3d0de23 commit 8d4120a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Illuminate/View/Compilers/ComponentTagCompiler.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Filesystem\Filesystem;
99
use Illuminate\Support\Str;
1010
use Illuminate\View\AnonymousComponent;
11+
use Illuminate\View\ViewFinderInterface;
1112
use InvalidArgumentException;
1213
use ReflectionClass;
1314

@@ -237,7 +238,7 @@ protected function componentClass(string $component)
237238
return $class;
238239
}
239240

240-
if ($viewFactory->exists($view = "components.{$component}")) {
241+
if ($viewFactory->exists($view = $this->guessViewName($component))) {
241242
return $view;
242243
}
243244

@@ -265,6 +266,25 @@ public function guessClassName(string $component)
265266
return $namespace.'View\\Components\\'.implode('\\', $componentPieces);
266267
}
267268

269+
/**
270+
* Guess the view name for the given component.
271+
*
272+
* @param string $name
273+
* @return string
274+
*/
275+
public function guessViewName($name)
276+
{
277+
$prefix = 'components.';
278+
279+
$delimiter = ViewFinderInterface::HINT_PATH_DELIMITER;
280+
281+
if (Str::contains($name, $delimiter)) {
282+
return Str::replaceFirst($delimiter, $delimiter.$prefix, $name);
283+
}
284+
285+
return $prefix.$name;
286+
}
287+
268288
/**
269289
* Partition the data and extra attributes from the given array of attributes.
270290
*

tests/View/Blade/BladeComponentTagCompilerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ public function testClasslessComponents()
205205
'@endcomponentClass', trim($result));
206206
}
207207

208+
public function testPackagesClasslessComponents()
209+
{
210+
$container = new Container;
211+
$container->instance(Application::class, $app = Mockery::mock(Application::class));
212+
$container->instance(Factory::class, $factory = Mockery::mock(Factory::class));
213+
$app->shouldReceive('getNamespace')->andReturn('App\\');
214+
$factory->shouldReceive('exists')->andReturn(true);
215+
Container::setInstance($container);
216+
217+
$result = $this->compiler()->compileTags('<x-package::anonymous-component :name="\'Taylor\'" :age="31" wire:model="foo" />');
218+
219+
$this->assertSame("@component('Illuminate\View\AnonymousComponent', 'package::anonymous-component', ['view' => 'package::components.anonymous-component','data' => ['name' => 'Taylor','age' => 31,'wire:model' => 'foo']])
220+
<?php \$component->withAttributes(['name' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute('Taylor'),'age' => 31,'wire:model' => 'foo']); ?>\n".
221+
'@endcomponentClass', trim($result));
222+
}
223+
208224
public function testAttributeSanitization()
209225
{
210226
$class = new class {

0 commit comments

Comments
 (0)