Advertisement
BackuPs-nl

twig v 1.33 node.php php 7.4 fix

Feb 26th, 2020
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.94 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * This file is part of Twig.
  5.  *
  6.  * (c) Fabien Potencier
  7.  * (c) Armin Ronacher
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12.  
  13. /**
  14.  * Represents a node in the AST.
  15.  *
  16.  * @author Fabien Potencier <fabien@symfony.com>
  17.  */
  18. class Twig_Node implements Twig_NodeInterface
  19. {
  20.     protected $nodes;
  21.     protected $attributes;
  22.     protected $lineno;
  23.     protected $tag;
  24.  
  25.     private $name;
  26.  
  27.     /**
  28.      * Constructor.
  29.      *
  30.      * The nodes are automatically made available as properties ($this->node).
  31.      * The attributes are automatically made available as array items ($this['name']).
  32.      *
  33.      * @param array  $nodes      An array of named nodes
  34.      * @param array  $attributes An array of attributes (should not be nodes)
  35.      * @param int    $lineno     The line number
  36.      * @param string $tag        The tag name associated with the Node
  37.      */
  38.     public function __construct(array $nodes = array(), array $attributes = array(), $lineno = 0, $tag = null)
  39.     {
  40.         foreach ($nodes as $name => $node) {
  41.             if (!$node instanceof Twig_NodeInterface) {
  42.                 @trigger_error(sprintf('Using "%s" for the value of node "%s" of "%s" is deprecated since version 1.25 and will be removed in 2.0.', is_object($node) ? get_class($node) : (null === $node ? 'null' : gettype($node)), $name, get_class($this)), E_USER_DEPRECATED);
  43.             }
  44.         }
  45.         $this->nodes = $nodes;
  46.         $this->attributes = $attributes;
  47.         $this->lineno = $lineno;
  48.         $this->tag = $tag;
  49.     }
  50.  
  51.     public function __toString()
  52.     {
  53.         $attributes = array();
  54.         foreach ($this->attributes as $name => $value) {
  55.             $attributes[] = sprintf('%s: %s', $name, str_replace("\n", '', var_export($value, true)));
  56.         }
  57.  
  58.         $repr = array(get_class($this).'('.implode(', ', $attributes));
  59.  
  60.         if (count($this->nodes)) {
  61.             foreach ($this->nodes as $name => $node) {
  62.                 $len = strlen($name) + 4;
  63.                 $noderepr = array();
  64.                 foreach (explode("\n", (string) $node) as $line) {
  65.                     $noderepr[] = str_repeat(' ', $len).$line;
  66.                 }
  67.  
  68.                 $repr[] = sprintf('  %s: %s', $name, ltrim(implode("\n", $noderepr)));
  69.             }
  70.  
  71.             $repr[] = ')';
  72.         } else {
  73.             $repr[0] .= ')';
  74.         }
  75.  
  76.         return implode("\n", $repr);
  77.     }
  78.  
  79.     /**
  80.      * @deprecated since 1.16.1 (to be removed in 2.0)
  81.      */
  82.     public function toXml($asDom = false)
  83.     {
  84.         @trigger_error(sprintf('%s is deprecated since version 1.16.1 and will be removed in 2.0.', __METHOD__), E_USER_DEPRECATED);
  85.  
  86.         $dom = new DOMDocument('1.0', 'UTF-8');
  87.         $dom->formatOutput = true;
  88.         $dom->appendChild($xml = $dom->createElement('twig'));
  89.  
  90.         $xml->appendChild($node = $dom->createElement('node'));
  91.         $node->setAttribute('class', get_class($this));
  92.  
  93.         foreach ($this->attributes as $name => $value) {
  94.             $node->appendChild($attribute = $dom->createElement('attribute'));
  95.             $attribute->setAttribute('name', $name);
  96.             $attribute->appendChild($dom->createTextNode($value));
  97.         }
  98.  
  99.         foreach ($this->nodes as $name => $n) {
  100.             if (null === $n) {
  101.                 continue;
  102.             }
  103.  
  104.             $child = $n->toXml(true)->getElementsByTagName('node')->item(0);
  105.             $child = $dom->importNode($child, true);
  106.             $child->setAttribute('name', $name);
  107.  
  108.             $node->appendChild($child);
  109.         }
  110.  
  111.         return $asDom ? $dom : $dom->saveXML();
  112.     }
  113.  
  114.     public function compile(Twig_Compiler $compiler)
  115.     {
  116.         foreach ($this->nodes as $node) {
  117.             $node->compile($compiler);
  118.         }
  119.     }
  120.  
  121.     public function getTemplateLine()
  122.     {
  123.         return $this->lineno;
  124.     }
  125.  
  126.     /**
  127.      * @deprecated since 1.27 (to be removed in 2.0)
  128.      */
  129.     public function getLine()
  130.     {
  131.         @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getTemplateLine() instead.', E_USER_DEPRECATED);
  132.  
  133.         return $this->lineno;
  134.     }
  135.  
  136.     public function getNodeTag()
  137.     {
  138.         return $this->tag;
  139.     }
  140.  
  141.     /**
  142.      * @return bool
  143.      */
  144.     public function hasAttribute($name)
  145.     {
  146.         return array_key_exists($name, $this->attributes);
  147.     }
  148.  
  149.     /**
  150.      * @return mixed
  151.      */
  152.     public function getAttribute($name)
  153.     {
  154.         if (!array_key_exists($name, $this->attributes)) {
  155.             throw new LogicException(sprintf('Attribute "%s" does not exist for Node "%s".', $name, get_class($this)));
  156.         }
  157.  
  158.         return $this->attributes[$name];
  159.     }
  160.  
  161.     /**
  162.      * @param string $name
  163.      * @param mixed  $value
  164.      */
  165.     public function setAttribute($name, $value)
  166.     {
  167.         $this->attributes[$name] = $value;
  168.     }
  169.  
  170.     public function removeAttribute($name)
  171.     {
  172.         unset($this->attributes[$name]);
  173.     }
  174.  
  175.     /**
  176.      * @return bool
  177.      */
  178.     public function hasNode($name)
  179.     {
  180.         return array_key_exists($name, $this->nodes);
  181.     }
  182.  
  183.     /**
  184.      * @return Twig_Node
  185.      */
  186.     public function getNode($name)
  187.     {
  188.         if (!array_key_exists($name, $this->nodes)) {
  189.             throw new LogicException(sprintf('Node "%s" does not exist for Node "%s".', $name, get_class($this)));
  190.         }
  191.  
  192.         return $this->nodes[$name];
  193.     }
  194.  
  195.     public function setNode($name, $node = null)
  196.     {
  197.         if (!$node instanceof Twig_NodeInterface) {
  198.             @trigger_error(sprintf('Using "%s" for the value of node "%s" of "%s" is deprecated since version 1.25 and will be removed in 2.0.', is_object($node) ? get_class($node) : (null === $node ? 'null' : gettype($node)), $name, get_class($this)), E_USER_DEPRECATED);
  199.         }
  200.  
  201.         $this->nodes[$name] = $node;
  202.     }
  203.  
  204.     public function removeNode($name)
  205.     {
  206.         unset($this->nodes[$name]);
  207.     }
  208.  
  209.     public function count()
  210.     {
  211.         return count($this->nodes);
  212.     }
  213.  
  214.     public function getIterator()
  215.     {
  216.         return new ArrayIterator($this->nodes);
  217.     }
  218.  
  219.     public function setTemplateName($name)
  220.     {
  221.         $this->name = $name;
  222.         foreach ($this->nodes as $node) {
  223.             if (null !== $node) {
  224.                 $node->setTemplateName($name);
  225.             }
  226.         }
  227.     }
  228.  
  229.     public function getTemplateName()
  230.     {
  231.         return $this->name;
  232.     }
  233.  
  234.     /**
  235.      * @deprecated since 1.27 (to be removed in 2.0)
  236.      */
  237.     public function setFilename($name)
  238.     {
  239.         @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use setTemplateName() instead.', E_USER_DEPRECATED);
  240.  
  241.         $this->setTemplateName($name);
  242.     }
  243.  
  244.     /**
  245.      * @deprecated since 1.27 (to be removed in 2.0)
  246.      */
  247.     public function getFilename()
  248.     {
  249.         @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getTemplateName() instead.', E_USER_DEPRECATED);
  250.  
  251.         return $this->name;
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement