[ Index ]

PHP Cross Reference of phpBB-3.3.11-deutsch

title

Body

[close]

/vendor/twig/twig/src/Node/Expression/Test/ -> DefinedTest.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of Twig.
   5   *
   6   * (c) Fabien Potencier
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Twig\Node\Expression\Test;
  13  
  14  use Twig\Compiler;
  15  use Twig\Error\SyntaxError;
  16  use Twig\Node\Expression\ArrayExpression;
  17  use Twig\Node\Expression\BlockReferenceExpression;
  18  use Twig\Node\Expression\ConstantExpression;
  19  use Twig\Node\Expression\FunctionExpression;
  20  use Twig\Node\Expression\GetAttrExpression;
  21  use Twig\Node\Expression\MethodCallExpression;
  22  use Twig\Node\Expression\NameExpression;
  23  use Twig\Node\Expression\TestExpression;
  24  use Twig\Node\Node;
  25  
  26  /**
  27   * Checks if a variable is defined in the current context.
  28   *
  29   *    {# defined works with variable names and variable attributes #}
  30   *    {% if foo is defined %}
  31   *        {# ... #}
  32   *    {% endif %}
  33   *
  34   * @author Fabien Potencier <fabien@symfony.com>
  35   */
  36  class DefinedTest extends TestExpression
  37  {
  38      public function __construct(Node $node, string $name, ?Node $arguments, int $lineno)
  39      {
  40          if ($node instanceof NameExpression) {
  41              $node->setAttribute('is_defined_test', true);
  42          } elseif ($node instanceof GetAttrExpression) {
  43              $node->setAttribute('is_defined_test', true);
  44              $this->changeIgnoreStrictCheck($node);
  45          } elseif ($node instanceof BlockReferenceExpression) {
  46              $node->setAttribute('is_defined_test', true);
  47          } elseif ($node instanceof FunctionExpression && 'constant' === $node->getAttribute('name')) {
  48              $node->setAttribute('is_defined_test', true);
  49          } elseif ($node instanceof ConstantExpression || $node instanceof ArrayExpression) {
  50              $node = new ConstantExpression(true, $node->getTemplateLine());
  51          } elseif ($node instanceof MethodCallExpression) {
  52              $node->setAttribute('is_defined_test', true);
  53          } else {
  54              throw new SyntaxError('The "defined" test only works with simple variables.', $lineno);
  55          }
  56  
  57          parent::__construct($node, $name, $arguments, $lineno);
  58      }
  59  
  60      private function changeIgnoreStrictCheck(GetAttrExpression $node)
  61      {
  62          $node->setAttribute('optimizable', false);
  63          $node->setAttribute('ignore_strict_check', true);
  64  
  65          if ($node->getNode('node') instanceof GetAttrExpression) {
  66              $this->changeIgnoreStrictCheck($node->getNode('node'));
  67          }
  68      }
  69  
  70      public function compile(Compiler $compiler)
  71      {
  72          $compiler->subcompile($this->getNode('node'));
  73      }
  74  }
  75  
  76  class_alias('Twig\Node\Expression\Test\DefinedTest', 'Twig_Node_Expression_Test_Defined');


Generated: Sat Nov 4 14:26:03 2023 Cross-referenced by PHPXref 0.7.1