DeferredBlockNode.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. /**
  3. * This file is part of the rybakit/twig-deferred-extension package.
  4. *
  5. * (c) Eugene Leonovich <gen.work@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Twig\DeferredExtension;
  12. use Twig\Compiler;
  13. use Twig\Node\BlockNode;
  14. final class DeferredBlockNode extends BlockNode
  15. {
  16. public function compile(Compiler $compiler) : void
  17. {
  18. $name = $this->getAttribute('name');
  19. $compiler
  20. ->write("public function block_$name(\$context, array \$blocks = [])\n", "{\n")
  21. ->indent()
  22. ->write("\$this->deferred->defer(\$this, '$name');\n")
  23. ->outdent()
  24. ->write("}\n\n")
  25. ;
  26. $compiler
  27. ->addDebugInfo($this)
  28. ->write("public function block_{$name}_deferred(\$context, array \$blocks = [])\n", "{\n")
  29. ->indent()
  30. ->subcompile($this->getNode('body'))
  31. ->write("\$this->deferred->resolve(\$this, \$context, \$blocks);\n")
  32. ->outdent()
  33. ->write("}\n\n")
  34. ;
  35. }
  36. }