vendor/twig/twig/src/Node/ImportNode.php line 25

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Twig\Node;
  11. use Twig\Compiler;
  12. use Twig\Node\Expression\AbstractExpression;
  13. use Twig\Node\Expression\NameExpression;
  14. /**
  15. * Represents an import node.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class ImportNode extends Node
  20. {
  21. public function __construct(AbstractExpression $expr, AbstractExpression $var, int $lineno, string $tag = null, bool $global = true)
  22. {
  23. parent::__construct(['expr' => $expr, 'var' => $var], ['global' => $global], $lineno, $tag);
  24. }
  25. public function compile(Compiler $compiler)
  26. {
  27. $compiler
  28. ->addDebugInfo($this)
  29. ->write('$macros[')
  30. ->repr($this->getNode('var')->getAttribute('name'))
  31. ->raw('] = ')
  32. ;
  33. if ($this->getAttribute('global')) {
  34. $compiler
  35. ->raw('$this->macros[')
  36. ->repr($this->getNode('var')->getAttribute('name'))
  37. ->raw('] = ')
  38. ;
  39. }
  40. if ($this->getNode('expr') instanceof NameExpression && '_self' === $this->getNode('expr')->getAttribute('name')) {
  41. $compiler->raw('$this');
  42. } else {
  43. $compiler
  44. ->raw('$this->loadTemplate(')
  45. ->subcompile($this->getNode('expr'))
  46. ->raw(', ')
  47. ->repr($this->getTemplateName())
  48. ->raw(', ')
  49. ->repr($this->getTemplateLine())
  50. ->raw(')->unwrap()')
  51. ;
  52. }
  53. $compiler->raw(";\n");
  54. }
  55. }
  56. class_alias('Twig\Node\ImportNode', 'Twig_Node_Import');