Revision 1852
Added by darkviper almost 13 years ago
| Environment.php | ||
|---|---|---|
| 17 | 17 |
*/ |
| 18 | 18 |
class Twig_Environment |
| 19 | 19 |
{
|
| 20 |
const VERSION = '1.7.0';
|
|
| 20 |
const VERSION = '1.11.1';
|
|
| 21 | 21 |
|
| 22 | 22 |
protected $charset; |
| 23 | 23 |
protected $loader; |
| ... | ... | |
| 50 | 50 |
* |
| 51 | 51 |
* Available options: |
| 52 | 52 |
* |
| 53 |
* * debug: When set to `true`, the generated templates have a __toString() |
|
| 54 |
* method that you can use to display the generated nodes (default to |
|
| 55 |
* false). |
|
| 53 |
* * debug: When set to true, it automatically set "auto_reload" to true as |
|
| 54 |
* well (default to false). |
|
| 56 | 55 |
* |
| 57 | 56 |
* * charset: The charset used by the templates (default to utf-8). |
| 58 | 57 |
* |
| ... | ... | |
| 60 | 59 |
* templates (default to Twig_Template). |
| 61 | 60 |
* |
| 62 | 61 |
* * cache: An absolute path where to store the compiled templates, or |
| 63 |
* false to disable compilation cache (default) |
|
| 62 |
* false to disable compilation cache (default).
|
|
| 64 | 63 |
* |
| 65 | 64 |
* * auto_reload: Whether to reload the template is the original source changed. |
| 66 | 65 |
* If you don't provide the auto_reload option, it will be |
| ... | ... | |
| 69 | 68 |
* * strict_variables: Whether to ignore invalid variables in templates |
| 70 | 69 |
* (default to false). |
| 71 | 70 |
* |
| 72 |
* * autoescape: Whether to enable auto-escaping (default to true); |
|
| 71 |
* * autoescape: Whether to enable auto-escaping (default to html): |
|
| 72 |
* * false: disable auto-escaping |
|
| 73 |
* * true: equivalent to html |
|
| 74 |
* * html, js: set the autoescaping to one of the supported strategies |
|
| 75 |
* * PHP callback: a PHP callback that returns an escaping strategy based on the template "filename" |
|
| 73 | 76 |
* |
| 74 | 77 |
* * optimizations: A flag that indicates which optimizations to apply |
| 75 | 78 |
* (default to -1 which means that all optimizations are enabled; |
| 76 |
* set it to 0 to disable) |
|
| 79 |
* set it to 0 to disable).
|
|
| 77 | 80 |
* |
| 78 |
* @param Twig_LoaderInterface $loader A Twig_LoaderInterface instance
|
|
| 79 |
* @param array $options An array of options
|
|
| 81 |
* @param Twig_LoaderInterface $loader A Twig_LoaderInterface instance |
|
| 82 |
* @param array $options An array of options |
|
| 80 | 83 |
*/ |
| 81 | 84 |
public function __construct(Twig_LoaderInterface $loader = null, $options = array()) |
| 82 | 85 |
{
|
| ... | ... | |
| 89 | 92 |
'charset' => 'UTF-8', |
| 90 | 93 |
'base_template_class' => 'Twig_Template', |
| 91 | 94 |
'strict_variables' => false, |
| 92 |
'autoescape' => true,
|
|
| 95 |
'autoescape' => 'html',
|
|
| 93 | 96 |
'cache' => false, |
| 94 | 97 |
'auto_reload' => null, |
| 95 | 98 |
'optimizations' => -1, |
| ... | ... | |
| 101 | 104 |
$this->autoReload = null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload']; |
| 102 | 105 |
$this->extensions = array( |
| 103 | 106 |
'core' => new Twig_Extension_Core(), |
| 104 |
'escaper' => new Twig_Extension_Escaper((bool) $options['autoescape']),
|
|
| 107 |
'escaper' => new Twig_Extension_Escaper($options['autoescape']), |
|
| 105 | 108 |
'optimizer' => new Twig_Extension_Optimizer($options['optimizations']), |
| 106 | 109 |
); |
| 107 | 110 |
$this->strictVariables = (bool) $options['strict_variables']; |
| ... | ... | |
| 109 | 112 |
$this->setCache($options['cache']); |
| 110 | 113 |
$this->functionCallbacks = array(); |
| 111 | 114 |
$this->filterCallbacks = array(); |
| 115 |
$this->staging = array( |
|
| 116 |
'functions' => array(), |
|
| 117 |
'filters' => array(), |
|
| 118 |
'tests' => array(), |
|
| 119 |
'token_parsers' => array(), |
|
| 120 |
'visitors' => array(), |
|
| 121 |
'globals' => array(), |
|
| 122 |
); |
|
| 112 | 123 |
} |
| 113 | 124 |
|
| 114 | 125 |
/** |
| ... | ... | |
| 251 | 262 |
/** |
| 252 | 263 |
* Gets the template class associated with the given string. |
| 253 | 264 |
* |
| 254 |
* @param string $name The name for which to calculate the template class name |
|
| 265 |
* @param string $name The name for which to calculate the template class name |
|
| 266 |
* @param integer $index The index if it is an embedded template |
|
| 255 | 267 |
* |
| 256 | 268 |
* @return string The template class name |
| 257 | 269 |
*/ |
| 258 |
public function getTemplateClass($name) |
|
| 270 |
public function getTemplateClass($name, $index = null)
|
|
| 259 | 271 |
{
|
| 260 |
return $this->templateClassPrefix.md5($this->loader->getCacheKey($name)); |
|
| 272 |
return $this->templateClassPrefix.md5($this->loader->getCacheKey($name)).(null === $index ? '' : '_'.$index);
|
|
| 261 | 273 |
} |
| 262 | 274 |
|
| 263 | 275 |
/** |
| ... | ... | |
| 297 | 309 |
/** |
| 298 | 310 |
* Loads a template by name. |
| 299 | 311 |
* |
| 300 |
* @param string $name The template name |
|
| 312 |
* @param string $name The template name |
|
| 313 |
* @param integer $index The index if it is an embedded template |
|
| 301 | 314 |
* |
| 302 | 315 |
* @return Twig_TemplateInterface A template instance representing the given template name |
| 303 | 316 |
*/ |
| 304 |
public function loadTemplate($name) |
|
| 317 |
public function loadTemplate($name, $index = null)
|
|
| 305 | 318 |
{
|
| 306 |
$cls = $this->getTemplateClass($name); |
|
| 319 |
$cls = $this->getTemplateClass($name, $index);
|
|
| 307 | 320 |
|
| 308 | 321 |
if (isset($this->loadedTemplates[$cls])) {
|
| 309 | 322 |
return $this->loadedTemplates[$cls]; |
| ... | ... | |
| 692 | 705 |
|
| 693 | 706 |
foreach ($this->getExtensions() as $extension) {
|
| 694 | 707 |
$parsers = $extension->getTokenParsers(); |
| 695 |
foreach($parsers as $parser) {
|
|
| 708 |
foreach ($parsers as $parser) {
|
|
| 696 | 709 |
if ($parser instanceof Twig_TokenParserInterface) {
|
| 697 | 710 |
$this->parsers->addTokenParser($parser); |
| 698 | 711 |
} elseif ($parser instanceof Twig_TokenParserBrokerInterface) {
|
| 699 | 712 |
$this->parsers->addTokenParserBroker($parser); |
| 700 | 713 |
} else {
|
| 701 |
throw new Twig_Error_Runtime('getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances');
|
|
| 714 |
throw new LogicException('getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances');
|
|
| 702 | 715 |
} |
| 703 | 716 |
} |
| 704 | 717 |
} |
| ... | ... | |
| 745 | 758 |
public function getNodeVisitors() |
| 746 | 759 |
{
|
| 747 | 760 |
if (null === $this->visitors) {
|
| 748 |
$this->visitors = isset($this->staging['visitors']) ? $this->staging['visitors'] : array(); |
|
| 749 | 761 |
foreach ($this->getExtensions() as $extension) {
|
| 750 |
$this->visitors = array_merge($this->visitors, $extension->getNodeVisitors()); |
|
| 762 |
foreach ($extension->getNodeVisitors() as $visitor) {
|
|
| 763 |
$this->addNodeVisitor($visitor); |
|
| 764 |
} |
|
| 751 | 765 |
} |
| 766 |
|
|
| 767 |
$this->visitors = $this->staging['visitors']; |
|
| 752 | 768 |
} |
| 753 | 769 |
|
| 754 | 770 |
return $this->visitors; |
| ... | ... | |
| 825 | 841 |
public function getFilters() |
| 826 | 842 |
{
|
| 827 | 843 |
if (null === $this->filters) {
|
| 828 |
$this->filters = isset($this->staging['filters']) ? $this->staging['filters'] : array(); |
|
| 829 | 844 |
foreach ($this->getExtensions() as $extension) {
|
| 830 |
$this->filters = array_merge($this->filters, $extension->getFilters()); |
|
| 845 |
foreach ($extension->getFilters() as $name => $filter) {
|
|
| 846 |
$this->addFilter($name, $filter); |
|
| 847 |
} |
|
| 831 | 848 |
} |
| 849 |
|
|
| 850 |
$this->filters = $this->staging['filters']; |
|
| 832 | 851 |
} |
| 833 | 852 |
|
| 834 | 853 |
return $this->filters; |
| ... | ... | |
| 854 | 873 |
public function getTests() |
| 855 | 874 |
{
|
| 856 | 875 |
if (null === $this->tests) {
|
| 857 |
$this->tests = isset($this->staging['tests']) ? $this->staging['tests'] : array(); |
|
| 858 | 876 |
foreach ($this->getExtensions() as $extension) {
|
| 859 |
$this->tests = array_merge($this->tests, $extension->getTests()); |
|
| 877 |
foreach ($extension->getTests() as $name => $test) {
|
|
| 878 |
$this->addTest($name, $test); |
|
| 879 |
} |
|
| 860 | 880 |
} |
| 881 |
|
|
| 882 |
$this->tests = $this->staging['tests']; |
|
| 861 | 883 |
} |
| 862 | 884 |
|
| 863 | 885 |
return $this->tests; |
| ... | ... | |
| 934 | 956 |
public function getFunctions() |
| 935 | 957 |
{
|
| 936 | 958 |
if (null === $this->functions) {
|
| 937 |
$this->functions = isset($this->staging['functions']) ? $this->staging['functions'] : array(); |
|
| 938 | 959 |
foreach ($this->getExtensions() as $extension) {
|
| 939 |
$this->functions = array_merge($this->functions, $extension->getFunctions()); |
|
| 960 |
foreach ($extension->getFunctions() as $name => $function) {
|
|
| 961 |
$this->addFunction($name, $function); |
|
| 962 |
} |
|
| 940 | 963 |
} |
| 964 |
|
|
| 965 |
$this->functions = $this->staging['functions']; |
|
| 941 | 966 |
} |
| 942 | 967 |
|
| 943 | 968 |
return $this->functions; |
| ... | ... | |
| 1069 | 1094 |
if (false !== @file_put_contents($tmpFile, $content)) {
|
| 1070 | 1095 |
// rename does not work on Win32 before 5.2.6 |
| 1071 | 1096 |
if (@rename($tmpFile, $file) || (@copy($tmpFile, $file) && unlink($tmpFile))) {
|
| 1072 |
@chmod($file, 0644);
|
|
| 1097 |
@chmod($file, 0666 & ~umask());
|
|
| 1073 | 1098 |
|
| 1074 | 1099 |
return; |
| 1075 | 1100 |
} |
| 1076 | 1101 |
} |
| 1077 | 1102 |
|
| 1078 |
throw new Twig_Error_Runtime(sprintf('Failed to write cache file "%s".', $file));
|
|
| 1103 |
throw new RuntimeException(sprintf('Failed to write cache file "%s".', $file));
|
|
| 1079 | 1104 |
} |
| 1080 | 1105 |
} |
Also available in: Unified diff
updated Twig template engine to stable version 1.11.1 step2