Skip to content

Commit d31efff

Browse files
ganyiczclaude
authored andcommitted
Use function_exists() guard inside ensureRequired
Move the function_exists() check into ensureRequired() so both the compiled caller and resolve() benefit from the C-level guard. Drop the $required hash table which is now redundant. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 4b96d2b commit d31efff

9 files changed

Lines changed: 17 additions & 26 deletions

File tree

src/Compiler/Compiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected function compileComponentTag(ComponentNode $node, ComponentSource $sou
9696
$functionName = ($this->manager->isFolding() ? '__' : '_') . $hash;
9797
[$attributesArrayString, $boundKeysArrayString, $originalKeysArrayString] = $this->compileAttributes($node);
9898

99-
$output = '<' . '?php if (!function_exists(\'' . $functionName . '\')) { $__blaze->ensureRequired(\'' . $source->path . '\', $__blaze->compiledPath.\'/'. $hash . '.php\'); } ?>' . "\n";
99+
$output = '<' . '?php $__blaze->ensureRequired(\'' . $source->path . '\', $__blaze->compiledPath.\'/'. $hash . '.php\', \'' . $functionName . '\'); ?>' . "\n";
100100

101101
if ($node->selfClosing) {
102102
$output .= '<' . '?php $__blaze->pushData(' . $attributesArrayString . '); ?>' . "\n";

src/Runtime/BlazeRuntime.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class BlazeRuntime
2323
protected ?string $compiledPath = null;
2424

2525
protected array $paths = [];
26-
protected array $required = [];
2726
protected array $blazed = [];
2827

2928
protected array $dataStack = [];
@@ -39,11 +38,11 @@ public function __construct(
3938
}
4039

4140
/**
42-
* Compile a component if its source is newer than the cached output.
41+
* Compile and require a component if its function is not yet defined.
4342
*/
44-
public function ensureRequired(string $path, string $compiledPath): void
43+
public function ensureRequired(string $path, string $compiledPath, string $functionName): void
4544
{
46-
if (isset($this->required[$compiledPath])) {
45+
if (function_exists($functionName)) {
4746
return;
4847
}
4948

@@ -52,8 +51,6 @@ public function ensureRequired(string $path, string $compiledPath): void
5251
}
5352

5453
require $compiledPath;
55-
56-
$this->required[$compiledPath] = true;
5754
}
5855

5956
/**
@@ -77,10 +74,9 @@ public function resolve(string $component): string|false
7774

7875
$hash = Utils::hash($path);
7976
$compiled = $this->getCompiledPath().'/'.$hash.'.php';
77+
$functionName = '_'.$hash;
8078

81-
if (! isset($this->required[$path])) {
82-
$this->ensureRequired($path, $compiled);
83-
}
79+
$this->ensureRequired($path, $compiled, $functionName);
8480

8581
return $hash;
8682
}

tests/ComparisonTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
BLADE
3434
));
3535

36+
test('aware nested', fn () => compare(<<<'BLADE'
37+
<x-nested-aware type="number" />
38+
BLADE
39+
));
40+
3641
test('foldable aware', fn () => compare(<<<'BLADE'
3742
<x-foldable.wrapper type="number">
3843
<x-foldable.input-aware />

tests/Compiler/CompilerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
$hash = Utils::hash($path);
1515

1616
expect($compiled->render())->toEqualCollapsingWhitespace(join('', [
17-
'<?php if (!function_exists(\'_'. $hash .'\')) { $__blaze->ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\'); } ?> ',
17+
'<?php $__blaze->ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\', \'_'. $hash .'\'); ?> ',
1818
'<?php $__blaze->pushData([\'type\' => \'text\',\'disabled\' => $disabled]); ?> ',
1919
'<?php _'. $hash .'($__blaze, [\'type\' => \'text\',\'disabled\' => $disabled], [], [\'disabled\'], [], $__this ?? (isset($this) ? $this : null)); ?> ',
2020
'<?php $__blaze->popData(); ?>',
@@ -42,7 +42,7 @@
4242
$hash = Utils::hash($path);
4343

4444
expect($compiled->render())->toEqualCollapsingWhitespace(join('', [
45-
'<?php if (!function_exists(\'_'. $hash .'\')) { $__blaze->ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\'); } ?> ',
45+
'<?php $__blaze->ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\', \'_'. $hash .'\'); ?> ',
4646
'<?php if (isset($__slots'. $hash .')) { $__slotsStack'. $hash .'[] = $__slots'. $hash .'; } ?> ',
4747
'<?php if (isset($__attrs'. $hash .')) { $__attrsStack'. $hash .'[] = $__attrs'. $hash .'; } ?> ',
4848
'<?php $__attrs'. $hash .' = [\'class\' => \'mt-8\']; ?> ',

tests/IntegrationTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ public function render()
8585
})->assertSee('from-livewire');
8686
});
8787

88-
test('self-closing component propagates data to @aware descendants', function () {
89-
$html = Blade::render('<x-aware-wrapper color="blue" />');
90-
91-
expect($html)->toContain('text-blue');
92-
});
93-
9488
test('folds and compiles the same component', function () {
9589
Blade::render(<<<'BLADE'
9690
<x-foldable.input required /> {{-- Folded --}}

tests/Memoizer/MemoizerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'<?php echo \Livewire\Blaze\Memoizer\Memo::get($blaze_memoized_key); ?>',
2323
'<?php else : ?>',
2424
'<?php ob_start(); ?>',
25-
'<?php if (!function_exists(\'_'. $hash .'\')) { $__blaze->ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\'); } ?> ',
25+
'<?php $__blaze->ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\', \'_'. $hash .'\'); ?> ',
2626
'<?php $__blaze->pushData([\'src\' => $user->avatar]); ?> ',
2727
'<?php _'. $hash .'($__blaze, [\'src\' => $user->avatar], [], [\'src\'], [], $__this ?? (isset($this) ? $this : null)); ?> ',
2828
'<?php $__blaze->popData(); ?>',

tests/fixtures/views/components/aware-child.blade.php

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/fixtures/views/components/aware-wrapper.blade.php

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@blaze
2+
3+
<x-input-aware />

0 commit comments

Comments
 (0)