Name.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. class Twig_Node_Expression_Name extends Twig_Node_Expression
  12. {
  13. public function __construct($name, $lineno)
  14. {
  15. parent::__construct(array(), array('name' => $name), $lineno);
  16. }
  17. public function compile(Twig_Compiler $compiler)
  18. {
  19. static $specialVars = array(
  20. '_self' => '$this',
  21. '_context' => '$context',
  22. '_charset' => '$this->env->getCharset()',
  23. );
  24. $name = $this->getAttribute('name');
  25. if ($this->hasAttribute('is_defined_test')) {
  26. if (isset($specialVars[$name])) {
  27. $compiler->repr(true);
  28. } else {
  29. $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)');
  30. }
  31. } elseif (isset($specialVars[$name])) {
  32. $compiler->raw($specialVars[$name]);
  33. } else {
  34. $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $name));
  35. }
  36. }
  37. }