diff --git a/src/Compiler/Compiler.php b/src/Compiler/Compiler.php index f525116..2e97d6e 100644 --- a/src/Compiler/Compiler.php +++ b/src/Compiler/Compiler.php @@ -96,7 +96,7 @@ protected function compileComponentTag(ComponentNode $node, ComponentSource $sou $functionName = ($this->manager->isFolding() ? '__' : '_') . $hash; [$attributesArrayString, $boundKeysArrayString, $originalKeysArrayString] = $this->compileAttributes($node); - $output = '<' . '?php $__blaze->ensureRequired(\'' . $source->path . '\', $__blaze->compiledPath.\'/'. $hash . '.php\'); ?>' . "\n"; + $output = '<' . '?php $__blaze->ensureRequired(\'' . $source->path . '\', $__blaze->compiledPath.\'/'. $hash . '.php\', \'' . $functionName . '\'); ?>' . "\n"; if ($node->selfClosing) { $output .= '<' . '?php $__blaze->pushData(' . $attributesArrayString . '); ?>' . "\n"; diff --git a/src/Runtime/BlazeRuntime.php b/src/Runtime/BlazeRuntime.php index dd40220..81a0bd4 100644 --- a/src/Runtime/BlazeRuntime.php +++ b/src/Runtime/BlazeRuntime.php @@ -23,7 +23,6 @@ class BlazeRuntime protected ?string $compiledPath = null; protected array $paths = []; - protected array $required = []; protected array $blazed = []; protected array $dataStack = []; @@ -39,11 +38,11 @@ public function __construct( } /** - * Compile a component if its source is newer than the cached output. + * Compile and require a component if its function is not yet defined. */ - public function ensureRequired(string $path, string $compiledPath): void + public function ensureRequired(string $path, string $compiledPath, string $functionName): void { - if (isset($this->required[$compiledPath])) { + if (function_exists($functionName)) { return; } @@ -52,8 +51,6 @@ public function ensureRequired(string $path, string $compiledPath): void } require $compiledPath; - - $this->required[$compiledPath] = true; } /** @@ -77,10 +74,9 @@ public function resolve(string $component): string|false $hash = Utils::hash($path); $compiled = $this->getCompiledPath().'/'.$hash.'.php'; + $functionName = '_'.$hash; - if (! isset($this->required[$path])) { - $this->ensureRequired($path, $compiled); - } + $this->ensureRequired($path, $compiled, $functionName); return $hash; } diff --git a/tests/ComparisonTest.php b/tests/ComparisonTest.php index 165b09c..14fe16f 100644 --- a/tests/ComparisonTest.php +++ b/tests/ComparisonTest.php @@ -33,6 +33,11 @@ BLADE )); +test('aware nested', fn () => compare(<<<'BLADE' + + BLADE +)); + test('foldable aware', fn () => compare(<<<'BLADE' diff --git a/tests/Compiler/CompilerTest.php b/tests/Compiler/CompilerTest.php index 8d34096..e6f71ef 100644 --- a/tests/Compiler/CompilerTest.php +++ b/tests/Compiler/CompilerTest.php @@ -14,7 +14,7 @@ $hash = Utils::hash($path); expect($compiled->render())->toEqualCollapsingWhitespace(join('', [ - 'ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\'); ?> ', + 'ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\', \'_'. $hash .'\'); ?> ', 'pushData([\'type\' => \'text\',\'disabled\' => $disabled]); ?> ', ' \'text\',\'disabled\' => $disabled], [], [\'disabled\'], [], $__this ?? (isset($this) ? $this : null)); ?> ', 'popData(); ?>', @@ -42,7 +42,7 @@ $hash = Utils::hash($path); expect($compiled->render())->toEqualCollapsingWhitespace(join('', [ - 'ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\'); ?> ', + 'ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\', \'_'. $hash .'\'); ?> ', ' ', ' ', ' \'mt-8\']; ?> ', diff --git a/tests/Memoizer/MemoizerTest.php b/tests/Memoizer/MemoizerTest.php index 3fcdff5..9ac2d1c 100644 --- a/tests/Memoizer/MemoizerTest.php +++ b/tests/Memoizer/MemoizerTest.php @@ -22,7 +22,7 @@ '', '', '', - 'ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\'); ?> ', + 'ensureRequired(\''. $path .'\', $__blaze->compiledPath.\'/'. $hash .'.php\', \'_'. $hash .'\'); ?> ', 'pushData([\'src\' => $user->avatar]); ?> ', ' $user->avatar], [], [\'src\'], [], $__this ?? (isset($this) ? $this : null)); ?> ', 'popData(); ?>', diff --git a/tests/fixtures/views/components/nested-aware.blade.php b/tests/fixtures/views/components/nested-aware.blade.php new file mode 100644 index 0000000..674ece8 --- /dev/null +++ b/tests/fixtures/views/components/nested-aware.blade.php @@ -0,0 +1,3 @@ +@blaze + + \ No newline at end of file