vendor/symfony/src/Symfony/Component/Templating/Loader/CacheLoader.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\Templating\Loader;
       
    13 
       
    14 use Symfony\Component\Templating\Storage\Storage;
       
    15 use Symfony\Component\Templating\Storage\FileStorage;
       
    16 use Symfony\Component\Templating\TemplateReferenceInterface;
       
    17 
       
    18 /**
       
    19  * CacheLoader is a loader that caches other loaders responses
       
    20  * on the filesystem.
       
    21  *
       
    22  * This cache only caches on disk to allow PHP accelerators to cache the opcodes.
       
    23  * All other mechanism would imply the use of `eval()`.
       
    24  *
       
    25  * @author Fabien Potencier <fabien@symfony.com>
       
    26  */
       
    27 class CacheLoader extends Loader
       
    28 {
       
    29     protected $loader;
       
    30     protected $dir;
       
    31 
       
    32     /**
       
    33      * Constructor.
       
    34      *
       
    35      * @param LoaderInterface $loader A Loader instance
       
    36      * @param string          $dir    The directory where to store the cache files
       
    37      */
       
    38     public function __construct(LoaderInterface $loader, $dir)
       
    39     {
       
    40         $this->loader = $loader;
       
    41         $this->dir = $dir;
       
    42     }
       
    43 
       
    44     /**
       
    45      * Loads a template.
       
    46      *
       
    47      * @param TemplateReferenceInterface $template A template
       
    48      *
       
    49      * @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise
       
    50      */
       
    51     public function load(TemplateReferenceInterface $template)
       
    52     {
       
    53         $key = md5($template->getLogicalName());
       
    54         $dir = $this->dir.DIRECTORY_SEPARATOR.substr($key, 0, 2);
       
    55         $file = substr($key, 2).'.tpl';
       
    56         $path = $dir.DIRECTORY_SEPARATOR.$file;
       
    57 
       
    58         if (file_exists($path)) {
       
    59             if (null !== $this->debugger) {
       
    60                 $this->debugger->log(sprintf('Fetching template "%s" from cache', $template->get('name')));
       
    61             }
       
    62 
       
    63             return new FileStorage($path);
       
    64         }
       
    65 
       
    66         if (false === $storage = $this->loader->load($template)) {
       
    67             return false;
       
    68         }
       
    69 
       
    70         $content = $storage->getContent();
       
    71 
       
    72         if (!file_exists($dir)) {
       
    73             mkdir($dir, 0777, true);
       
    74         }
       
    75 
       
    76         file_put_contents($path, $content);
       
    77 
       
    78         if (null !== $this->debugger) {
       
    79             $this->debugger->log(sprintf('Storing template "%s" in cache', $template->get('name')));
       
    80         }
       
    81 
       
    82         return new FileStorage($path);
       
    83     }
       
    84 
       
    85     /**
       
    86      * Returns true if the template is still fresh.
       
    87      *
       
    88      * @param TemplateReferenceInterface $template A template
       
    89      * @param integer                    $time     The last modification time of the cached template (timestamp)
       
    90      */
       
    91     public function isFresh(TemplateReferenceInterface $template, $time)
       
    92     {
       
    93         return $this->loader->isFresh($template, $time);
       
    94     }
       
    95 }