2011-01-24 10:27:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-24 10:27:34 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Bundle\TwigBundle\CacheWarmer;
|
|
|
|
|
2011-04-26 15:02:24 +01:00
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
|
2011-01-24 10:27:34 +00:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2011-06-07 08:38:25 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
|
2011-01-24 10:27:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-01-24 10:27:34 +00:00
|
|
|
*/
|
2011-04-25 10:44:48 +01:00
|
|
|
class TemplateCacheCacheWarmer implements CacheWarmerInterface
|
2011-01-24 10:27:34 +00:00
|
|
|
{
|
|
|
|
protected $container;
|
2011-04-14 17:23:13 +01:00
|
|
|
protected $warmer;
|
2011-01-24 10:27:34 +00:00
|
|
|
|
2011-03-18 10:23:23 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-06-07 08:38:25 +01:00
|
|
|
* @param ContainerInterface $container The dependency injection container
|
|
|
|
* @param TemplateFinderInterface $finder The template paths cache warmer
|
2011-03-18 10:23:23 +00:00
|
|
|
*/
|
2011-06-07 08:38:25 +01:00
|
|
|
public function __construct(ContainerInterface $container, TemplateFinderInterface $finder)
|
2011-01-24 10:27:34 +00:00
|
|
|
{
|
2011-04-14 17:23:13 +01:00
|
|
|
// We don't inject the Twig environment directly as it depends on the
|
|
|
|
// template locator (via the loader) which might be a cached one.
|
|
|
|
// The cached template locator is available once the TemplatePathsCacheWarmer
|
|
|
|
// has been warmed up
|
2011-01-24 10:27:34 +00:00
|
|
|
$this->container = $container;
|
2011-04-14 17:23:13 +01:00
|
|
|
$this->finder = $finder;
|
2011-01-24 10:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Warms up the cache.
|
|
|
|
*
|
|
|
|
* @param string $cacheDir The cache directory
|
|
|
|
*/
|
|
|
|
public function warmUp($cacheDir)
|
|
|
|
{
|
|
|
|
$twig = $this->container->get('twig');
|
2011-03-18 10:23:23 +00:00
|
|
|
|
2011-04-14 17:23:13 +01:00
|
|
|
foreach ($this->finder->findAllTemplates() as $template) {
|
2011-06-17 14:03:38 +01:00
|
|
|
if ('twig' !== $template->get('engine')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2011-03-28 21:04:39 +01:00
|
|
|
$twig->loadTemplate($template);
|
2011-06-17 14:03:38 +01:00
|
|
|
} catch (\Twig_Error $e) {
|
|
|
|
// problem during compilation, give up
|
2011-03-28 21:04:39 +01:00
|
|
|
}
|
2011-03-18 10:23:23 +00:00
|
|
|
}
|
2011-01-24 10:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether this warmer is optional or not.
|
|
|
|
*
|
|
|
|
* @return Boolean always true
|
|
|
|
*/
|
|
|
|
public function isOptional()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|