This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php
2011-03-06 12:40:06 +01:00

63 lines
1.6 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\TwigBundle\CacheWarmer;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Generates the Twig cache for all templates.
*
* This warmer must be registered after TemplatePathsCacheWarmer,
* as the Twig loader will need the cache generated by it.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TemplateCacheCacheWarmer extends CacheWarmer
{
protected $container;
public function __construct(ContainerInterface $container)
{
// we don't inject the Twig environment directly as it needs
// the loader, which is a cached one, and the cache is not
// yet available when this instance is created (the
// TemplateCacheCacheWarmer has not been run yet).
$this->container = $container;
}
/**
* Warms up the cache.
*
* @param string $cacheDir The cache directory
*/
public function warmUp($cacheDir)
{
$templates = include $cacheDir.'/templates.php';
$twig = $this->container->get('twig');
foreach (array_keys($templates) as $template) {
$twig->loadTemplate($template);
}
}
/**
* Checks whether this warmer is optional or not.
*
* @return Boolean always true
*/
public function isOptional()
{
return true;
}
}