PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Twig/Node/Expression/Test/Defined.php

http://github.com/fabpot/Twig
PHP | 56 lines | 29 code | 7 blank | 20 comment | 4 complexity | 3b2c03077dd4cc9fba7c793998e24f32 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2011 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. /**
  11. * Checks if a variable is defined in the current context.
  12. *
  13. * <pre>
  14. * {# defined works with variable names and variable attributes #}
  15. * {% if foo is defined %}
  16. * {# ... #}
  17. * {% endif %}
  18. * </pre>
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class Twig_Node_Expression_Test_Defined extends Twig_Node_Expression_Test
  23. {
  24. public function __construct(Twig_Node $node, $name, Twig_Node $arguments = null, $lineno)
  25. {
  26. if ($node instanceof Twig_Node_Expression_Name) {
  27. $node->setAttribute('is_defined_test', true);
  28. } elseif ($node instanceof Twig_Node_Expression_GetAttr) {
  29. $node->setAttribute('is_defined_test', true);
  30. $this->changeIgnoreStrictCheck($node);
  31. } elseif ($node instanceof Twig_Node_Expression_Constant || $node instanceof Twig_Node_Expression_Array) {
  32. $node = new Twig_Node_Expression_Constant(true, $node->getTemplateLine());
  33. } else {
  34. throw new Twig_Error_Syntax('The "defined" test only works with simple variables.', $this->getTemplateLine());
  35. }
  36. parent::__construct($node, $name, $arguments, $lineno);
  37. }
  38. private function changeIgnoreStrictCheck(Twig_Node_Expression_GetAttr $node)
  39. {
  40. $node->setAttribute('ignore_strict_check', true);
  41. if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
  42. $this->changeIgnoreStrictCheck($node->getNode('node'));
  43. }
  44. }
  45. public function compile(Twig_Compiler $compiler)
  46. {
  47. $compiler->subcompile($this->getNode('node'));
  48. }
  49. }