* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ /** * CacheLoader is a loader that caches other loaders responses * on the filesystem. * * This cache only caches on disk to allow PHP accelerators to cache the opcodes. * All other mecanism would imply the use of `eval()`. * * @package symfony * @subpackage templating * @author Fabien Potencier */ class CacheLoader extends Loader { protected $loader; protected $dir; /** * Constructor. * * @param Loader $loader A Loader instance * @param string $dir The directory where to store the cache files */ public function __construct(Loader $loader, $dir) { $this->loader = $loader; $this->dir = $dir; if (!file_exists($dir)) { mkdir($dir, 0777, true); } parent::__construct(); } /** * Loads a template. * * @param string $template The logical template name * @param array $options An array of options * * @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise */ public function load($template, array $options = array()) { $options = $this->mergeDefaultOptions($options); $path = $this->dir.DIRECTORY_SEPARATOR.md5($template.serialize($options)).'.tpl'; if ($this->loader instanceof CompilableLoaderInterface) { $options['renderer'] = 'php'; } if (file_exists($path)) { if (null !== $this->debugger) { $this->debugger->log(sprintf('Fetching template "%s" from cache', $template)); } return new FileStorage($path, $options['renderer']); } if (false === $storage = $this->loader->load($template, $options)) { return false; } $content = $storage->getContent(); if ($this->loader instanceof CompilableLoaderInterface) { $content = $this->loader->compile($content); } file_put_contents($path, $content); if (null !== $this->debugger) { $this->debugger->log(sprintf('Storing template "%s" in cache', $template)); } return new FileStorage($path, $options['renderer']); } }