From 7f523466f4a3b8f120e1766bca786ed021f13cf8 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 18 Mar 2011 11:23:23 +0100 Subject: [PATCH] [TwigBundle] Fix the cache warmer --- .../CacheWarmer/TemplatePathsCacheWarmer.php | 2 +- .../CacheWarmer/TemplateCacheCacheWarmer.php | 57 +++++++++++++++++-- .../TwigBundle/Resources/config/twig.xml | 1 + 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TemplatePathsCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TemplatePathsCacheWarmer.php index 477a73af9931..75cec241d5cf 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TemplatePathsCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TemplatePathsCacheWarmer.php @@ -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) { diff --git a/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php b/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php index b8f80ca41ab7..944a456245ed 100644 --- a/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php +++ b/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php @@ -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. @@ -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; } /** @@ -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); } } @@ -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; + } } diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 2220db3fae2c..01202f6d9ebd 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -19,6 +19,7 @@ + %kernel.root_dir%/views