12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace Symfony\Component\Templating\Loader;
- use Symfony\Component\Templating\Storage\Storage;
- use Symfony\Component\Templating\Storage\FileStorage;
- use Symfony\Component\Templating\TemplateReferenceInterface;
- class CacheLoader extends Loader
- {
- protected $loader;
- protected $dir;
-
- public function __construct(LoaderInterface $loader, $dir)
- {
- $this->loader = $loader;
- $this->dir = $dir;
- }
-
- public function load(TemplateReferenceInterface $template)
- {
- $key = hash('sha256', $template->getLogicalName());
- $dir = $this->dir.DIRECTORY_SEPARATOR.substr($key, 0, 2);
- $file = substr($key, 2).'.tpl';
- $path = $dir.DIRECTORY_SEPARATOR.$file;
- if (is_file($path)) {
- if (null !== $this->logger) {
- $this->logger->debug('Fetching template from cache.', array('name' => $template->get('name')));
- }
- return new FileStorage($path);
- }
- if (false === $storage = $this->loader->load($template)) {
- return false;
- }
- $content = $storage->getContent();
- if (!is_dir($dir) && !@mkdir($dir, 0777, true) && !is_dir($dir)) {
- throw new \RuntimeException(sprintf('Cache Loader was not able to create directory "%s"', $dir));
- }
- file_put_contents($path, $content);
- if (null !== $this->logger) {
- $this->logger->debug('Storing template in cache.', array('name' => $template->get('name')));
- }
- return new FileStorage($path);
- }
-
- public function isFresh(TemplateReferenceInterface $template, $time)
- {
- return $this->loader->isFresh($template, $time);
- }
- }
|