| 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 |
namespace ElementorDeps\Twig\Node\Expression\Test; |
| 12 |
|
| 13 |
use ElementorDeps\Twig\Compiler; |
| 14 |
use ElementorDeps\Twig\Error\SyntaxError; |
| 15 |
use ElementorDeps\Twig\Node\Expression\ArrayExpression; |
| 16 |
use ElementorDeps\Twig\Node\Expression\BlockReferenceExpression; |
| 17 |
use ElementorDeps\Twig\Node\Expression\ConstantExpression; |
| 18 |
use ElementorDeps\Twig\Node\Expression\FunctionExpression; |
| 19 |
use ElementorDeps\Twig\Node\Expression\GetAttrExpression; |
| 20 |
use ElementorDeps\Twig\Node\Expression\MethodCallExpression; |
| 21 |
use ElementorDeps\Twig\Node\Expression\NameExpression; |
| 22 |
use ElementorDeps\Twig\Node\Expression\TestExpression; |
| 23 |
use ElementorDeps\Twig\Node\Node; |
| 24 |
/** |
| 25 |
* Checks if a variable is defined in the current context. |
| 26 |
* |
| 27 |
* {# defined works with variable names and variable attributes #} |
| 28 |
* {% if foo is defined %} |
| 29 |
* {# ... #} |
| 30 |
* {% endif %} |
| 31 |
* |
| 32 |
* @author Fabien Potencier <fabien@symfony.com> |
| 33 |
*/ |
| 34 |
class DefinedTest extends TestExpression |
| 35 |
{ |
| 36 |
public function __construct(Node $node, string $name, ?Node $arguments, int $lineno) |
| 37 |
{ |
| 38 |
if ($node instanceof NameExpression) { |
| 39 |
$node->setAttribute('is_defined_test', \true); |
| 40 |
} elseif ($node instanceof GetAttrExpression) { |
| 41 |
$node->setAttribute('is_defined_test', \true); |
| 42 |
$this->changeIgnoreStrictCheck($node); |
| 43 |
} elseif ($node instanceof BlockReferenceExpression) { |
| 44 |
$node->setAttribute('is_defined_test', \true); |
| 45 |
} elseif ($node instanceof FunctionExpression && 'constant' === $node->getAttribute('name')) { |
| 46 |
$node->setAttribute('is_defined_test', \true); |
| 47 |
} elseif ($node instanceof ConstantExpression || $node instanceof ArrayExpression) { |
| 48 |
$node = new ConstantExpression(\true, $node->getTemplateLine()); |
| 49 |
} elseif ($node instanceof MethodCallExpression) { |
| 50 |
$node->setAttribute('is_defined_test', \true); |
| 51 |
} else { |
| 52 |
throw new SyntaxError('The "defined" test only works with simple variables.', $lineno); |
| 53 |
} |
| 54 |
parent::__construct($node, $name, $arguments, $lineno); |
| 55 |
} |
| 56 |
private function changeIgnoreStrictCheck(GetAttrExpression $node) |
| 57 |
{ |
| 58 |
$node->setAttribute('optimizable', \false); |
| 59 |
$node->setAttribute('ignore_strict_check', \true); |
| 60 |
if ($node->getNode('node') instanceof GetAttrExpression) { |
| 61 |
$this->changeIgnoreStrictCheck($node->getNode('node')); |
| 62 |
} |
| 63 |
} |
| 64 |
public function compile(Compiler $compiler) : void |
| 65 |
{ |
| 66 |
$compiler->subcompile($this->getNode('node')); |
| 67 |
} |
| 68 |
} |
| 69 |
|