12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace Symfony\Bundle\TwigBundle\Loader;
- use Symfony\Component\Templating\Engine;
- use Symfony\Component\Templating\Storage\Storage;
- use Symfony\Component\Templating\Storage\FileStorage;
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- class Loader implements \Twig_LoaderInterface
- {
- protected $engine;
- public function setEngine(Engine $engine)
- {
- $this->engine = $engine;
- }
- /**
- * Gets the source code of a template, given its name.
- *
- * @param string $name string The name of the template to load
- *
- * @return string The template source code
- */
- public function getSource($name)
- {
- if ($name instanceof Storage) {
- return $name->getContent();
- }
- list($name, $options) = $this->engine->splitTemplateName($name);
- if ('twig' !== $options['renderer']) {
- throw new \LogicException(sprintf('A "%s" template cannot extend a "Twig" template.', $options['renderer']));
- }
- $template = $this->engine->getLoader()->load($name, $options);
- if (false === $template) {
- throw new \InvalidArgumentException(sprintf('The template "%s" does not exist (renderer: %s).', $name, $options['renderer']));
- }
- return $template->getContent();
- }
- /**
- * Gets the cache key to use for the cache for a given template name.
- *
- * @param string $name string The name of the template to load
- *
- * @return string The cache key
- */
- public function getCacheKey($name)
- {
- if ($name instanceof Storage) {
- return (string) $name;
- }
- list($name, $options) = $this->engine->splitTemplateName($name);
- return $name.'_'.serialize($options);
- }
- /**
- * Returns true if the template is still fresh.
- *
- * @param string $name The template name
- * @param timestamp $time The last modification time of the cached template
- */
- public function isFresh($name, $time)
- {
- if ($name instanceof Storage) {
- if ($name instanceof FileStorage) {
- return filemtime((string) $name) < $time;
- }
- return false;
- }
- list($name, $options) = $this->engine->splitTemplateName($name);
- return $this->engine->getLoader()->isFresh($name, $options, $time);
- }
- }
|