Skip to content
Closed
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
26 changes: 26 additions & 0 deletions src/BladeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,32 @@ public function componentNameToPath($name): string
return '';
}

/**
* Determine if view path is the conventional view for a class-based component.
*/
public function isClassBasedComponentView(string $path): bool
{
$path = str_replace('\\', '/', $path);

if (! preg_match('#^(.*/components/)(.+)$#i', $path, $matches)) {
return false;
}

$afterComponents = $matches[2];

if (! Str::endsWith($afterComponents, '.blade.php')) {
return false;
}

$name = str_replace('/', '.', Str::beforeLast($afterComponents, '.blade.php'));

if ($name === '') {
return false;
}

return $this->hasClassBasedComponent($name);
}

/**
* Determine if a component resolves to a class rather than a blade view.
*
Expand Down
16 changes: 11 additions & 5 deletions src/BlazeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ public function compile(string $template, ?string $path = null): string

$directives = new Directives($source);

if ($path && ($directives->blaze() || $this->config->shouldCompile($path))) {
$shouldWrap = $path
&& ($directives->blaze() || $this->config->shouldCompile($path))
&& ! $this->blade->isClassBasedComponentView($path);

if ($shouldWrap) {
$output = $this->wrapper->wrap($output, $path, $source);
} elseif ($this->isDebugging() && ! $this->isFolding() && $path) {
$output = $this->instrumenter->profileView($output, $path, $source);
Expand Down Expand Up @@ -231,11 +235,13 @@ public function compileForFolding(string $template, ?string $path = null): strin
}

$directives = new Directives($source);
$shouldWrap = $this->config->shouldFold($path)
|| $this->config->shouldMemoize($path)
|| $this->config->shouldCompile($path);
$shouldWrap = ($directives->blaze()
|| $this->config->shouldFold($path)
|| $this->config->shouldMemoize($path)
|| $this->config->shouldCompile($path))
&& ! $this->blade->isClassBasedComponentView($path);

if ($directives->blaze() || $shouldWrap) {
if ($shouldWrap) {
$output = $this->wrapper->wrap($output, $path, $source);
}

Expand Down
18 changes: 18 additions & 0 deletions tests/BladeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,21 @@

expect(app(BladeService::class)->customConditions())->toContain('disk');
});

test('isClassBasedComponentView detects app convention class component', function () {
expect(app(BladeService::class)->isClassBasedComponentView(
fixture_path('views/components/alert.blade.php')
))->toBeTrue();
});

test('isClassBasedComponentView is false for anonymous components', function () {
expect(app(BladeService::class)->isClassBasedComponentView(
fixture_path('views/components/input.blade.php')
))->toBeFalse();
});

test('isClassBasedComponentView handles nested names', function () {
expect(app(BladeService::class)->isClassBasedComponentView(
fixture_path('views/components/dummy/foo.blade.php')
))->toBeFalse();
});
8 changes: 8 additions & 0 deletions tests/BlazeManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,11 @@

expect($manager->viewContainsExpiredFrontMatter($view))->toBeFalse();
});

test('class-based component renders when its directory is optimized', function () {
Blaze::optimize()->in(fixture_path('views/components'));

// Alert maps to App\View\Components\Alert in workbench.
expect(trim(Blade::render('<x-alert message="hello" />')))
->toBe('<div class="alert">hello</div>');
});
3 changes: 3 additions & 0 deletions tests/fixtures/views/components/alert.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@blaze

<div class="alert">{{ $message }}</div>
7 changes: 5 additions & 2 deletions workbench/app/View/Components/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace App\View\Components;

use Illuminate\View\Component;
use Illuminate\Contracts\View\View;

class Alert extends Component
{
public function render(): string
public function __construct(public string $message = '') {}

public function render(): View
{
return '<div class="alert"></div>';
return view('components.alert');
}
}
Loading