PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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