Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 19 additions & 75 deletions src/BladeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Str;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Compilers\ComponentTagCompiler;
use Illuminate\View\Factory;
use Livewire\Blaze\Compiler\DirectiveCompiler;
use Livewire\Blaze\Support\LaravelRegex;
use ReflectionClass;
Expand All @@ -16,6 +17,7 @@ class BladeService

public function __construct(
public BladeCompiler $compiler,
protected Factory $view,
) {
$this->tagCompiler = new ComponentTagCompiler(blade: $compiler);
}
Expand Down Expand Up @@ -194,87 +196,29 @@ public function viewCacheInvalidationHook(callable $callback): void
*/
public function componentNameToPath($name): string
{
$viewFinder = app('view')->getFinder();
$finder = $this->view->getFinder();

$reflection = new \ReflectionClass($this->compiler);
$pathsProperty = $reflection->getProperty('anonymousComponentPaths');
$paths = $pathsProperty->getValue($this->compiler) ?? [];

if (str_contains($name, '::')) {
[$namespace, $componentName] = explode('::', $name, 2);
$componentPath = str_replace('.', '/', $componentName);

foreach ($paths as $pathData) {
if (isset($pathData['prefix']) && $pathData['prefix'] === $namespace) {
$basePath = rtrim($pathData['path'], '/');

$fullPath = $basePath.'/'.$componentPath.'.blade.php';
if (file_exists($fullPath)) {
return $fullPath;
}

$indexPath = $basePath.'/'.$componentPath.'/index.blade.php';
if (file_exists($indexPath)) {
return $indexPath;
}

$lastSegment = basename($componentPath);
$sameNamePath = $basePath.'/'.$componentPath.'/'.$lastSegment.'.blade.php';
if (file_exists($sameNamePath)) {
return $sameNamePath;
}
}
}

try {
$viewName = str_replace('::', '::components.', $name);
return $viewFinder->find($viewName);
} catch (\Exception $e) {
try {
return $viewFinder->find(str_replace('::', '::components.', $name).'.index');
} catch (\Exception $e2) {
return '';
}
}
if (! is_null($guess = $this->guessAnonymousComponentUsingNamespaces($this->view, $name)) ||
! is_null($guess = $this->guessAnonymousComponentUsingPaths($this->view, $name))) {
return $finder->find($guess);
}

$componentPath = str_replace('.', '/', $name);

foreach ($paths as $pathData) {
if (! isset($pathData['prefix']) || $pathData['prefix'] === null) {
$registeredPath = $pathData['path'] ?? $pathData;

if (is_string($registeredPath)) {
$basePath = rtrim($registeredPath, '/');
return '';
}

$fullPath = $basePath.'/'.$componentPath.'.blade.php';
if (file_exists($fullPath)) {
return $fullPath;
}
protected function guessAnonymousComponentUsingNamespaces(Factory $viewFactory, string $component): string|null
{
$reflection = new \ReflectionClass($this->tagCompiler);
$method = $reflection->getMethod('guessAnonymousComponentUsingNamespaces');

$indexPath = $basePath.'/'.$componentPath.'/index.blade.php';
if (file_exists($indexPath)) {
return $indexPath;
}
return $method->invoke($this->tagCompiler, $viewFactory, $component);
}

$lastSegment = basename($componentPath);
$sameNamePath = $basePath.'/'.$componentPath.'/'.$lastSegment.'.blade.php';
if (file_exists($sameNamePath)) {
return $sameNamePath;
}
}
}
}
protected function guessAnonymousComponentUsingPaths(Factory $viewFactory, string $component): string|null
{
$reflection = new \ReflectionClass($this->tagCompiler);
$method = $reflection->getMethod('guessAnonymousComponentUsingPaths');

try {
return $viewFinder->find("components.{$name}");
} catch (\Exception $e) {
try {
return $viewFinder->find("components.{$name}.index");
} catch (\Exception $e2) {
return '';
}
}
return $method->invoke($this->tagCompiler, $viewFactory, $component);
}

}
21 changes: 21 additions & 0 deletions tests/BladeServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Support\Facades\Blade;
use Livewire\Blaze\BladeService;

