Environment.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Stores the Twig configuration.
  12. *
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class Twig_Environment
  16. {
  17. const VERSION = '1.24.0';
  18. protected $charset;
  19. protected $loader;
  20. protected $debug;
  21. protected $autoReload;
  22. protected $cache;
  23. protected $lexer;
  24. protected $parser;
  25. protected $compiler;
  26. protected $baseTemplateClass;
  27. protected $extensions;
  28. protected $parsers;
  29. protected $visitors;
  30. protected $filters;
  31. protected $tests;
  32. protected $functions;
  33. protected $globals;
  34. protected $runtimeInitialized = false;
  35. protected $extensionInitialized = false;
  36. protected $loadedTemplates;
  37. protected $strictVariables;
  38. protected $unaryOperators;
  39. protected $binaryOperators;
  40. protected $templateClassPrefix = '__TwigTemplate_';
  41. protected $functionCallbacks = array();
  42. protected $filterCallbacks = array();
  43. protected $staging;
  44. private $originalCache;
  45. private $bcWriteCacheFile = false;
  46. private $bcGetCacheFilename = false;
  47. private $lastModifiedExtension = 0;
  48. /**
  49. * Constructor.
  50. *
  51. * Available options:
  52. *
  53. * * debug: When set to true, it automatically set "auto_reload" to true as
  54. * well (default to false).
  55. *
  56. * * charset: The charset used by the templates (default to UTF-8).
  57. *
  58. * * base_template_class: The base template class to use for generated
  59. * templates (default to Twig_Template).
  60. *
  61. * * cache: An absolute path where to store the compiled templates,
  62. * a Twig_Cache_Interface implementation,
  63. * or false to disable compilation cache (default).
  64. *
  65. * * auto_reload: Whether to reload the template if the original source changed.
  66. * If you don't provide the auto_reload option, it will be
  67. * determined automatically based on the debug value.
  68. *
  69. * * strict_variables: Whether to ignore invalid variables in templates
  70. * (default to false).
  71. *
  72. * * autoescape: Whether to enable auto-escaping (default to html):
  73. * * false: disable auto-escaping
  74. * * true: equivalent to html
  75. * * html, js: set the autoescaping to one of the supported strategies
  76. * * filename: set the autoescaping strategy based on the template filename extension
  77. * * PHP callback: a PHP callback that returns an escaping strategy based on the template "filename"
  78. *
  79. * * optimizations: A flag that indicates which optimizations to apply
  80. * (default to -1 which means that all optimizations are enabled;
  81. * set it to 0 to disable).
  82. *
  83. * @param Twig_LoaderInterface $loader A Twig_LoaderInterface instance
  84. * @param array $options An array of options
  85. */
  86. public function __construct(Twig_LoaderInterface $loader = null, $options = array())
  87. {
  88. if (null !== $loader) {
  89. $this->setLoader($loader);
  90. } else {
  91. @trigger_error('Not passing a Twig_LoaderInterface as the first constructor argument of Twig_Environment is deprecated since version 1.21.', E_USER_DEPRECATED);
  92. }
  93. $options = array_merge(array(
  94. 'debug' => false,
  95. 'charset' => 'UTF-8',
  96. 'base_template_class' => 'Twig_Template',
  97. 'strict_variables' => false,
  98. 'autoescape' => 'html',
  99. 'cache' => false,
  100. 'auto_reload' => null,
  101. 'optimizations' => -1,
  102. ), $options);
  103. $this->debug = (bool) $options['debug'];
  104. $this->charset = strtoupper($options['charset']);
  105. $this->baseTemplateClass = $options['base_template_class'];
  106. $this->autoReload = null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload'];
  107. $this->strictVariables = (bool) $options['strict_variables'];
  108. $this->setCache($options['cache']);
  109. $this->addExtension(new Twig_Extension_Core());
  110. $this->addExtension(new Twig_Extension_Escaper($options['autoescape']));
  111. $this->addExtension(new Twig_Extension_Optimizer($options['optimizations']));
  112. $this->staging = new Twig_Extension_Staging();
  113. // For BC
  114. if (is_string($this->originalCache)) {
  115. $r = new ReflectionMethod($this, 'writeCacheFile');
  116. if ($r->getDeclaringClass()->getName() !== __CLASS__) {
  117. @trigger_error('The Twig_Environment::writeCacheFile method is deprecated since version 1.22 and will be removed in Twig 2.0.', E_USER_DEPRECATED);
  118. $this->bcWriteCacheFile = true;
  119. }
  120. $r = new ReflectionMethod($this, 'getCacheFilename');
  121. if ($r->getDeclaringClass()->getName() !== __CLASS__) {
  122. @trigger_error('The Twig_Environment::getCacheFilename method is deprecated since version 1.22 and will be removed in Twig 2.0.', E_USER_DEPRECATED);
  123. $this->bcGetCacheFilename = true;
  124. }
  125. }
  126. }
  127. /**
  128. * Gets the base template class for compiled templates.
  129. *
  130. * @return string The base template class name
  131. */
  132. public function getBaseTemplateClass()
  133. {
  134. return $this->baseTemplateClass;
  135. }
  136. /**
  137. * Sets the base template class for compiled templates.
  138. *
  139. * @param string $class The base template class name
  140. */
  141. public function setBaseTemplateClass($class)
  142. {
  143. $this->baseTemplateClass = $class;
  144. }
  145. /**
  146. * Enables debugging mode.
  147. */
  148. public function enableDebug()
  149. {
  150. $this->debug = true;
  151. }
  152. /**
  153. * Disables debugging mode.
  154. */
  155. public function disableDebug()
  156. {
  157. $this->debug = false;
  158. }
  159. /**
  160. * Checks if debug mode is enabled.
  161. *
  162. * @return bool true if debug mode is enabled, false otherwise
  163. */
  164. public function isDebug()
  165. {
  166. return $this->debug;
  167. }
  168. /**
  169. * Enables the auto_reload option.
  170. */
  171. public function enableAutoReload()
  172. {
  173. $this->autoReload = true;
  174. }
  175. /**
  176. * Disables the auto_reload option.
  177. */
  178. public function disableAutoReload()
  179. {
  180. $this->autoReload = false;
  181. }
  182. /**
  183. * Checks if the auto_reload option is enabled.
  184. *
  185. * @return bool true if auto_reload is enabled, false otherwise
  186. */
  187. public function isAutoReload()
  188. {
  189. return $this->autoReload;
  190. }
  191. /**
  192. * Enables the strict_variables option.
  193. */
  194. public function enableStrictVariables()
  195. {
  196. $this->strictVariables = true;
  197. }
  198. /**
  199. * Disables the strict_variables option.
  200. */
  201. public function disableStrictVariables()
  202. {
  203. $this->strictVariables = false;
  204. }
  205. /**
  206. * Checks if the strict_variables option is enabled.
  207. *
  208. * @return bool true if strict_variables is enabled, false otherwise
  209. */
  210. public function isStrictVariables()
  211. {
  212. return $this->strictVariables;
  213. }
  214. /**
  215. * Gets the current cache implementation.
  216. *
  217. * @param bool $original Whether to return the original cache option or the real cache instance
  218. *
  219. * @return Twig_CacheInterface|string|false A Twig_CacheInterface implementation,
  220. * an absolute path to the compiled templates,
  221. * or false to disable cache
  222. */
  223. public function getCache($original = true)
  224. {
  225. return $original ? $this->originalCache : $this->cache;
  226. }
  227. /**
  228. * Sets the current cache implementation.
  229. *
  230. * @param Twig_CacheInterface|string|false $cache A Twig_CacheInterface implementation,
  231. * an absolute path to the compiled templates,
  232. * or false to disable cache
  233. */
  234. public function setCache($cache)
  235. {
  236. if (is_string($cache)) {
  237. $this->originalCache = $cache;
  238. $this->cache = new Twig_Cache_Filesystem($cache);
  239. } elseif (false === $cache) {
  240. $this->originalCache = $cache;
  241. $this->cache = new Twig_Cache_Null();
  242. } elseif (null === $cache) {
  243. @trigger_error('Using "null" as the cache strategy is deprecated since version 1.23 and will be removed in Twig 2.0.', E_USER_DEPRECATED);
  244. $this->originalCache = false;
  245. $this->cache = new Twig_Cache_Null();
  246. } elseif ($cache instanceof Twig_CacheInterface) {
  247. $this->originalCache = $this->cache = $cache;
  248. } else {
  249. throw new LogicException(sprintf('Cache can only be a string, false, or a Twig_CacheInterface implementation.'));
  250. }
  251. }
  252. /**
  253. * Gets the cache filename for a given template.
  254. *
  255. * @param string $name The template name
  256. *
  257. * @return string|false The cache file name or false when caching is disabled
  258. *
  259. * @deprecated since 1.22 (to be removed in 2.0)
  260. */
  261. public function getCacheFilename($name)
  262. {
  263. @trigger_error(sprintf('The %s method is deprecated since version 1.22 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
  264. $key = $this->cache->generateKey($name, $this->getTemplateClass($name));
  265. return !$key ? false : $key;
  266. }
  267. /**
  268. * Gets the template class associated with the given string.
  269. *
  270. * The generated template class is based on the following parameters:
  271. *
  272. * * The cache key for the given template;
  273. * * The currently enabled extensions;
  274. * * Whether the Twig C extension is available or not.
  275. *
  276. * @param string $name The name for which to calculate the template class name
  277. * @param int|null $index The index if it is an embedded template
  278. *
  279. * @return string The template class name
  280. */
  281. public function getTemplateClass($name, $index = null)
  282. {
  283. $key = $this->getLoader()->getCacheKey($name);
  284. $key .= json_encode(array_keys($this->extensions));
  285. $key .= function_exists('twig_template_get_attributes');
  286. return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '_'.$index);
  287. }
  288. /**
  289. * Gets the template class prefix.
  290. *
  291. * @return string The template class prefix
  292. *
  293. * @deprecated since 1.22 (to be removed in 2.0)
  294. */
  295. public function getTemplateClassPrefix()
  296. {
  297. @trigger_error(sprintf('The %s method is deprecated since version 1.22 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
  298. return $this->templateClassPrefix;
  299. }
  300. /**
  301. * Renders a template.
  302. *
  303. * @param string $name The template name
  304. * @param array $context An array of parameters to pass to the template
  305. *
  306. * @return string The rendered template
  307. *
  308. * @throws Twig_Error_Loader When the template cannot be found
  309. * @throws Twig_Error_Syntax When an error occurred during compilation
  310. * @throws Twig_Error_Runtime When an error occurred during rendering
  311. */
  312. public function render($name, array $context = array())
  313. {
  314. return $this->loadTemplate($name)->render($context);
  315. }
  316. /**
  317. * Displays a template.
  318. *
  319. * @param string $name The template name
  320. * @param array $context An array of parameters to pass to the template
  321. *
  322. * @throws Twig_Error_Loader When the template cannot be found
  323. * @throws Twig_Error_Syntax When an error occurred during compilation
  324. * @throws Twig_Error_Runtime When an error occurred during rendering
  325. */
  326. public function display($name, array $context = array())
  327. {
  328. $this->loadTemplate($name)->display($context);
  329. }
  330. /**
  331. * Loads a template by name.
  332. *
  333. * @param string $name The template name
  334. * @param int $index The index if it is an embedded template
  335. *
  336. * @return Twig_TemplateInterface A template instance representing the given template name
  337. *
  338. * @throws Twig_Error_Loader When the template cannot be found
  339. * @throws Twig_Error_Syntax When an error occurred during compilation
  340. */
  341. public function loadTemplate($name, $index = null)
  342. {
  343. $cls = $this->getTemplateClass($name, $index);
  344. if (isset($this->loadedTemplates[$cls])) {
  345. return $this->loadedTemplates[$cls];
  346. }
  347. if (!class_exists($cls, false)) {
  348. if ($this->bcGetCacheFilename) {
  349. $key = $this->getCacheFilename($name);
  350. } else {
  351. $key = $this->cache->generateKey($name, $cls);
  352. }
  353. if (!$this->isAutoReload() || $this->isTemplateFresh($name, $this->cache->getTimestamp($key))) {
  354. $this->cache->load($key);
  355. }
  356. if (!class_exists($cls, false)) {
  357. $content = $this->compileSource($this->getLoader()->getSource($name), $name);
  358. if ($this->bcWriteCacheFile) {
  359. $this->writeCacheFile($key, $content);
  360. } else {
  361. $this->cache->write($key, $content);
  362. }
  363. eval('?>'.$content);
  364. }
  365. }
  366. if (!$this->runtimeInitialized) {
  367. $this->initRuntime();
  368. }
  369. return $this->loadedTemplates[$cls] = new $cls($this);
  370. }
  371. /**
  372. * Creates a template from source.
  373. *
  374. * This method should not be used as a generic way to load templates.
  375. *
  376. * @param string $template The template name
  377. *
  378. * @return Twig_Template A template instance representing the given template name
  379. *
  380. * @throws Twig_Error_Loader When the template cannot be found
  381. * @throws Twig_Error_Syntax When an error occurred during compilation
  382. */
  383. public function createTemplate($template)
  384. {
  385. $name = sprintf('__string_template__%s', hash('sha256', uniqid(mt_rand(), true), false));
  386. $loader = new Twig_Loader_Chain(array(
  387. new Twig_Loader_Array(array($name => $template)),
  388. $current = $this->getLoader(),
  389. ));
  390. $this->setLoader($loader);
  391. try {
  392. $template = $this->loadTemplate($name);
  393. } catch (Exception $e) {
  394. $this->setLoader($current);
  395. throw $e;
  396. }
  397. $this->setLoader($current);
  398. return $template;
  399. }
  400. /**
  401. * Returns true if the template is still fresh.
  402. *
  403. * Besides checking the loader for freshness information,
  404. * this method also checks if the enabled extensions have
  405. * not changed.
  406. *
  407. * @param string $name The template name
  408. * @param int $time The last modification time of the cached template
  409. *
  410. * @return bool true if the template is fresh, false otherwise
  411. */
  412. public function isTemplateFresh($name, $time)
  413. {
  414. if (0 === $this->lastModifiedExtension) {
  415. foreach ($this->extensions as $extension) {
  416. $r = new ReflectionObject($extension);
  417. if (file_exists($r->getFileName()) && ($extensionTime = filemtime($r->getFileName())) > $this->lastModifiedExtension) {
  418. $this->lastModifiedExtension = $extensionTime;
  419. }
  420. }
  421. }
  422. return $this->lastModifiedExtension <= $time && $this->getLoader()->isFresh($name, $time);
  423. }
  424. /**
  425. * Tries to load a template consecutively from an array.
  426. *
  427. * Similar to loadTemplate() but it also accepts Twig_TemplateInterface instances and an array
  428. * of templates where each is tried to be loaded.
  429. *
  430. * @param string|Twig_Template|array $names A template or an array of templates to try consecutively
  431. *
  432. * @return Twig_Template
  433. *
  434. * @throws Twig_Error_Loader When none of the templates can be found
  435. * @throws Twig_Error_Syntax When an error occurred during compilation
  436. */
  437. public function resolveTemplate($names)
  438. {
  439. if (!is_array($names)) {
  440. $names = array($names);
  441. }
  442. foreach ($names as $name) {
  443. if ($name instanceof Twig_Template) {
  444. return $name;
  445. }
  446. try {
  447. return $this->loadTemplate($name);
  448. } catch (Twig_Error_Loader $e) {
  449. }
  450. }
  451. if (1 === count($names)) {
  452. throw $e;
  453. }
  454. throw new Twig_Error_Loader(sprintf('Unable to find one of the following templates: "%s".', implode('", "', $names)));
  455. }
  456. /**
  457. * Clears the internal template cache.
  458. *
  459. * @deprecated since 1.18.3 (to be removed in 2.0)
  460. */
  461. public function clearTemplateCache()
  462. {
  463. @trigger_error(sprintf('The %s method is deprecated since version 1.18.3 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
  464. $this->loadedTemplates = array();
  465. }
  466. /**
  467. * Clears the template cache files on the filesystem.
  468. *
  469. * @deprecated since 1.22 (to be removed in 2.0)
  470. */
  471. public function clearCacheFiles()
  472. {
  473. @trigger_error(sprintf('The %s method is deprecated since version 1.22 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
  474. if (is_string($this->originalCache)) {
  475. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->originalCache), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  476. if ($file->isFile()) {
  477. @unlink($file->getPathname());
  478. }
  479. }
  480. }
  481. }
  482. /**
  483. * Gets the Lexer instance.
  484. *
  485. * @return Twig_LexerInterface A Twig_LexerInterface instance
  486. */
  487. public function getLexer()
  488. {
  489. if (null === $this->lexer) {
  490. $this->lexer = new Twig_Lexer($this);
  491. }
  492. return $this->lexer;
  493. }
  494. /**
  495. * Sets the Lexer instance.
  496. *
  497. * @param Twig_LexerInterface $lexer A Twig_LexerInterface instance
  498. */
  499. public function setLexer(Twig_LexerInterface $lexer)
  500. {
  501. $this->lexer = $lexer;
  502. }
  503. /**
  504. * Tokenizes a source code.
  505. *
  506. * @param string $source The template source code
  507. * @param string $name The template name
  508. *
  509. * @return Twig_TokenStream A Twig_TokenStream instance
  510. *
  511. * @throws Twig_Error_Syntax When the code is syntactically wrong
  512. */
  513. public function tokenize($source, $name = null)
  514. {
  515. return $this->getLexer()->tokenize($source, $name);
  516. }
  517. /**
  518. * Gets the Parser instance.
  519. *
  520. * @return Twig_ParserInterface A Twig_ParserInterface instance
  521. */
  522. public function getParser()
  523. {
  524. if (null === $this->parser) {
  525. $this->parser = new Twig_Parser($this);
  526. }
  527. return $this->parser;
  528. }
  529. /**
  530. * Sets the Parser instance.
  531. *
  532. * @param Twig_ParserInterface $parser A Twig_ParserInterface instance
  533. */
  534. public function setParser(Twig_ParserInterface $parser)
  535. {
  536. $this->parser = $parser;
  537. }
  538. /**
  539. * Converts a token stream to a node tree.
  540. *
  541. * @param Twig_TokenStream $stream A token stream instance
  542. *
  543. * @return Twig_Node_Module A node tree
  544. *
  545. * @throws Twig_Error_Syntax When the token stream is syntactically or semantically wrong
  546. */
  547. public function parse(Twig_TokenStream $stream)
  548. {
  549. return $this->getParser()->parse($stream);
  550. }
  551. /**
  552. * Gets the Compiler instance.
  553. *
  554. * @return Twig_CompilerInterface A Twig_CompilerInterface instance
  555. */
  556. public function getCompiler()
  557. {
  558. if (null === $this->compiler) {
  559. $this->compiler = new Twig_Compiler($this);
  560. }
  561. return $this->compiler;
  562. }
  563. /**
  564. * Sets the Compiler instance.
  565. *
  566. * @param Twig_CompilerInterface $compiler A Twig_CompilerInterface instance
  567. */
  568. public function setCompiler(Twig_CompilerInterface $compiler)
  569. {
  570. $this->compiler = $compiler;
  571. }
  572. /**
  573. * Compiles a node and returns the PHP code.
  574. *
  575. * @param Twig_NodeInterface $node A Twig_NodeInterface instance
  576. *
  577. * @return string The compiled PHP source code
  578. */
  579. public function compile(Twig_NodeInterface $node)
  580. {
  581. return $this->getCompiler()->compile($node)->getSource();
  582. }
  583. /**
  584. * Compiles a template source code.
  585. *
  586. * @param string $source The template source code
  587. * @param string $name The template name
  588. *
  589. * @return string The compiled PHP source code
  590. *
  591. * @throws Twig_Error_Syntax When there was an error during tokenizing, parsing or compiling
  592. */
  593. public function compileSource($source, $name = null)
  594. {
  595. try {
  596. $compiled = $this->compile($this->parse($this->tokenize($source, $name)), $source);
  597. if (isset($source[0])) {
  598. $compiled .= '/* '.str_replace(array('*/', "\r\n", "\r", "\n"), array('*//* ', "\n", "\n", "*/\n/* "), $source)."*/\n";
  599. }
  600. return $compiled;
  601. } catch (Twig_Error $e) {
  602. $e->setTemplateFile($name);
  603. throw $e;
  604. } catch (Exception $e) {
  605. throw new Twig_Error_Syntax(sprintf('An exception has been thrown during the compilation of a template ("%s").', $e->getMessage()), -1, $name, $e);
  606. }
  607. }
  608. /**
  609. * Sets the Loader instance.
  610. *
  611. * @param Twig_LoaderInterface $loader A Twig_LoaderInterface instance
  612. */
  613. public function setLoader(Twig_LoaderInterface $loader)
  614. {
  615. $this->loader = $loader;
  616. }
  617. /**
  618. * Gets the Loader instance.
  619. *
  620. * @return Twig_LoaderInterface A Twig_LoaderInterface instance
  621. */
  622. public function getLoader()
  623. {
  624. if (null === $this->loader) {
  625. throw new LogicException('You must set a loader first.');
  626. }
  627. return $this->loader;
  628. }
  629. /**
  630. * Sets the default template charset.
  631. *
  632. * @param string $charset The default charset
  633. */
  634. public function setCharset($charset)
  635. {
  636. $this->charset = strtoupper($charset);
  637. }
  638. /**
  639. * Gets the default template charset.
  640. *
  641. * @return string The default charset
  642. */
  643. public function getCharset()
  644. {
  645. return $this->charset;
  646. }
  647. /**
  648. * Initializes the runtime environment.
  649. *
  650. * @deprecated since 1.23 (to be removed in 2.0)
  651. */
  652. public function initRuntime()
  653. {
  654. $this->runtimeInitialized = true;
  655. foreach ($this->getExtensions() as $name => $extension) {
  656. if (!$extension instanceof Twig_Extension_InitRuntimeInterface) {
  657. $m = new ReflectionMethod($extension, 'initRuntime');
  658. if ('Twig_Extension' !== $m->getDeclaringClass()->getName()) {
  659. @trigger_error(sprintf('Defining the initRuntime() method in the "%s" extension is deprecated since version 1.23. Use the `needs_environment` option to get the Twig_Environment instance in filters, functions, or tests; or explicitly implement Twig_Extension_InitRuntimeInterface if needed (not recommended).', $name), E_USER_DEPRECATED);
  660. }
  661. }
  662. $extension->initRuntime($this);
  663. }
  664. }
  665. /**
  666. * Returns true if the given extension is registered.
  667. *
  668. * @param string $name The extension name
  669. *
  670. * @return bool Whether the extension is registered or not
  671. */
  672. public function hasExtension($name)
  673. {
  674. return isset($this->extensions[$name]);
  675. }
  676. /**
  677. * Gets an extension by name.
  678. *
  679. * @param string $name The extension name
  680. *
  681. * @return Twig_ExtensionInterface A Twig_ExtensionInterface instance
  682. */
  683. public function getExtension($name)
  684. {
  685. if (!isset($this->extensions[$name])) {
  686. throw new Twig_Error_Runtime(sprintf('The "%s" extension is not enabled.', $name));
  687. }
  688. return $this->extensions[$name];
  689. }
  690. /**
  691. * Registers an extension.
  692. *
  693. * @param Twig_ExtensionInterface $extension A Twig_ExtensionInterface instance
  694. */
  695. public function addExtension(Twig_ExtensionInterface $extension)
  696. {
  697. $name = $extension->getName();
  698. if ($this->extensionInitialized) {
  699. throw new LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.', $name));
  700. }
  701. if (isset($this->extensions[$name])) {
  702. @trigger_error(sprintf('The possibility to register the same extension twice ("%s") is deprecated since version 1.23 and will be removed in Twig 2.0. Use proper PHP inheritance instead.', $name), E_USER_DEPRECATED);
  703. }
  704. $this->lastModifiedExtension = 0;
  705. $this->extensions[$name] = $extension;
  706. }
  707. /**
  708. * Removes an extension by name.
  709. *
  710. * This method is deprecated and you should not use it.
  711. *
  712. * @param string $name The extension name
  713. *
  714. * @deprecated since 1.12 (to be removed in 2.0)
  715. */
  716. public function removeExtension($name)
  717. {
  718. @trigger_error(sprintf('The %s method is deprecated since version 1.12 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
  719. if ($this->extensionInitialized) {
  720. throw new LogicException(sprintf('Unable to remove extension "%s" as extensions have already been initialized.', $name));
  721. }
  722. unset($this->extensions[$name]);
  723. }
  724. /**
  725. * Registers an array of extensions.
  726. *
  727. * @param array $extensions An array of extensions
  728. */
  729. public function setExtensions(array $extensions)
  730. {
  731. foreach ($extensions as $extension) {
  732. $this->addExtension($extension);
  733. }
  734. }
  735. /**
  736. * Returns all registered extensions.
  737. *
  738. * @return array An array of extensions
  739. */
  740. public function getExtensions()
  741. {
  742. return $this->extensions;
  743. }
  744. /**
  745. * Registers a Token Parser.
  746. *
  747. * @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance
  748. */
  749. public function addTokenParser(Twig_TokenParserInterface $parser)
  750. {
  751. if ($this->extensionInitialized) {
  752. throw new LogicException('Unable to add a token parser as extensions have already been initialized.');
  753. }
  754. $this->staging->addTokenParser($parser);
  755. }
  756. /**
  757. * Gets the registered Token Parsers.
  758. *
  759. * @return Twig_TokenParserBrokerInterface A broker containing token parsers
  760. */
  761. public function getTokenParsers()
  762. {
  763. if (!$this->extensionInitialized) {
  764. $this->initExtensions();
  765. }
  766. return $this->parsers;
  767. }
  768. /**
  769. * Gets registered tags.
  770. *
  771. * Be warned that this method cannot return tags defined by Twig_TokenParserBrokerInterface classes.
  772. *
  773. * @return Twig_TokenParserInterface[] An array of Twig_TokenParserInterface instances
  774. */
  775. public function getTags()
  776. {
  777. $tags = array();
  778. foreach ($this->getTokenParsers()->getParsers() as $parser) {
  779. if ($parser instanceof Twig_TokenParserInterface) {
  780. $tags[$parser->getTag()] = $parser;
  781. }
  782. }
  783. return $tags;
  784. }
  785. /**
  786. * Registers a Node Visitor.
  787. *
  788. * @param Twig_NodeVisitorInterface $visitor A Twig_NodeVisitorInterface instance
  789. */
  790. public function addNodeVisitor(Twig_NodeVisitorInterface $visitor)
  791. {
  792. if ($this->extensionInitialized) {
  793. throw new LogicException('Unable to add a node visitor as extensions have already been initialized.');
  794. }
  795. $this->staging->addNodeVisitor($visitor);
  796. }
  797. /**
  798. * Gets the registered Node Visitors.
  799. *
  800. * @return Twig_NodeVisitorInterface[] An array of Twig_NodeVisitorInterface instances
  801. */
  802. public function getNodeVisitors()
  803. {
  804. if (!$this->extensionInitialized) {
  805. $this->initExtensions();
  806. }
  807. return $this->visitors;
  808. }
  809. /**
  810. * Registers a Filter.
  811. *
  812. * @param string|Twig_SimpleFilter $name The filter name or a Twig_SimpleFilter instance
  813. * @param Twig_FilterInterface|Twig_SimpleFilter $filter A Twig_FilterInterface instance or a Twig_SimpleFilter instance
  814. */
  815. public function addFilter($name, $filter = null)
  816. {
  817. if (!$name instanceof Twig_SimpleFilter && !($filter instanceof Twig_SimpleFilter || $filter instanceof Twig_FilterInterface)) {
  818. throw new LogicException('A filter must be an instance of Twig_FilterInterface or Twig_SimpleFilter');
  819. }
  820. if ($name instanceof Twig_SimpleFilter) {
  821. $filter = $name;
  822. $name = $filter->getName();
  823. } else {
  824. @trigger_error(sprintf('Passing a name as a first argument to the %s method is deprecated since version 1.21. Pass an instance of "Twig_SimpleFilter" instead when defining filter "%s".', __METHOD__, $name), E_USER_DEPRECATED);
  825. }
  826. if ($this->extensionInitialized) {
  827. throw new LogicException(sprintf('Unable to add filter "%s" as extensions have already been initialized.', $name));
  828. }
  829. $this->staging->addFilter($name, $filter);
  830. }
  831. /**
  832. * Get a filter by name.
  833. *
  834. * Subclasses may override this method and load filters differently;
  835. * so no list of filters is available.
  836. *
  837. * @param string $name The filter name
  838. *
  839. * @return Twig_Filter|false A Twig_Filter instance or false if the filter does not exist
  840. */
  841. public function getFilter($name)
  842. {
  843. if (!$this->extensionInitialized) {
  844. $this->initExtensions();
  845. }
  846. if (isset($this->filters[$name])) {
  847. return $this->filters[$name];
  848. }
  849. foreach ($this->filters as $pattern => $filter) {
  850. $pattern = str_replace('\\*', '(.*?)', preg_quote($pattern, '#'), $count);
  851. if ($count) {
  852. if (preg_match('#^'.$pattern.'$#', $name, $matches)) {
  853. array_shift($matches);
  854. $filter->setArguments($matches);
  855. return $filter;
  856. }
  857. }
  858. }
  859. foreach ($this->filterCallbacks as $callback) {
  860. if (false !== $filter = call_user_func($callback, $name)) {
  861. return $filter;
  862. }
  863. }
  864. return false;
  865. }
  866. public function registerUndefinedFilterCallback($callable)
  867. {
  868. $this->filterCallbacks[] = $callable;
  869. }
  870. /**
  871. * Gets the registered Filters.
  872. *
  873. * Be warned that this method cannot return filters defined with registerUndefinedFilterCallback.
  874. *
  875. * @return Twig_FilterInterface[] An array of Twig_FilterInterface instances
  876. *
  877. * @see registerUndefinedFilterCallback
  878. */
  879. public function getFilters()
  880. {
  881. if (!$this->extensionInitialized) {
  882. $this->initExtensions();
  883. }
  884. return $this->filters;
  885. }
  886. /**
  887. * Registers a Test.
  888. *
  889. * @param string|Twig_SimpleTest $name The test name or a Twig_SimpleTest instance
  890. * @param Twig_TestInterface|Twig_SimpleTest $test A Twig_TestInterface instance or a Twig_SimpleTest instance
  891. */
  892. public function addTest($name, $test = null)
  893. {
  894. if (!$name instanceof Twig_SimpleTest && !($test instanceof Twig_SimpleTest || $test instanceof Twig_TestInterface)) {
  895. throw new LogicException('A test must be an instance of Twig_TestInterface or Twig_SimpleTest');
  896. }
  897. if ($name instanceof Twig_SimpleTest) {
  898. $test = $name;
  899. $name = $test->getName();
  900. } else {
  901. @trigger_error(sprintf('Passing a name as a first argument to the %s method is deprecated since version 1.21. Pass an instance of "Twig_SimpleTest" instead when defining test "%s".', __METHOD__, $name), E_USER_DEPRECATED);
  902. }
  903. if ($this->extensionInitialized) {
  904. throw new LogicException(sprintf('Unable to add test "%s" as extensions have already been initialized.', $name));
  905. }
  906. $this->staging->addTest($name, $test);
  907. }
  908. /**
  909. * Gets the registered Tests.
  910. *
  911. * @return Twig_TestInterface[] An array of Twig_TestInterface instances
  912. */
  913. public function getTests()
  914. {
  915. if (!$this->extensionInitialized) {
  916. $this->initExtensions();
  917. }
  918. return $this->tests;
  919. }
  920. /**
  921. * Gets a test by name.
  922. *
  923. * @param string $name The test name
  924. *
  925. * @return Twig_Test|false A Twig_Test instance or false if the test does not exist
  926. */
  927. public function getTest($name)
  928. {
  929. if (!$this->extensionInitialized) {
  930. $this->initExtensions();
  931. }
  932. if (isset($this->tests[$name])) {
  933. return $this->tests[$name];
  934. }
  935. return false;
  936. }
  937. /**
  938. * Registers a Function.
  939. *
  940. * @param string|Twig_SimpleFunction $name The function name or a Twig_SimpleFunction instance
  941. * @param Twig_FunctionInterface|Twig_SimpleFunction $function A Twig_FunctionInterface instance or a Twig_SimpleFunction instance
  942. */
  943. public function addFunction($name, $function = null)
  944. {
  945. if (!$name instanceof Twig_SimpleFunction && !($function instanceof Twig_SimpleFunction || $function instanceof Twig_FunctionInterface)) {
  946. throw new LogicException('A function must be an instance of Twig_FunctionInterface or Twig_SimpleFunction');
  947. }
  948. if ($name instanceof Twig_SimpleFunction) {
  949. $function = $name;
  950. $name = $function->getName();
  951. } else {
  952. @trigger_error(sprintf('Passing a name as a first argument to the %s method is deprecated since version 1.21. Pass an instance of "Twig_SimpleFunction" instead when defining function "%s".', __METHOD__, $name), E_USER_DEPRECATED);
  953. }
  954. if ($this->extensionInitialized) {
  955. throw new LogicException(sprintf('Unable to add function "%s" as extensions have already been initialized.', $name));
  956. }
  957. $this->staging->addFunction($name, $function);
  958. }
  959. /**
  960. * Get a function by name.
  961. *
  962. * Subclasses may override this method and load functions differently;
  963. * so no list of functions is available.
  964. *
  965. * @param string $name function name
  966. *
  967. * @return Twig_Function|false A Twig_Function instance or false if the function does not exist
  968. */
  969. public function getFunction($name)
  970. {
  971. if (!$this->extensionInitialized) {
  972. $this->initExtensions();
  973. }
  974. if (isset($this->functions[$name])) {
  975. return $this->functions[$name];
  976. }
  977. foreach ($this->functions as $pattern => $function) {
  978. $pattern = str_replace('\\*', '(.*?)', preg_quote($pattern, '#'), $count);
  979. if ($count) {
  980. if (preg_match('#^'.$pattern.'$#', $name, $matches)) {
  981. array_shift($matches);
  982. $function->setArguments($matches);
  983. return $function;
  984. }
  985. }
  986. }
  987. foreach ($this->functionCallbacks as $callback) {
  988. if (false !== $function = call_user_func($callback, $name)) {
  989. return $function;
  990. }
  991. }
  992. return false;
  993. }
  994. public function registerUndefinedFunctionCallback($callable)
  995. {
  996. $this->functionCallbacks[] = $callable;
  997. }
  998. /**
  999. * Gets registered functions.
  1000. *
  1001. * Be warned that this method cannot return functions defined with registerUndefinedFunctionCallback.
  1002. *
  1003. * @return Twig_FunctionInterface[] An array of Twig_FunctionInterface instances
  1004. *
  1005. * @see registerUndefinedFunctionCallback
  1006. */
  1007. public function getFunctions()
  1008. {
  1009. if (!$this->extensionInitialized) {
  1010. $this->initExtensions();
  1011. }
  1012. return $this->functions;
  1013. }
  1014. /**
  1015. * Registers a Global.
  1016. *
  1017. * New globals can be added before compiling or rendering a template;
  1018. * but after, you can only update existing globals.
  1019. *
  1020. * @param string $name The global name
  1021. * @param mixed $value The global value
  1022. */
  1023. public function addGlobal($name, $value)
  1024. {
  1025. if ($this->extensionInitialized || $this->runtimeInitialized) {
  1026. if (null === $this->globals) {
  1027. $this->globals = $this->initGlobals();
  1028. }
  1029. if (!array_key_exists($name, $this->globals)) {
  1030. // The deprecation notice must be turned into the following exception in Twig 2.0
  1031. @trigger_error(sprintf('Registering global variable "%s" at runtime or when the extensions have already been initialized is deprecated since version 1.21.', $name), E_USER_DEPRECATED);
  1032. //throw new LogicException(sprintf('Unable to add global "%s" as the runtime or the extensions have already been initialized.', $name));
  1033. }
  1034. }
  1035. if ($this->extensionInitialized || $this->runtimeInitialized) {
  1036. // update the value
  1037. $this->globals[$name] = $value;
  1038. } else {
  1039. $this->staging->addGlobal($name, $value);
  1040. }
  1041. }
  1042. /**
  1043. * Gets the registered Globals.
  1044. *
  1045. * @return array An array of globals
  1046. */
  1047. public function getGlobals()
  1048. {
  1049. if (!$this->runtimeInitialized && !$this->extensionInitialized) {
  1050. return $this->initGlobals();
  1051. }
  1052. if (null === $this->globals) {
  1053. $this->globals = $this->initGlobals();
  1054. }
  1055. return $this->globals;
  1056. }
  1057. /**
  1058. * Merges a context with the defined globals.
  1059. *
  1060. * @param array $context An array representing the context
  1061. *
  1062. * @return array The context merged with the globals
  1063. */
  1064. public function mergeGlobals(array $context)
  1065. {
  1066. // we don't use array_merge as the context being generally
  1067. // bigger than globals, this code is faster.
  1068. foreach ($this->getGlobals() as $key => $value) {
  1069. if (!array_key_exists($key, $context)) {
  1070. $context[$key] = $value;
  1071. }
  1072. }
  1073. return $context;
  1074. }
  1075. /**
  1076. * Gets the registered unary Operators.
  1077. *
  1078. * @return array An array of unary operators
  1079. */
  1080. public function getUnaryOperators()
  1081. {
  1082. if (!$this->extensionInitialized) {
  1083. $this->initExtensions();
  1084. }
  1085. return $this->unaryOperators;
  1086. }
  1087. /**
  1088. * Gets the registered binary Operators.
  1089. *
  1090. * @return array An array of binary operators
  1091. */
  1092. public function getBinaryOperators()
  1093. {
  1094. if (!$this->extensionInitialized) {
  1095. $this->initExtensions();
  1096. }
  1097. return $this->binaryOperators;
  1098. }
  1099. /**
  1100. * @deprecated since 1.23 (to be removed in 2.0)
  1101. */
  1102. public function computeAlternatives($name, $items)
  1103. {
  1104. @trigger_error(sprintf('The %s method is deprecated since version 1.23 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
  1105. return Twig_Error_Syntax::computeAlternatives($name, $items);
  1106. }
  1107. protected function initGlobals()
  1108. {
  1109. $globals = array();
  1110. foreach ($this->extensions as $name => $extension) {
  1111. if (!$extension instanceof Twig_Extension_GlobalsInterface) {
  1112. $m = new ReflectionMethod($extension, 'getGlobals');
  1113. if ('Twig_Extension' !== $m->getDeclaringClass()->getName()) {
  1114. @trigger_error(sprintf('Defining the getGlobals() method in the "%s" extension without explicitly implementing Twig_Extension_GlobalsInterface is deprecated since version 1.23.', $name), E_USER_DEPRECATED);
  1115. }
  1116. }
  1117. $extGlob = $extension->getGlobals();
  1118. if (!is_array($extGlob)) {
  1119. throw new UnexpectedValueException(sprintf('"%s::getGlobals()" must return an array of globals.', get_class($extension)));
  1120. }
  1121. $globals[] = $extGlob;
  1122. }
  1123. $globals[] = $this->staging->getGlobals();
  1124. return call_user_func_array('array_merge', $globals);
  1125. }
  1126. protected function initExtensions()
  1127. {
  1128. if ($this->extensionInitialized) {
  1129. return;
  1130. }
  1131. $this->extensionInitialized = true;
  1132. $this->parsers = new Twig_TokenParserBroker(array(), array(), false);
  1133. $this->filters = array();
  1134. $this->functions = array();
  1135. $this->tests = array();
  1136. $this->visitors = array();
  1137. $this->unaryOperators = array();
  1138. $this->binaryOperators = array();
  1139. foreach ($this->extensions as $extension) {
  1140. $this->initExtension($extension);
  1141. }
  1142. $this->initExtension($this->staging);
  1143. }
  1144. protected function initExtension(Twig_ExtensionInterface $extension)
  1145. {
  1146. // filters
  1147. foreach ($extension->getFilters() as $name => $filter) {
  1148. if ($filter instanceof Twig_SimpleFilter) {
  1149. $name = $filter->getName();
  1150. } else {
  1151. @trigger_error(sprintf('Using an instance of "%s" for filter "%s" is deprecated since version 1.21. Use Twig_SimpleFilter instead.', get_class($filter), $name), E_USER_DEPRECATED);
  1152. }
  1153. $this->filters[$name] = $filter;
  1154. }
  1155. // functions
  1156. foreach ($extension->getFunctions() as $name => $function) {
  1157. if ($function instanceof Twig_SimpleFunction) {
  1158. $name = $function->getName();
  1159. } else {
  1160. @trigger_error(sprintf('Using an instance of "%s" for function "%s" is deprecated since version 1.21. Use Twig_SimpleFunction instead.', get_class($function), $name), E_USER_DEPRECATED);
  1161. }
  1162. $this->functions[$name] = $function;
  1163. }
  1164. // tests
  1165. foreach ($extension->getTests() as $name => $test) {
  1166. if ($test instanceof Twig_SimpleTest) {
  1167. $name = $test->getName();
  1168. } else {
  1169. @trigger_error(sprintf('Using an instance of "%s" for test "%s" is deprecated since version 1.21. Use Twig_SimpleTest instead.', get_class($test), $name), E_USER_DEPRECATED);
  1170. }
  1171. $this->tests[$name] = $test;
  1172. }
  1173. // token parsers
  1174. foreach ($extension->getTokenParsers() as $parser) {
  1175. if ($parser instanceof Twig_TokenParserInterface) {
  1176. $this->parsers->addTokenParser($parser);
  1177. } elseif ($parser instanceof Twig_TokenParserBrokerInterface) {
  1178. @trigger_error('Registering a Twig_TokenParserBrokerInterface instance is deprecated since version 1.21.', E_USER_DEPRECATED);
  1179. $this->parsers->addTokenParserBroker($parser);
  1180. } else {
  1181. throw new LogicException('getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances');
  1182. }
  1183. }
  1184. // node visitors
  1185. foreach ($extension->getNodeVisitors() as $visitor) {
  1186. $this->visitors[] = $visitor;
  1187. }
  1188. // operators
  1189. if ($operators = $extension->getOperators()) {
  1190. if (2 !== count($operators)) {
  1191. throw new InvalidArgumentException(sprintf('"%s::getOperators()" does not return a valid operators array.', get_class($extension)));
  1192. }
  1193. $this->unaryOperators = array_merge($this->unaryOperators, $operators[0]);
  1194. $this->binaryOperators = array_merge($this->binaryOperators, $operators[1]);
  1195. }
  1196. }
  1197. /**
  1198. * @deprecated since 1.22 (to be removed in 2.0)
  1199. */
  1200. protected function writeCacheFile($file, $content)
  1201. {
  1202. $this->cache->write($file, $content);
  1203. }
  1204. }