Environment.php 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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. * @package twig
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Environment
  17. {
  18. const VERSION = '1.1.2';
  19. protected $charset;
  20. protected $loader;
  21. protected $debug;
  22. protected $autoReload;
  23. protected $cache;
  24. protected $lexer;
  25. protected $parser;
  26. protected $compiler;
  27. protected $baseTemplateClass;
  28. protected $extensions;
  29. protected $parsers;
  30. protected $visitors;
  31. protected $filters;
  32. protected $tests;
  33. protected $functions;
  34. protected $globals;
  35. protected $runtimeInitialized;
  36. protected $loadedTemplates;
  37. protected $strictVariables;
  38. protected $unaryOperators;
  39. protected $binaryOperators;
  40. protected $templateClassPrefix = '__TwigTemplate_';
  41. protected $functionCallbacks;
  42. protected $filterCallbacks;
  43. /**
  44. * Constructor.
  45. *
  46. * Available options:
  47. *
  48. * * debug: When set to `true`, the generated templates have a __toString()
  49. * method that you can use to display the generated nodes (default to
  50. * false).
  51. *
  52. * * charset: The charset used by the templates (default to utf-8).
  53. *
  54. * * base_template_class: The base template class to use for generated
  55. * templates (default to Twig_Template).
  56. *
  57. * * cache: An absolute path where to store the compiled templates, or
  58. * false to disable compilation cache (default)
  59. *
  60. * * auto_reload: Whether to reload the template is the original source changed.
  61. * If you don't provide the auto_reload option, it will be
  62. * determined automatically base on the debug value.
  63. *
  64. * * strict_variables: Whether to ignore invalid variables in templates
  65. * (default to false).
  66. *
  67. * * autoescape: Whether to enable auto-escaping (default to true);
  68. *
  69. * * optimizations: A flag that indicates which optimizations to apply
  70. * (default to -1 which means that all optimizations are enabled;
  71. * set it to 0 to disable)
  72. *
  73. * @param Twig_LoaderInterface $loader A Twig_LoaderInterface instance
  74. * @param array $options An array of options
  75. */
  76. public function __construct(Twig_LoaderInterface $loader = null, $options = array())
  77. {
  78. if (null !== $loader) {
  79. $this->setLoader($loader);
  80. }
  81. $options = array_merge(array(
  82. 'debug' => false,
  83. 'charset' => 'UTF-8',
  84. 'base_template_class' => 'Twig_Template',
  85. 'strict_variables' => false,
  86. 'autoescape' => true,
  87. 'cache' => false,
  88. 'auto_reload' => null,
  89. 'optimizations' => -1,
  90. ), $options);
  91. $this->debug = (bool) $options['debug'];
  92. $this->charset = $options['charset'];
  93. $this->baseTemplateClass = $options['base_template_class'];
  94. $this->autoReload = null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload'];
  95. $this->extensions = array(
  96. 'core' => new Twig_Extension_Core(),
  97. 'escaper' => new Twig_Extension_Escaper((bool) $options['autoescape']),
  98. 'optimizer' => new Twig_Extension_Optimizer($options['optimizations']),
  99. );
  100. $this->strictVariables = (bool) $options['strict_variables'];
  101. $this->runtimeInitialized = false;
  102. $this->setCache($options['cache']);
  103. $this->functionCallbacks = array();
  104. $this->filterCallbacks = array();
  105. }
  106. /**
  107. * Gets the base template class for compiled templates.
  108. *
  109. * @return string The base template class name
  110. */
  111. public function getBaseTemplateClass()
  112. {
  113. return $this->baseTemplateClass;
  114. }
  115. /**
  116. * Sets the base template class for compiled templates.
  117. *
  118. * @param string $class The base template class name
  119. */
  120. public function setBaseTemplateClass($class)
  121. {
  122. $this->baseTemplateClass = $class;
  123. }
  124. /**
  125. * Enables debugging mode.
  126. */
  127. public function enableDebug()
  128. {
  129. $this->debug = true;
  130. }
  131. /**
  132. * Disables debugging mode.
  133. */
  134. public function disableDebug()
  135. {
  136. $this->debug = false;
  137. }
  138. /**
  139. * Checks if debug mode is enabled.
  140. *
  141. * @return Boolean true if debug mode is enabled, false otherwise
  142. */
  143. public function isDebug()
  144. {
  145. return $this->debug;
  146. }
  147. /**
  148. * Enables the auto_reload option.
  149. */
  150. public function enableAutoReload()
  151. {
  152. $this->autoReload = true;
  153. }
  154. /**
  155. * Disables the auto_reload option.
  156. */
  157. public function disableAutoReload()
  158. {
  159. $this->autoReload = false;
  160. }
  161. /**
  162. * Checks if the auto_reload option is enabled.
  163. *
  164. * @return Boolean true if auto_reload is enabled, false otherwise
  165. */
  166. public function isAutoReload()
  167. {
  168. return $this->autoReload;
  169. }
  170. /**
  171. * Enables the strict_variables option.
  172. */
  173. public function enableStrictVariables()
  174. {
  175. $this->strictVariables = true;
  176. }
  177. /**
  178. * Disables the strict_variables option.
  179. */
  180. public function disableStrictVariables()
  181. {
  182. $this->strictVariables = false;
  183. }
  184. /**
  185. * Checks if the strict_variables option is enabled.
  186. *
  187. * @return Boolean true if strict_variables is enabled, false otherwise
  188. */
  189. public function isStrictVariables()
  190. {
  191. return $this->strictVariables;
  192. }
  193. /**
  194. * Gets the cache directory or false if cache is disabled.
  195. *
  196. * @return string|false
  197. */
  198. public function getCache()
  199. {
  200. return $this->cache;
  201. }
  202. /**
  203. * Sets the cache directory or false if cache is disabled.
  204. *
  205. * @param string|false $cache The absolute path to the compiled templates,
  206. * or false to disable cache
  207. */
  208. public function setCache($cache)
  209. {
  210. $this->cache = $cache ? $cache : false;
  211. }
  212. /**
  213. * Gets the cache filename for a given template.
  214. *
  215. * @param string $name The template name
  216. *
  217. * @return string The cache file name
  218. */
  219. public function getCacheFilename($name)
  220. {
  221. if (false === $this->cache) {
  222. return false;
  223. }
  224. $class = substr($this->getTemplateClass($name), strlen($this->templateClassPrefix));
  225. return $this->getCache().'/'.substr($class, 0, 2).'/'.substr($class, 2, 2).'/'.substr($class, 4).'.php';
  226. }
  227. /**
  228. * Gets the template class associated with the given string.
  229. *
  230. * @param string $name The name for which to calculate the template class name
  231. *
  232. * @return string The template class name
  233. */
  234. public function getTemplateClass($name)
  235. {
  236. return $this->templateClassPrefix.md5($this->loader->getCacheKey($name));
  237. }
  238. /**
  239. * Gets the template class prefix.
  240. *
  241. * @return string The template class prefix
  242. */
  243. public function getTemplateClassPrefix()
  244. {
  245. return $this->templateClassPrefix;
  246. }
  247. /**
  248. * Renders a template.
  249. *
  250. * @param string $name The template name
  251. * @param array $context An array of parameters to pass to the template
  252. *
  253. * @return string The rendered template
  254. */
  255. public function render($name, array $context = array())
  256. {
  257. return $this->loadTemplate($name)->render($context);
  258. }
  259. /**
  260. * Loads a template by name.
  261. *
  262. * @param string $name The template name
  263. *
  264. * @return Twig_TemplateInterface A template instance representing the given template name
  265. */
  266. public function loadTemplate($name)
  267. {
  268. $cls = $this->getTemplateClass($name);
  269. if (isset($this->loadedTemplates[$cls])) {
  270. return $this->loadedTemplates[$cls];
  271. }
  272. if (!class_exists($cls, false)) {
  273. if (false === $cache = $this->getCacheFilename($name)) {
  274. eval('?>'.$this->compileSource($this->loader->getSource($name), $name));
  275. } else {
  276. if (!file_exists($cache) || ($this->isAutoReload() && !$this->loader->isFresh($name, filemtime($cache)))) {
  277. $this->writeCacheFile($cache, $this->compileSource($this->loader->getSource($name), $name));
  278. }
  279. require_once $cache;
  280. }
  281. }
  282. if (!$this->runtimeInitialized) {
  283. $this->initRuntime();
  284. }
  285. return $this->loadedTemplates[$cls] = new $cls($this);
  286. }
  287. /**
  288. * Clears the internal template cache.
  289. */
  290. public function clearTemplateCache()
  291. {
  292. $this->loadedTemplates = array();
  293. }
  294. /**
  295. * Clears the template cache files on the filesystem.
  296. */
  297. public function clearCacheFiles()
  298. {
  299. if (false === $this->cache) {
  300. return;
  301. }
  302. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->cache), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  303. if ($file->isFile()) {
  304. @unlink($file->getPathname());
  305. }
  306. }
  307. }
  308. /**
  309. * Gets the Lexer instance.
  310. *
  311. * @return Twig_LexerInterface A Twig_LexerInterface instance
  312. */
  313. public function getLexer()
  314. {
  315. if (null === $this->lexer) {
  316. $this->lexer = new Twig_Lexer($this);
  317. }
  318. return $this->lexer;
  319. }
  320. /**
  321. * Sets the Lexer instance.
  322. *
  323. * @param Twig_LexerInterface A Twig_LexerInterface instance
  324. */
  325. public function setLexer(Twig_LexerInterface $lexer)
  326. {
  327. $this->lexer = $lexer;
  328. }
  329. /**
  330. * Tokenizes a source code.
  331. *
  332. * @param string $source The template source code
  333. * @param string $name The template name
  334. *
  335. * @return Twig_TokenStream A Twig_TokenStream instance
  336. */
  337. public function tokenize($source, $name = null)
  338. {
  339. return $this->getLexer()->tokenize($source, $name);
  340. }
  341. /**
  342. * Gets the Parser instance.
  343. *
  344. * @return Twig_ParserInterface A Twig_ParserInterface instance
  345. */
  346. public function getParser()
  347. {
  348. if (null === $this->parser) {
  349. $this->parser = new Twig_Parser($this);
  350. }
  351. return $this->parser;
  352. }
  353. /**
  354. * Sets the Parser instance.
  355. *
  356. * @param Twig_ParserInterface A Twig_ParserInterface instance
  357. */
  358. public function setParser(Twig_ParserInterface $parser)
  359. {
  360. $this->parser = $parser;
  361. }
  362. /**
  363. * Parses a token stream.
  364. *
  365. * @param Twig_TokenStream $tokens A Twig_TokenStream instance
  366. *
  367. * @return Twig_Node_Module A Node tree
  368. */
  369. public function parse(Twig_TokenStream $tokens)
  370. {
  371. return $this->getParser()->parse($tokens);
  372. }
  373. /**
  374. * Gets the Compiler instance.
  375. *
  376. * @return Twig_CompilerInterface A Twig_CompilerInterface instance
  377. */
  378. public function getCompiler()
  379. {
  380. if (null === $this->compiler) {
  381. $this->compiler = new Twig_Compiler($this);
  382. }
  383. return $this->compiler;
  384. }
  385. /**
  386. * Sets the Compiler instance.
  387. *
  388. * @param Twig_CompilerInterface $compiler A Twig_CompilerInterface instance
  389. */
  390. public function setCompiler(Twig_CompilerInterface $compiler)
  391. {
  392. $this->compiler = $compiler;
  393. }
  394. /**
  395. * Compiles a Node.
  396. *
  397. * @param Twig_NodeInterface $node A Twig_NodeInterface instance
  398. *
  399. * @return string The compiled PHP source code
  400. */
  401. public function compile(Twig_NodeInterface $node)
  402. {
  403. return $this->getCompiler()->compile($node)->getSource();
  404. }
  405. /**
  406. * Compiles a template source code.
  407. *
  408. * @param string $source The template source code
  409. * @param string $name The template name
  410. *
  411. * @return string The compiled PHP source code
  412. */
  413. public function compileSource($source, $name = null)
  414. {
  415. try {
  416. return $this->compile($this->parse($this->tokenize($source, $name)));
  417. } catch (Twig_Error $e) {
  418. $e->setTemplateFile($name);
  419. throw $e;
  420. } catch (Exception $e) {
  421. throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the compilation of a template ("%s").', $e->getMessage()), -1, $name, $e);
  422. }
  423. }
  424. /**
  425. * Sets the Loader instance.
  426. *
  427. * @param Twig_LoaderInterface $loader A Twig_LoaderInterface instance
  428. */
  429. public function setLoader(Twig_LoaderInterface $loader)
  430. {
  431. $this->loader = $loader;
  432. }
  433. /**
  434. * Gets the Loader instance.
  435. *
  436. * @return Twig_LoaderInterface A Twig_LoaderInterface instance
  437. */
  438. public function getLoader()
  439. {
  440. return $this->loader;
  441. }
  442. /**
  443. * Sets the default template charset.
  444. *
  445. * @param string $charset The default charset
  446. */
  447. public function setCharset($charset)
  448. {
  449. $this->charset = $charset;
  450. }
  451. /**
  452. * Gets the default template charset.
  453. *
  454. * @return string The default charset
  455. */
  456. public function getCharset()
  457. {
  458. return $this->charset;
  459. }
  460. /**
  461. * Initializes the runtime environment.
  462. */
  463. public function initRuntime()
  464. {
  465. $this->runtimeInitialized = true;
  466. foreach ($this->getExtensions() as $extension) {
  467. $extension->initRuntime($this);
  468. }
  469. }
  470. /**
  471. * Returns true if the given extension is registered.
  472. *
  473. * @param string $name The extension name
  474. *
  475. * @return Boolean Whether the extension is registered or not
  476. */
  477. public function hasExtension($name)
  478. {
  479. return isset($this->extensions[$name]);
  480. }
  481. /**
  482. * Gets an extension by name.
  483. *
  484. * @param string $name The extension name
  485. *
  486. * @return Twig_ExtensionInterface A Twig_ExtensionInterface instance
  487. */
  488. public function getExtension($name)
  489. {
  490. if (!isset($this->extensions[$name])) {
  491. throw new Twig_Error_Runtime(sprintf('The "%s" extension is not enabled.', $name));
  492. }
  493. return $this->extensions[$name];
  494. }
  495. /**
  496. * Registers an extension.
  497. *
  498. * @param Twig_ExtensionInterface $extension A Twig_ExtensionInterface instance
  499. */
  500. public function addExtension(Twig_ExtensionInterface $extension)
  501. {
  502. $this->extensions[$extension->getName()] = $extension;
  503. }
  504. /**
  505. * Removes an extension by name.
  506. *
  507. * @param string $name The extension name
  508. */
  509. public function removeExtension($name)
  510. {
  511. unset($this->extensions[$name]);
  512. }
  513. /**
  514. * Registers an array of extensions.
  515. *
  516. * @param array $extensions An array of extensions
  517. */
  518. public function setExtensions(array $extensions)
  519. {
  520. foreach ($extensions as $extension) {
  521. $this->addExtension($extension);
  522. }
  523. }
  524. /**
  525. * Returns all registered extensions.
  526. *
  527. * @return array An array of extensions
  528. */
  529. public function getExtensions()
  530. {
  531. return $this->extensions;
  532. }
  533. /**
  534. * Registers a Token Parser.
  535. *
  536. * @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance
  537. */
  538. public function addTokenParser(Twig_TokenParserInterface $parser)
  539. {
  540. if (null === $this->parsers) {
  541. $this->getTokenParsers();
  542. }
  543. $this->parsers->addTokenParser($parser);
  544. }
  545. /**
  546. * Gets the registered Token Parsers.
  547. *
  548. * @return Twig_TokenParserInterface[] An array of Twig_TokenParserInterface instances
  549. */
  550. public function getTokenParsers()
  551. {
  552. if (null === $this->parsers) {
  553. $this->parsers = new Twig_TokenParserBroker;
  554. foreach ($this->getExtensions() as $extension) {
  555. $parsers = $extension->getTokenParsers();
  556. foreach($parsers as $parser) {
  557. if ($parser instanceof Twig_TokenParserInterface) {
  558. $this->parsers->addTokenParser($parser);
  559. } else if ($parser instanceof Twig_TokenParserBrokerInterface) {
  560. $this->parsers->addTokenParserBroker($parser);
  561. } else {
  562. throw new Twig_Error_Runtime('getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances');
  563. }
  564. }
  565. }
  566. }
  567. return $this->parsers;
  568. }
  569. /**
  570. * Registers a Node Visitor.
  571. *
  572. * @param Twig_NodeVisitorInterface $visitor A Twig_NodeVisitorInterface instance
  573. */
  574. public function addNodeVisitor(Twig_NodeVisitorInterface $visitor)
  575. {
  576. if (null === $this->visitors) {
  577. $this->getNodeVisitors();
  578. }
  579. $this->visitors[] = $visitor;
  580. }
  581. /**
  582. * Gets the registered Node Visitors.
  583. *
  584. * @return Twig_NodeVisitorInterface[] An array of Twig_NodeVisitorInterface instances
  585. */
  586. public function getNodeVisitors()
  587. {
  588. if (null === $this->visitors) {
  589. $this->visitors = array();
  590. foreach ($this->getExtensions() as $extension) {
  591. $this->visitors = array_merge($this->visitors, $extension->getNodeVisitors());
  592. }
  593. }
  594. return $this->visitors;
  595. }
  596. /**
  597. * Registers a Filter.
  598. *
  599. * @param string $name The filter name
  600. * @param Twig_FilterInterface $visitor A Twig_FilterInterface instance
  601. */
  602. public function addFilter($name, Twig_FilterInterface $filter)
  603. {
  604. if (null === $this->filters) {
  605. $this->loadFilters();
  606. }
  607. $this->filters[$name] = $filter;
  608. }
  609. /**
  610. * Get a filter by name.
  611. *
  612. * Subclasses may override this method and load filters differently;
  613. * so no list of filters is available.
  614. *
  615. * @param string $name The filter name
  616. *
  617. * @return Twig_Filter|false A Twig_Filter instance or false if the filter does not exists
  618. */
  619. public function getFilter($name)
  620. {
  621. if (null === $this->filters) {
  622. $this->loadFilters();
  623. }
  624. if (isset($this->filters[$name])) {
  625. return $this->filters[$name];
  626. }
  627. foreach ($this->filterCallbacks as $callback) {
  628. if (false !== $filter = call_user_func($callback, $name)) {
  629. return $filter;
  630. }
  631. }
  632. return false;
  633. }
  634. public function registerUndefinedFilterCallback($callable)
  635. {
  636. $this->filterCallbacks[] = $callable;
  637. }
  638. /**
  639. * Gets the registered Filters.
  640. *
  641. * @return Twig_FilterInterface[] An array of Twig_FilterInterface instances
  642. */
  643. protected function loadFilters()
  644. {
  645. $this->filters = array();
  646. foreach ($this->getExtensions() as $extension) {
  647. $this->filters = array_merge($this->filters, $extension->getFilters());
  648. }
  649. }
  650. /**
  651. * Registers a Test.
  652. *
  653. * @param string $name The test name
  654. * @param Twig_TestInterface $visitor A Twig_TestInterface instance
  655. */
  656. public function addTest($name, Twig_TestInterface $test)
  657. {
  658. if (null === $this->tests) {
  659. $this->getTests();
  660. }
  661. $this->tests[$name] = $test;
  662. }
  663. /**
  664. * Gets the registered Tests.
  665. *
  666. * @return Twig_TestInterface[] An array of Twig_TestInterface instances
  667. */
  668. public function getTests()
  669. {
  670. if (null === $this->tests) {
  671. $this->tests = array();
  672. foreach ($this->getExtensions() as $extension) {
  673. $this->tests = array_merge($this->tests, $extension->getTests());
  674. }
  675. }
  676. return $this->tests;
  677. }
  678. /**
  679. * Registers a Function.
  680. *
  681. * @param string $name The function name
  682. * @param Twig_FunctionInterface $function A Twig_FunctionInterface instance
  683. */
  684. public function addFunction($name, Twig_FunctionInterface $function)
  685. {
  686. if (null === $this->functions) {
  687. $this->loadFunctions();
  688. }
  689. $this->functions[$name] = $function;
  690. }
  691. /**
  692. * Get a function by name.
  693. *
  694. * Subclasses may override this method and load functions differently;
  695. * so no list of functions is available.
  696. *
  697. * @param string $name function name
  698. *
  699. * @return Twig_Function|false A Twig_Function instance or false if the function does not exists
  700. */
  701. public function getFunction($name)
  702. {
  703. if (null === $this->functions) {
  704. $this->loadFunctions();
  705. }
  706. if (isset($this->functions[$name])) {
  707. return $this->functions[$name];
  708. }
  709. foreach ($this->functionCallbacks as $callback) {
  710. if (false !== $function = call_user_func($callback, $name)) {
  711. return $function;
  712. }
  713. }
  714. return false;
  715. }
  716. public function registerUndefinedFunctionCallback($callable)
  717. {
  718. $this->functionCallbacks[] = $callable;
  719. }
  720. protected function loadFunctions()
  721. {
  722. $this->functions = array();
  723. foreach ($this->getExtensions() as $extension) {
  724. $this->functions = array_merge($this->functions, $extension->getFunctions());
  725. }
  726. }
  727. /**
  728. * Registers a Global.
  729. *
  730. * @param string $name The global name
  731. * @param mixed $value The global value
  732. */
  733. public function addGlobal($name, $value)
  734. {
  735. if (null === $this->globals) {
  736. $this->getGlobals();
  737. }
  738. $this->globals[$name] = $value;
  739. }
  740. /**
  741. * Gets the registered Globals.
  742. *
  743. * @return array An array of globals
  744. */
  745. public function getGlobals()
  746. {
  747. if (null === $this->globals) {
  748. $this->globals = array();
  749. foreach ($this->getExtensions() as $extension) {
  750. $this->globals = array_merge($this->globals, $extension->getGlobals());
  751. }
  752. }
  753. return $this->globals;
  754. }
  755. /**
  756. * Gets the registered unary Operators.
  757. *
  758. * @return array An array of unary operators
  759. */
  760. public function getUnaryOperators()
  761. {
  762. if (null === $this->unaryOperators) {
  763. $this->initOperators();
  764. }
  765. return $this->unaryOperators;
  766. }
  767. /**
  768. * Gets the registered binary Operators.
  769. *
  770. * @return array An array of binary operators
  771. */
  772. public function getBinaryOperators()
  773. {
  774. if (null === $this->binaryOperators) {
  775. $this->initOperators();
  776. }
  777. return $this->binaryOperators;
  778. }
  779. protected function initOperators()
  780. {
  781. $this->unaryOperators = array();
  782. $this->binaryOperators = array();
  783. foreach ($this->getExtensions() as $extension) {
  784. $operators = $extension->getOperators();
  785. if (!$operators) {
  786. continue;
  787. }
  788. if (2 !== count($operators)) {
  789. throw new InvalidArgumentException(sprintf('"%s::getOperators()" does not return a valid operators array.', get_class($extension)));
  790. }
  791. $this->unaryOperators = array_merge($this->unaryOperators, $operators[0]);
  792. $this->binaryOperators = array_merge($this->binaryOperators, $operators[1]);
  793. }
  794. }
  795. protected function writeCacheFile($file, $content)
  796. {
  797. if (!is_dir(dirname($file))) {
  798. mkdir(dirname($file), 0777, true);
  799. }
  800. $tmpFile = tempnam(dirname($file), basename($file));
  801. if (false !== @file_put_contents($tmpFile, $content)) {
  802. // rename does not work on Win32 before 5.2.6
  803. if (@rename($tmpFile, $file) || (@copy($tmpFile, $file) && unlink($tmpFile))) {
  804. chmod($file, 0644);
  805. return;
  806. }
  807. }
  808. throw new Twig_Error_Runtime(sprintf('Failed to write cache file "%s".', $file));
  809. }
  810. }