setLoader($loader); } $this->setLexer(null !== $lexer ? $lexer : new Twig_Lexer()); $this->setParser(null !== $parser ? $parser : new Twig_Parser()); $this->setCompiler(null !== $compiler ? $compiler : new Twig_Compiler()); $this->debug = isset($options['debug']) ? (bool) $options['debug'] : false; $this->charset = isset($options['charset']) ? $options['charset'] : 'UTF-8'; $this->baseTemplateClass = isset($options['base_template_class']) ? $options['base_template_class'] : 'Twig_Template'; $this->autoReload = isset($options['auto_reload']) ? (bool) $options['auto_reload'] : $this->debug; $this->extensions = array('core' => new Twig_Extension_Core()); $this->strictVariables = isset($options['strict_variables']) ? (bool) $options['strict_variables'] : false; $this->runtimeInitialized = false; if (isset($options['cache']) && $options['cache']) { $this->setCache($options['cache']); } } public function getBaseTemplateClass() { return $this->baseTemplateClass; } public function setBaseTemplateClass($class) { $this->baseTemplateClass = $class; } public function enableDebug() { $this->debug = true; } public function disableDebug() { $this->debug = false; } public function isDebug() { return $this->debug; } public function isAutoReload() { return $this->autoReload; } public function setAutoReload($autoReload) { $this->autoReload = (Boolean) $autoReload; } public function enableStrictVariables() { $this->strictVariables = true; } public function disableStrictVariables() { $this->strictVariables = false; } public function isStrictVariables() { return $this->strictVariables; } public function getCache() { return $this->cache; } public function setCache($cache) { $this->cache = $cache; if ($this->cache && !is_dir($this->cache)) { mkdir($this->cache, 0777, true); } } public function getCacheFilename($name) { return $this->getCache() ? $this->getCache().'/'.$this->getTemplateClass($name).'.php' : false; } /** * Gets the template class associated with the given string. * * @param string $name The name for which to calculate the template class name * * @return string The template class name */ public function getTemplateClass($name) { return '__TwigTemplate_'.md5($this->loader->getCacheKey($name)); } /** * Loads a template by name. * * @param string $name The template name * * @return Twig_TemplateInterface A template instance representing the given template name */ public function loadTemplate($name) { $cls = $this->getTemplateClass($name); if (isset($this->loadedTemplates[$cls])) { return $this->loadedTemplates[$cls]; } if (!class_exists($cls, false)) { if (false === $cache = $this->getCacheFilename($name)) { eval('?>'.$this->compileSource($this->loader->getSource($name), $name)); } else { if (!file_exists($cache) || ($this->isAutoReload() && !$this->loader->isFresh($name, filemtime($cache)))) { $this->writeCacheFile($cache, $this->compileSource($this->loader->getSource($name), $name)); } require_once $cache; } } if (!$this->runtimeInitialized) { $this->initRuntime(); } return $this->loadedTemplates[$cls] = new $cls($this); } public function clearTemplateCache() { $this->loadedTemplates = array(); } public function getLexer() { return $this->lexer; } public function setLexer(Twig_LexerInterface $lexer) { $this->lexer = $lexer; $lexer->setEnvironment($this); } public function tokenize($source, $name) { return $this->getLexer()->tokenize($source, $name); } public function getParser() { return $this->parser; } public function setParser(Twig_ParserInterface $parser) { $this->parser = $parser; $parser->setEnvironment($this); } public function parse(Twig_TokenStream $tokens) { return $this->getParser()->parse($tokens); } public function getCompiler() { return $this->compiler; } public function setCompiler(Twig_CompilerInterface $compiler) { $this->compiler = $compiler; $compiler->setEnvironment($this); } public function compile(Twig_NodeInterface $node) { return $this->getCompiler()->compile($node)->getSource(); } public function compileSource($source, $name) { return $this->compile($this->parse($this->tokenize($source, $name))); } public function setLoader(Twig_LoaderInterface $loader) { $this->loader = $loader; } public function getLoader() { return $this->loader; } public function setCharset($charset) { $this->charset = $charset; } public function getCharset() { return $this->charset; } public function initRuntime() { $this->runtimeInitialized = true; foreach ($this->getExtensions() as $extension) { $extension->initRuntime($this); } } public function hasExtension($name) { return isset($this->extensions[$name]); } public function getExtension($name) { if (!isset($this->extensions[$name])) { throw new Twig_Error_Runtime(sprintf('The "%s" extension is not enabled.', $name)); } return $this->extensions[$name]; } public function addExtension(Twig_ExtensionInterface $extension) { $this->extensions[$extension->getName()] = $extension; } public function removeExtension($name) { unset($this->extensions[$name]); } public function setExtensions(array $extensions) { foreach ($extensions as $extension) { $this->addExtension($extension); } } public function getExtensions() { return $this->extensions; } public function addTokenParser(Twig_TokenParserInterface $parser) { if (null === $this->parsers) { $this->getTokenParsers(); } $this->parsers->addTokenParser($parser); } public function getTokenParsers() { if (null === $this->parsers) { $this->parsers = new Twig_TokenParserBroker; foreach ($this->getExtensions() as $extension) { $parsers = $extension->getTokenParsers(); foreach($parsers as $parser) { if ($parser instanceof Twig_TokenParserInterface) { $this->parsers->addTokenParser($parser); } else if ($parser instanceof Twig_TokenParserBrokerInterface) { $this->parsers->addTokenParserBroker($parser); } else { throw new Twig_Error_Runtime('getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances'); } } } } return $this->parsers; } public function addNodeVisitor(Twig_NodeVisitorInterface $visitor) { if (null === $this->visitors) { $this->getNodeVisitors(); } $this->visitors[] = $visitor; } public function getNodeVisitors() { if (null === $this->visitors) { $this->visitors = array(); foreach ($this->getExtensions() as $extension) { $this->visitors = array_merge($this->visitors, $extension->getNodeVisitors()); } } return $this->visitors; } public function addFilter($name, Twig_FilterInterface $filter) { if (null === $this->filters) { $this->getFilters(); } $this->filters[$name] = $filter; } public function getFilters() { if (null === $this->filters) { $this->filters = array(); foreach ($this->getExtensions() as $extension) { $this->filters = array_merge($this->filters, $extension->getFilters()); } } return $this->filters; } public function addTest($name, Twig_TestInterface $test) { if (null === $this->tests) { $this->getTests(); } $this->tests[$name] = $test; } public function getTests() { if (null === $this->tests) { $this->tests = array(); foreach ($this->getExtensions() as $extension) { $this->tests = array_merge($this->tests, $extension->getTests()); } } return $this->tests; } protected function writeCacheFile($file, $content) { $tmpFile = tempnam(dirname($file), basename($file)); if (false !== @file_put_contents($tmpFile, $content)) { // rename does not work on Win32 before 5.2.6 if (@rename($tmpFile, $file) || (@copy($tmpFile, $file) && unlink($tmpFile))) { chmod($file, 0644); return; } } throw new Twig_Error_Runtime(sprintf('Failed to write cache file "%s".', $file)); } }