test('componentNameToPath', function ($callback, $prefix, $name, $path) {
$callback();

expect(app(BladeService::class)->componentNameToPath($prefix . $name))->toBe($path);
})
->with([
'default path' => [fn () => null, 'dummy.'],
'custom path' => [fn () => Blade::anonymousComponentPath(fixture_path('views/components/dummy')), ''],
'custom path with prefix' => [fn () => Blade::anonymousComponentPath(fixture_path('views/components/dummy'), 'dummy'), 'dummy::'],
'custom namespace' => [fn () => Blade::anonymousComponentNamespace('components.dummy', 'dummy'), 'dummy::'],
])
->with([
'exact file' => ['foo', fixture_path('views/components/dummy/foo.blade.php')],
'index file' => ['bar', fixture_path('views/components/dummy/bar/index.blade.php')],
'directory name' => ['baz', fixture_path('views/components/dummy/baz/baz.blade.php')],
]);
4 changes: 2 additions & 2 deletions tests/Compiler/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$node = app(Parser::class)->parse($input)[0];
$compiled = app(Compiler::class)->compile($node);

$path = fixture_path('components/input.blade.php');
$path = fixture_path('views/components/input.blade.php');
$hash = Utils::hash($path);

expect($compiled->render())->toEqualCollapsingWhitespace(join('', [
Expand Down Expand Up @@ -38,7 +38,7 @@
$node = app(Parser::class)->parse($input)[0];
$compiled = app(Compiler::class)->compile($node);

$path = fixture_path('components/card.blade.php');
$path = fixture_path('views/components/card.blade.php');
$hash = Utils::hash($path);

expect($compiled->render())->toEqualCollapsingWhitespace(join('', [
Expand Down
4 changes: 2 additions & 2 deletions tests/Compiler/WrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Livewire\Blaze\BladeService;

test('wraps component templates into function definitions', function () {
$path = fixture_path('components/input.blade.php');
$path = fixture_path('views/components/input.blade.php');
$source = file_get_contents($path);
$hash = Utils::hash($path);

Expand All @@ -31,7 +31,7 @@
});

test('compiles aware props', function () {
$path = fixture_path('components/input-aware.blade.php');
$path = fixture_path('views/components/input-aware.blade.php');
$source = file_get_contents($path);
$hash = Utils::hash($path);

Expand Down
20 changes: 10 additions & 10 deletions tests/Folder/FoldableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$input = '<x-foldable.input :type="$type" />';

$node = app(Parser::class)->parse($input)[0];
$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/input.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/input.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(
'<input type="{{ $type }}" >'
Expand All @@ -35,7 +35,7 @@
;

$node = app(Parser::class)->parse($input)[0];
$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/card.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/card.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(<<<'HTML'
<div>
Expand All @@ -53,7 +53,7 @@
$input = '<x-foldable.input :disabled="false" />';

$node = app(Parser::class)->parse($input)[0];
$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/input.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/input.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(
'<input type="text" >'
Expand All @@ -64,7 +64,7 @@
$input = '<x-foldable.input :disabled="null" />';

$node = app(Parser::class)->parse($input)[0];
$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/input.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/input.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(
'<input type="text" >'
Expand All @@ -75,7 +75,7 @@
$input = '<x-foldable.input-aware />';

$node = app(Parser::class)->parse($input)[0];
$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/input-aware.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/input-aware.blade.php')), app(BladeRenderer::class), app(BladeService::class));

$node->setParentsAttributes([
'type' => new Attribute(
Expand Down Expand Up @@ -106,7 +106,7 @@
),
]);

$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/input-aware.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/input-aware.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(
'<input type="{{ $type }}" >'
Expand All @@ -117,7 +117,7 @@
$input = '<x-foldable.input :readonly="$readonly" />';

$node = app(Parser::class)->parse($input)[0];
$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/input.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/input.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(
sprintf('<input %s type="text" >', join('', [
Expand All @@ -134,7 +134,7 @@
$node = app(Parser::class)->parse($input)[0];
$node->hasAwareDescendants = true;

$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/card.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/card.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(join('', [
'<?php $__blaze->pushData([\'name\' => \'John\']); $__env->pushConsumableComponentData([\'name\' => \'John\']); ?>',
Expand All @@ -149,7 +149,7 @@
$node = app(Parser::class)->parse($input)[0];
$node->hasAwareDescendants = true;

$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/card.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/card.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(join('', [
'<?php $__blaze->pushData([\'name\' => $name]); $__env->pushConsumableComponentData([\'name\' => $name]); ?>',
Expand All @@ -164,7 +164,7 @@
$node = app(Parser::class)->parse($input)[0];
$node->hasAwareDescendants = true;

$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/card.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/card.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(join('', [
'<?php $__blaze->pushData([\'name\' => \'Mr. \'.e($name)]); $__env->pushConsumableComponentData([\'name\' => \'Mr. \'.e($name)]); ?>',
Expand Down
4 changes: 2 additions & 2 deletions tests/Folder/FolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
test('folds components with no blaze directive if enabled in config', function () {
$input = '<x-foldable.input-no-blaze />';

app(Config::class)->add(fixture_path('components/foldable'), fold: true);
app(Config::class)->add(fixture_path('views/components/foldable'), fold: true);

$node = app(Parser::class)->parse($input)[0];
$folded = app(Folder::class)->fold($node);
Expand All @@ -221,7 +221,7 @@
test('folds components with blaze directive even if disabled in config', function () {
$input = '<x-foldable.input />';

app(Config::class)->add(fixture_path('components/foldable'), fold: false);
app(Config::class)->add(fixture_path('views/components/foldable'), fold: false);

$node = app(Parser::class)->parse($input)[0];
$folded = app(Folder::class)->fold($node);
Expand Down
6 changes: 3 additions & 3 deletions tests/Folder/UnblazeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$input = '<x-foldable.input-unblaze name="address" />';

$node = app(Parser::class)->parse($input)[0];
$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/input-unblaze.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/input-unblaze.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(
sprintf('<input %s >', join('', [
Expand All @@ -26,7 +26,7 @@
$input = '<x-foldable.nested-input-unblaze />';

$node = app(Parser::class)->parse($input)[0];
$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/nested-input-unblaze.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/nested-input-unblaze.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(
sprintf('<div> <input %s ></div>', join('', [
Expand All @@ -42,7 +42,7 @@
$input = '<x-foldable.input-unblaze :name="$field" />';

$node = app(Parser::class)->parse($input)[0];
$foldable = new Foldable($node, new ComponentSource(fixture_path('components/foldable/input-unblaze.blade.php')), app(BladeRenderer::class), app(BladeService::class));
$foldable = new Foldable($node, new ComponentSource(fixture_path('views/components/foldable/input-unblaze.blade.php')), app(BladeRenderer::class), app(BladeService::class));

expect($foldable->fold())->toEqualCollapsingWhitespace(
sprintf('<input %s >', join('', [
Expand Down
4 changes: 2 additions & 2 deletions tests/Memoizer/MemoizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$node = app(Parser::class)->parse($input)[0];
$memoized = app(Memoizer::class)->memoize($node);

$path = fixture_path('components/memoizable/avatar.blade.php');
$path = fixture_path('views/components/memoizable/avatar.blade.php');
$hash = Utils::hash($path);

expect($memoized->render())->toEqualCollapsingWhitespace(join('', [
Expand Down Expand Up @@ -58,7 +58,7 @@
test('memoizes components without blaze directive if enabled in config', function () {
$input = '<x-memoizable.avatar-no-blaze />';

app(Config::class)->add(fixture_path('components/memoizable'), memo: true);
app(Config::class)->add(fixture_path('views/components/memoizable'), memo: true);

$node = app(Parser::class)->parse($input)[0];
$memoized = app(Memoizer::class)->memoize($node);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function getEnvironmentSetUp($app)
]);

$app['blade.compiler']->anonymousComponentPath(
__DIR__.'/fixtures/components',
__DIR__.'/fixtures/views/components',
);

// Isolate compiled view paths for parallel testing to prevent
Expand Down
Empty file.
Empty file.
Empty file.