Skip to content

Commit

Permalink
[TwigBundle] Fix the cache warmer
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Mar 18, 2011
1 parent cead49f commit 7f52346
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
Expand Up @@ -49,7 +49,7 @@ public function __construct(KernelInterface $kernel, TemplateNameParser $parser,
* @param string $cacheDir The cache directory
*/
public function warmUp($cacheDir)
{
{
$templates = array();

foreach ($this->kernel->getBundles() as $name => $bundle) {
Expand Down
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Finder\Finder;

/**
* Generates the Twig cache for all templates.
Expand All @@ -24,15 +25,28 @@
*/
class TemplateCacheCacheWarmer extends CacheWarmer
{
const TEMPLATES_PATH_IN_BUNDLE = '/Resources/views';

protected $container;
protected $parser;
protected $kernel;

public function __construct(ContainerInterface $container)
/**
* Constructor.
*
* @param ContainerInterface $container The dependency injection container
* @param string $rootDir The directory where global templates can be stored
*/
public function __construct(ContainerInterface $container, $rootDir)
{
// 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;
$this->parser = $container->get('templating.name_parser');
$this->kernel = $container->get('kernel');
$this->rootDir = $rootDir;
}

/**
Expand All @@ -42,11 +56,16 @@ public function __construct(ContainerInterface $container)
*/
public function warmUp($cacheDir)
{
$templates = include $cacheDir.'/templates.php';

$twig = $this->container->get('twig');
foreach (array_keys($templates) as $template) {
$twig->loadTemplate($template);

foreach ($this->kernel->getBundles() as $name => $bundle) {
foreach ($this->findTemplatesIn($bundle->getPath().self::TEMPLATES_PATH_IN_BUNDLE, $name) as $file) {
$twig->loadTemplate($file);
}
}

foreach ($this->findTemplatesIn($this->rootDir) as $file) {
$twig->loadTemplate($file);
}
}

Expand All @@ -59,4 +78,32 @@ public function isOptional()
{
return true;
}

/**
* Find templates in the given directory
*
* @param string $dir The folder where to look for templates
* @param string $bundle The name of the bundle (null when out of a bundle)
*
* @return array An array of template paths
*/
protected function findTemplatesIn($dir, $bundle = null)
{
$templates = array();

if (is_dir($dir)) {
$finder = new Finder();
foreach ($finder->files()->followLinks()->in($dir) as $file) {
$template = $this->parser->parseFromFilename($file->getRelativePathname());
if (false !== $template && 'twig' == $template->get('engine')) {
if (null !== $bundle) {
$template->set('bundle', $bundle);
}
$templates[] = $template;
}
}
}

return $templates;
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Expand Up @@ -19,6 +19,7 @@

<service id="templating.cache_warmer.templates_cache" class="%templating.cache_warmer.templates_cache.class%" public="false">
<argument type="service" id="service_container" />
<argument>%kernel.root_dir%/views</argument>
</service>

<service id="twig.loader" class="%twig.loader.class%">
Expand Down

0 comments on commit 7f52346

Please sign in to comment.