PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/community/vendor/twig/twig/lib/Twig/Parser.php

https://bitbucket.org/ke2083/transfans.co.uk-website
PHP | 412 lines | 293 code | 76 blank | 43 comment | 36 complexity | 2ba4e3ac3ae0ad5757c30672aa4a4c7e MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. * (c) 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. /**
  12. * Default parser implementation.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Parser implements Twig_ParserInterface
  17. {
  18. protected $stack = array();
  19. protected $stream;
  20. protected $parent;
  21. protected $handlers;
  22. protected $visitors;
  23. protected $expressionParser;
  24. protected $blocks;
  25. protected $blockStack;
  26. protected $macros;
  27. protected $env;
  28. protected $reservedMacroNames;
  29. protected $importedSymbols;
  30. protected $traits;
  31. protected $embeddedTemplates = array();
  32. private $varNameSalt = 0;
  33. public function __construct(Twig_Environment $env)
  34. {
  35. $this->env = $env;
  36. }
  37. /**
  38. * @deprecated since 1.27 (to be removed in 2.0)
  39. */
  40. public function getEnvironment()
  41. {
  42. @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0.', E_USER_DEPRECATED);
  43. return $this->env;
  44. }
  45. public function getVarName()
  46. {
  47. return sprintf('__internal_%s', hash('sha256', __METHOD__.$this->stream->getSourceContext()->getCode().$this->varNameSalt++));
  48. }
  49. /**
  50. * @deprecated since 1.27 (to be removed in 2.0). Use $parser->getStream()->getSourceContext()->getPath() instead.
  51. */
  52. public function getFilename()
  53. {
  54. @trigger_error(sprintf('The "%s" method is deprecated since version 1.27 and will be removed in 2.0. Use $parser->getStream()->getSourceContext()->getPath() instead.', __METHOD__), E_USER_DEPRECATED);
  55. return $this->stream->getSourceContext()->getName();
  56. }
  57. public function parse(Twig_TokenStream $stream, $test = null, $dropNeedle = false)
  58. {
  59. // push all variables into the stack to keep the current state of the parser
  60. // using get_object_vars() instead of foreach would lead to https://bugs.php.net/71336
  61. // This hack can be removed when min version if PHP 7.0
  62. $vars = array();
  63. foreach ($this as $k => $v) {
  64. $vars[$k] = $v;
  65. }
  66. unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames']);
  67. $this->stack[] = $vars;
  68. // tag handlers
  69. if (null === $this->handlers) {
  70. $this->handlers = $this->env->getTokenParsers();
  71. $this->handlers->setParser($this);
  72. }
  73. // node visitors
  74. if (null === $this->visitors) {
  75. $this->visitors = $this->env->getNodeVisitors();
  76. }
  77. if (null === $this->expressionParser) {
  78. $this->expressionParser = new Twig_ExpressionParser($this, $this->env);
  79. }
  80. $this->stream = $stream;
  81. $this->parent = null;
  82. $this->blocks = array();
  83. $this->macros = array();
  84. $this->traits = array();
  85. $this->blockStack = array();
  86. $this->importedSymbols = array(array());
  87. $this->embeddedTemplates = array();
  88. $this->varNameSalt = 0;
  89. try {
  90. $body = $this->subparse($test, $dropNeedle);
  91. if (null !== $this->parent && null === $body = $this->filterBodyNodes($body)) {
  92. $body = new Twig_Node();
  93. }
  94. } catch (Twig_Error_Syntax $e) {
  95. if (!$e->getSourceContext()) {
  96. $e->setSourceContext($this->stream->getSourceContext());
  97. }
  98. if (!$e->getTemplateLine()) {
  99. $e->setTemplateLine($this->stream->getCurrent()->getLine());
  100. }
  101. throw $e;
  102. }
  103. $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->embeddedTemplates, $stream->getSourceContext());
  104. $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
  105. $node = $traverser->traverse($node);
  106. // restore previous stack so previous parse() call can resume working
  107. foreach (array_pop($this->stack) as $key => $val) {
  108. $this->$key = $val;
  109. }
  110. return $node;
  111. }
  112. public function subparse($test, $dropNeedle = false)
  113. {
  114. $lineno = $this->getCurrentToken()->getLine();
  115. $rv = array();
  116. while (!$this->stream->isEOF()) {
  117. switch ($this->getCurrentToken()->getType()) {
  118. case Twig_Token::TEXT_TYPE:
  119. $token = $this->stream->next();
  120. $rv[] = new Twig_Node_Text($token->getValue(), $token->getLine());
  121. break;
  122. case Twig_Token::VAR_START_TYPE:
  123. $token = $this->stream->next();
  124. $expr = $this->expressionParser->parseExpression();
  125. $this->stream->expect(Twig_Token::VAR_END_TYPE);
  126. $rv[] = new Twig_Node_Print($expr, $token->getLine());
  127. break;
  128. case Twig_Token::BLOCK_START_TYPE:
  129. $this->stream->next();
  130. $token = $this->getCurrentToken();
  131. if (Twig_Token::NAME_TYPE !== $token->getType()) {
  132. throw new Twig_Error_Syntax('A block must start with a tag name.', $token->getLine(), $this->stream->getSourceContext());
  133. }
  134. if (null !== $test && call_user_func($test, $token)) {
  135. if ($dropNeedle) {
  136. $this->stream->next();
  137. }
  138. if (1 === count($rv)) {
  139. return $rv[0];
  140. }
  141. return new Twig_Node($rv, array(), $lineno);
  142. }
  143. $subparser = $this->handlers->getTokenParser($token->getValue());
  144. if (null === $subparser) {
  145. if (null !== $test) {
  146. $e = new Twig_Error_Syntax(sprintf('Unexpected "%s" tag', $token->getValue()), $token->getLine(), $this->stream->getSourceContext());
  147. if (is_array($test) && isset($test[0]) && $test[0] instanceof Twig_TokenParserInterface) {
  148. $e->appendMessage(sprintf(' (expecting closing tag for the "%s" tag defined near line %s).', $test[0]->getTag(), $lineno));
  149. }
  150. } else {
  151. $e = new Twig_Error_Syntax(sprintf('Unknown "%s" tag.', $token->getValue()), $token->getLine(), $this->stream->getSourceContext());
  152. $e->addSuggestions($token->getValue(), array_keys($this->env->getTags()));
  153. }
  154. throw $e;
  155. }
  156. $this->stream->next();
  157. $node = $subparser->parse($token);
  158. if (null !== $node) {
  159. $rv[] = $node;
  160. }
  161. break;
  162. default:
  163. throw new Twig_Error_Syntax('Lexer or parser ended up in unsupported state.', $this->getCurrentToken()->getLine(), $this->stream->getSourceContext());
  164. }
  165. }
  166. if (1 === count($rv)) {
  167. return $rv[0];
  168. }
  169. return new Twig_Node($rv, array(), $lineno);
  170. }
  171. /**
  172. * @deprecated since 1.27 (to be removed in 2.0)
  173. */
  174. public function addHandler($name, $class)
  175. {
  176. @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0.', E_USER_DEPRECATED);
  177. $this->handlers[$name] = $class;
  178. }
  179. /**
  180. * @deprecated since 1.27 (to be removed in 2.0)
  181. */
  182. public function addNodeVisitor(Twig_NodeVisitorInterface $visitor)
  183. {
  184. @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0.', E_USER_DEPRECATED);
  185. $this->visitors[] = $visitor;
  186. }
  187. public function getBlockStack()
  188. {
  189. return $this->blockStack;
  190. }
  191. public function peekBlockStack()
  192. {
  193. return $this->blockStack[count($this->blockStack) - 1];
  194. }
  195. public function popBlockStack()
  196. {
  197. array_pop($this->blockStack);
  198. }
  199. public function pushBlockStack($name)
  200. {
  201. $this->blockStack[] = $name;
  202. }
  203. public function hasBlock($name)
  204. {
  205. return isset($this->blocks[$name]);
  206. }
  207. public function getBlock($name)
  208. {
  209. return $this->blocks[$name];
  210. }
  211. public function setBlock($name, Twig_Node_Block $value)
  212. {
  213. $this->blocks[$name] = new Twig_Node_Body(array($value), array(), $value->getTemplateLine());
  214. }
  215. public function hasMacro($name)
  216. {
  217. return isset($this->macros[$name]);
  218. }
  219. public function setMacro($name, Twig_Node_Macro $node)
  220. {
  221. if ($this->isReservedMacroName($name)) {
  222. throw new Twig_Error_Syntax(sprintf('"%s" cannot be used as a macro name as it is a reserved keyword.', $name), $node->getTemplateLine(), $this->stream->getSourceContext());
  223. }
  224. $this->macros[$name] = $node;
  225. }
  226. public function isReservedMacroName($name)
  227. {
  228. if (null === $this->reservedMacroNames) {
  229. $this->reservedMacroNames = array();
  230. $r = new ReflectionClass($this->env->getBaseTemplateClass());
  231. foreach ($r->getMethods() as $method) {
  232. $methodName = strtolower($method->getName());
  233. if ('get' === substr($methodName, 0, 3) && isset($methodName[3])) {
  234. $this->reservedMacroNames[] = substr($methodName, 3);
  235. }
  236. }
  237. }
  238. return in_array(strtolower($name), $this->reservedMacroNames);
  239. }
  240. public function addTrait($trait)
  241. {
  242. $this->traits[] = $trait;
  243. }
  244. public function hasTraits()
  245. {
  246. return count($this->traits) > 0;
  247. }
  248. public function embedTemplate(Twig_Node_Module $template)
  249. {
  250. $template->setIndex(mt_rand());
  251. $this->embeddedTemplates[] = $template;
  252. }
  253. public function addImportedSymbol($type, $alias, $name = null, Twig_Node_Expression $node = null)
  254. {
  255. $this->importedSymbols[0][$type][$alias] = array('name' => $name, 'node' => $node);
  256. }
  257. public function getImportedSymbol($type, $alias)
  258. {
  259. foreach ($this->importedSymbols as $functions) {
  260. if (isset($functions[$type][$alias])) {
  261. return $functions[$type][$alias];
  262. }
  263. }
  264. }
  265. public function isMainScope()
  266. {
  267. return 1 === count($this->importedSymbols);
  268. }
  269. public function pushLocalScope()
  270. {
  271. array_unshift($this->importedSymbols, array());
  272. }
  273. public function popLocalScope()
  274. {
  275. array_shift($this->importedSymbols);
  276. }
  277. /**
  278. * @return Twig_ExpressionParser
  279. */
  280. public function getExpressionParser()
  281. {
  282. return $this->expressionParser;
  283. }
  284. public function getParent()
  285. {
  286. return $this->parent;
  287. }
  288. public function setParent($parent)
  289. {
  290. $this->parent = $parent;
  291. }
  292. /**
  293. * @return Twig_TokenStream
  294. */
  295. public function getStream()
  296. {
  297. return $this->stream;
  298. }
  299. /**
  300. * @return Twig_Token
  301. */
  302. public function getCurrentToken()
  303. {
  304. return $this->stream->getCurrent();
  305. }
  306. protected function filterBodyNodes(Twig_NodeInterface $node)
  307. {
  308. // check that the body does not contain non-empty output nodes
  309. if (
  310. ($node instanceof Twig_Node_Text && !ctype_space($node->getAttribute('data')))
  311. ||
  312. (!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface)
  313. ) {
  314. if (false !== strpos((string) $node, chr(0xEF).chr(0xBB).chr(0xBF))) {
  315. throw new Twig_Error_Syntax('A template that extends another one cannot start with a byte order mark (BOM); it must be removed.', $node->getTemplateLine(), $this->stream->getSourceContext());
  316. }
  317. throw new Twig_Error_Syntax('A template that extends another one cannot include content outside Twig blocks. Did you forget to put the content inside a {% block %} tag?', $node->getTemplateLine(), $this->stream->getSourceContext());
  318. }
  319. // bypass nodes that will "capture" the output
  320. if ($node instanceof Twig_NodeCaptureInterface) {
  321. return $node;
  322. }
  323. if ($node instanceof Twig_NodeOutputInterface) {
  324. return;
  325. }
  326. foreach ($node as $k => $n) {
  327. if (null !== $n && null === $this->filterBodyNodes($n)) {
  328. $node->removeNode($k);
  329. }
  330. }
  331. return $node;
  332. }
  333. }
  334. class_alias('Twig_Parser', 'Twig\Parser', false);
  335. class_exists('Twig_Node');
  336. class_exists('Twig_TokenStream');