Explorar el Código

implement block nesting support in PurePlate

michelphp hace 2 semanas
padre
commit
b6aaa68485
Se han modificado 3 ficheros con 17 adiciones y 16 borrados
  1. 6 6
      src/Engine.php
  2. 10 9
      src/PhpRenderer.php
  3. 1 1
      tests/PurePlateTest.php

+ 6 - 6
src/Engine.php

@@ -65,14 +65,14 @@ final class Engine
                 $this->handleError($e, $compiledCode, $templatePath);
             }
         }
+        set_error_handler(function ($severity, $message, $file, $line) {
+            if (!(error_reporting() & $severity)) {
+                return;
+            }
+            throw new ErrorException($message, 0, $severity, $file, $line);
+        });
 
         try {
-            set_error_handler(function ($severity, $message, $file, $line) {
-                if (!(error_reporting() & $severity)) {
-                    return;
-                }
-                throw new ErrorException($message, 0, $severity, $file, $line);
-            });
             return $this->renderer->render(str_replace($this->cacheDir, '', realpath($cacheFile)), $context);
         } catch (Throwable $e) {
             $this->handleError($e, file_get_contents($cacheFile), $templatePath);

+ 10 - 9
src/PhpRenderer.php

@@ -19,7 +19,7 @@ final class PhpRenderer
     private string $templateDir;
     private array $globals;
     private array $blocks = [];
-    private ?string $currentBlock = null;
+    private array $blockStack = [];
     private ?string $layout = null;
 
     public function __construct(
@@ -65,12 +65,8 @@ final class PhpRenderer
 
     public function startBlock(string $name): void
     {
-        if ($this->currentBlock !== null) {
-            throw new RuntimeException("A block is already started. Call endBlock() before starting a new block.");
-        }
-
         $content = $this->block($name);
-        $this->currentBlock = $name;
+        $this->blockStack[] = $name;
         if (!empty($content)) {
             echo $content;
         }
@@ -79,11 +75,16 @@ final class PhpRenderer
 
     public function endBlock(): void
     {
-        if ($this->currentBlock === null) {
+        if (empty($this->blockStack)) {
             throw new RuntimeException("No block started. Call startBlock() before calling endBlock().");
         }
-        $this->blocks[$this->currentBlock] = trim(ob_get_clean());
-        $this->currentBlock = null;
+
+        $content = trim(ob_get_clean());
+        $name = array_pop($this->blockStack);
+        $this->blocks[$name] = $content;
+        if (!empty($this->blockStack)) {
+            echo $content;
+        }
     }
 
     public function block(string $name): string

+ 1 - 1
tests/PurePlateTest.php

@@ -102,7 +102,7 @@ final class PurePlateTest extends TestCase
         } catch (ErrorException $e) {
             // Vérification du mapping de ligne magique /*L:3;F:...*/
             $this->assertEquals(3, $e->getLine());
-            $this->assertStringContains($e->getFile(), 'error.html');
+            $this->assertStringContains($e->getMessage(), 'error.html');
         }
     }