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
12 changes: 9 additions & 3 deletions src/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,16 @@ protected function compileDelegateComponentTag(ComponentNode $node): string
$componentName = "'flux::' . " . $attributesArray['component']->value;

$output = '<' . '?php $__resolved = $__blaze->resolve(' . $componentName . '); ?>' . "\n";
$output .= '<' . '?php require_once $__blaze->compiledPath . \'/\' . $__resolved . \'.php\'; ?>' . "\n";

$output .= '<' . '?php $__blaze->pushData($attributes->all()); ?>';

$slotsVariableName = '$slots' . hash('xxh128', $componentName);

$functionName = '(\'' . (Blaze::isFolding() ? '__' : '_') . '\' . $__resolved)';

$output .= '<' . '?php $__blaze->pushData($attributes->all()); ?>';

$output .= "\n" . '<' . '?php if ($__resolved !== false): ?>';
$output .= "\n" . '<' . '?php require_once $__blaze->compiledPath . \'/\' . $__resolved . \'.php\'; ?>';

if ($node->selfClosing) {
$output .= "\n" . '<' . '?php ' . $functionName . '($__blaze, $attributes->all(), $__blaze->mergedComponentSlots(), [], isset($this) ? $this : null); ?>';
} else {
Expand All @@ -143,6 +145,10 @@ protected function compileDelegateComponentTag(ComponentNode $node): string
$output .= "\n" . '<' . '?php ' . $functionName . '($__blaze, $attributes->all(), ' . $slotsVariableName . ', [], isset($this) ? $this : null); ?>';
}

$output .= "\n" . '<' . '?php else: ?>';
$output .= "\n" . $node->render();
$output .= "\n" . '<' . '?php endif; ?>';

$output .= "\n" . '<' . '?php $__blaze->popData(); ?>';
$output .= "\n" . '<' . '?php unset($__resolved) ?>' . "\n";

Expand Down
38 changes: 37 additions & 1 deletion src/Runtime/BlazeRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Livewire\Blaze\Support\Utils;
use Livewire\Blaze\Debugger;
use Illuminate\View\Compilers\Compiler;
use Livewire\Blaze\Support\Directives;

/**
* Runtime context shared with all Blaze-compiled components via $__blaze.
Expand All @@ -25,6 +26,7 @@ class BlazeRuntime

protected array $paths = [];
protected array $compiled = [];
protected array $blazed = [];

protected array $dataStack = [];
protected array $slotsStack = [];
Expand Down Expand Up @@ -58,15 +60,23 @@ public function ensureCompiled(string $path, string $compiledPath): void

/**
* Resolve a component name to its compiled hash, compiling if needed.
*
* Returns false when the component exists but is not Blaze-eligible
* (no @blaze directive and not configured for compilation), so the
* caller can fall back to standard Blade rendering.
*/
public function resolve(string $component): string
public function resolve(string $component): string|false
{
if (isset($this->paths[$component])) {
$path = $this->paths[$component];
} else {
$path = $this->paths[$component] = BladeService::componentNameToPath($component);
}

if (! $this->isBlazeComponent($path)) {
return false;
}

$hash = Utils::hash($path);
$compiled = $this->compiledPath.'/'.$hash.'.php';

Expand All @@ -77,6 +87,32 @@ public function resolve(string $component): string
return $hash;
}

/**
* Check if a component file is a Blaze component.
*/
protected function isBlazeComponent(string $path): bool
{
if (isset($this->blazed[$path])) {
return $this->blazed[$path];
}

if (! file_exists($path)) {
return $this->blazed[$path] = false;
}

$directives = new Directives(file_get_contents($path));

if ($directives->blaze()) {
return $this->blazed[$path] = true;
}

$config = app('blaze.config');

return $this->blazed[$path] = $config->shouldCompile($path)
|| $config->shouldMemoize($path)
|| $config->shouldFold($path);
}

/**
* Get merged data from all stack levels for delegate forwarding.
*/
Expand Down
6 changes: 5 additions & 1 deletion tests/Compiler/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@

expect($compiled->render())->toEqualCollapsingWhitespace(join('', [
'<?php $__resolved = $__blaze->resolve(\'flux::\' . card); ?> ',
'<?php require_once $__blaze->compiledPath . \'/\' . $__resolved . \'.php\'; ?> ',
'<?php $__blaze->pushData($attributes->all()); ?> ',
'<?php if ($__resolved !== false): ?> ',
'<?php require_once $__blaze->compiledPath . \'/\' . $__resolved . \'.php\'; ?> ',
'<?php (\'_\' . $__resolved)($__blaze, $attributes->all(), $__blaze->mergedComponentSlots(), [], isset($this) ? $this : null); ?> ',
'<?php else: ?> ',
'<flux:delegate-component component="card" /> ',
'<?php endif; ?> ',
'<?php $__blaze->popData(); ?> ',
'<?php unset($__resolved) ?> ',
]));
Expand Down