Name.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. protected $specialVars = array(
  14. '_self' => '$this',
  15. '_context' => '$context',
  16. '_charset' => '$this->env->getCharset()',
  17. );
  18. public function __construct($name, $lineno)
  19. {
  20. parent::__construct(array(), array('name' => $name, 'is_defined_test' => false, 'ignore_strict_check' => false), $lineno);
  21. }
  22. public function compile(Twig_Compiler $compiler)
  23. {
  24. $name = $this->getAttribute('name');
  25. if ($this->getAttribute('is_defined_test')) {
  26. if ($this->isSpecial()) {
  27. $compiler->repr(true);
  28. } else {
  29. $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)');
  30. }
  31. } elseif ($this->isSpecial()) {
  32. $compiler->raw($this->specialVars[$name]);
  33. } else {
  34. if (version_compare(phpversion(), '5.4.0RC1', '>=') && ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables())) {
  35. // PHP 5.4 ternary operator performance was optimized
  36. $compiler
  37. ->raw('(isset($context[')
  38. ->string($name)
  39. ->raw(']) ? $context[')
  40. ->string($name)
  41. ->raw('] : null)')
  42. ;
  43. } else {
  44. $compiler
  45. ->raw('$this->getContext($context, ')
  46. ->string($name)
  47. ;
  48. if ($this->getAttribute('ignore_strict_check')) {
  49. $compiler->raw(', true');
  50. }
  51. $compiler
  52. ->raw(')')
  53. ;
  54. }
  55. }
  56. }
  57. public function isSpecial()
  58. {
  59. return isset($this->specialVars[$this->getAttribute('name')]);
  60. }
  61. public function isSimple()
  62. {
  63. return !$this->isSpecial() && !$this->getAttribute('is_defined_test');
  64. }
  65. }