You are here

protected function Twig_NodeVisitor_SafeAnalysis::doLeaveNode in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/twig/twig/lib/Twig/NodeVisitor/SafeAnalysis.php \Twig_NodeVisitor_SafeAnalysis::doLeaveNode()

Called after child nodes are visited.

Parameters

Twig_Node $node The node to visit:

Twig_Environment $env The Twig environment instance:

Return value

Twig_Node|false The modified node or false if the node must be removed

Overrides Twig_BaseNodeVisitor::doLeaveNode

File

vendor/twig/twig/lib/Twig/NodeVisitor/SafeAnalysis.php, line 71

Class

Twig_NodeVisitor_SafeAnalysis

Code

protected function doLeaveNode(Twig_Node $node, Twig_Environment $env) {
  if ($node instanceof Twig_Node_Expression_Constant) {

    // constants are marked safe for all
    $this
      ->setSafe($node, array(
      'all',
    ));
  }
  elseif ($node instanceof Twig_Node_Expression_BlockReference) {

    // blocks are safe by definition
    $this
      ->setSafe($node, array(
      'all',
    ));
  }
  elseif ($node instanceof Twig_Node_Expression_Parent) {

    // parent block is safe by definition
    $this
      ->setSafe($node, array(
      'all',
    ));
  }
  elseif ($node instanceof Twig_Node_Expression_Conditional) {

    // intersect safeness of both operands
    $safe = $this
      ->intersectSafe($this
      ->getSafe($node
      ->getNode('expr2')), $this
      ->getSafe($node
      ->getNode('expr3')));
    $this
      ->setSafe($node, $safe);
  }
  elseif ($node instanceof Twig_Node_Expression_Filter) {

    // filter expression is safe when the filter is safe
    $name = $node
      ->getNode('filter')
      ->getAttribute('value');
    $args = $node
      ->getNode('arguments');
    if (false !== ($filter = $env
      ->getFilter($name))) {
      $safe = $filter
        ->getSafe($args);
      if (null === $safe) {
        $safe = $this
          ->intersectSafe($this
          ->getSafe($node
          ->getNode('node')), $filter
          ->getPreservesSafety());
      }
      $this
        ->setSafe($node, $safe);
    }
    else {
      $this
        ->setSafe($node, array());
    }
  }
  elseif ($node instanceof Twig_Node_Expression_Function) {

    // function expression is safe when the function is safe
    $name = $node
      ->getAttribute('name');
    $args = $node
      ->getNode('arguments');
    $function = $env
      ->getFunction($name);
    if (false !== $function) {
      $this
        ->setSafe($node, $function
        ->getSafe($args));
    }
    else {
      $this
        ->setSafe($node, array());
    }
  }
  elseif ($node instanceof Twig_Node_Expression_MethodCall) {
    if ($node
      ->getAttribute('safe')) {
      $this
        ->setSafe($node, array(
        'all',
      ));
    }
    else {
      $this
        ->setSafe($node, array());
    }
  }
  elseif ($node instanceof Twig_Node_Expression_GetAttr && $node
    ->getNode('node') instanceof Twig_Node_Expression_Name) {
    $name = $node
      ->getNode('node')
      ->getAttribute('name');

    // attributes on template instances are safe
    if ('_self' == $name || in_array($name, $this->safeVars)) {
      $this
        ->setSafe($node, array(
        'all',
      ));
    }
    else {
      $this
        ->setSafe($node, array());
    }
  }
  else {
    $this
      ->setSafe($node, array());
  }
  return $node;
}