diff --git a/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php b/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php index 65827eba5a..5dde940691 100644 --- a/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php +++ b/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php @@ -11,9 +11,11 @@ namespace Symfony\Bundle\TwigBundle\CacheWarmer; +use Symfony\Component\Finder\Finder; use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface; +use Symfony\Component\Templating\TemplateReference; /** * Generates the Twig cache for all templates. @@ -27,14 +29,16 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface { protected $container; protected $finder; + private $paths; /** * Constructor. * * @param ContainerInterface $container The dependency injection container * @param TemplateFinderInterface $finder The template paths cache warmer + * @param array $paths Additional twig paths to warm */ - public function __construct(ContainerInterface $container, TemplateFinderInterface $finder) + public function __construct(ContainerInterface $container, TemplateFinderInterface $finder, array $paths = array()) { // We don't inject the Twig environment directly as it depends on the // template locator (via the loader) which might be a cached one. @@ -42,6 +46,7 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface // has been warmed up $this->container = $container; $this->finder = $finder; + $this->paths = $paths; } /** @@ -53,7 +58,13 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface { $twig = $this->container->get('twig'); - foreach ($this->finder->findAllTemplates() as $template) { + $templates = $this->finder->findAllTemplates(); + + foreach ($this->paths as $path => $namespace) { + $templates = array_merge($templates, $this->findTemplatesInFolder($namespace, $path)); + } + + foreach ($templates as $template) { if ('twig' !== $template->get('engine')) { continue; } @@ -75,4 +86,32 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface { return true; } + + /** + * Find templates in the given directory. + * + * @param string $namespace The namespace for these templates + * @param string $dir The folder where to look for templates + * + * @return array An array of templates of type TemplateReferenceInterface + */ + private function findTemplatesInFolder($namespace, $dir) + { + if (!is_dir($dir)) { + return array(); + } + + $templates = array(); + $finder = new Finder(); + + foreach ($finder->files()->followLinks()->in($dir) as $file) { + $name = $file->getRelativePathname(); + $templates[] = new TemplateReference( + $namespace ? sprintf('@%s/%s', $namespace, $name) : $name, + 'twig' + ); + } + + return $templates; + } } diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php index df49f8b372..7b6a676a52 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php @@ -77,6 +77,8 @@ class TwigExtension extends Extension } } + $container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']); + // register bundles as Twig namespaces foreach ($container->getParameter('kernel.bundles') as $bundle => $class) { $dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views'; diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 9e1a117774..c06a82f06e 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -48,6 +48,7 @@ +