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

/Web/Admin/App/Lib/Twig/Parser.php

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