Procházet zdrojové kódy

Merge remote branch 'vicb/templating'

Fabien Potencier před 14 roky
rodič
revize
c5a6c8432f

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TemplatePathsCacheWarmer.php

@@ -49,7 +49,7 @@ class TemplatePathsCacheWarmer extends CacheWarmer
      * @param string $cacheDir The cache directory
      */
     public function warmUp($cacheDir)
-    {       
+    {
         $templates = array();
 
         foreach ($this->kernel->getBundles() as $name => $bundle) {

+ 51 - 4
src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php

@@ -13,6 +13,7 @@ namespace Symfony\Bundle\TwigBundle\CacheWarmer;
 
 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 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  */
 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,10 +56,15 @@ class TemplateCacheCacheWarmer extends CacheWarmer
      */
     public function warmUp($cacheDir)
     {
-        $templates = include $cacheDir.'/templates.php';
-
         $twig = $this->container->get('twig');
-        foreach (array_keys($templates) as $template) {
+
+        foreach ($this->kernel->getBundles() as $name => $bundle) {
+            foreach ($this->findTemplatesIn($bundle->getPath().self::TEMPLATES_PATH_IN_BUNDLE, $name) as $template) {
+                $twig->loadTemplate($template);
+            }
+        }
+
+        foreach ($this->findTemplatesIn($this->rootDir) as $template) {
             $twig->loadTemplate($template);
         }
     }
@@ -59,4 +78,32 @@ class TemplateCacheCacheWarmer extends CacheWarmer
     {
         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 TemplateReference
+     */
+    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 - 0
src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

@@ -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%">

+ 3 - 3
src/Symfony/Component/HttpKernel/Kernel.php

@@ -427,12 +427,12 @@ abstract class Kernel implements KernelInterface
     {
         $class = $this->getContainerClass();
         $cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
-        $fresh = false;
+        $fresh = true;
         if (!$cache->isFresh()) {
             $container = $this->buildContainer();
             $this->dumpContainer($cache, $container, $class);
 
-            $fresh = true;
+            $fresh = false;
         }
 
         require_once $cache;
@@ -440,7 +440,7 @@ abstract class Kernel implements KernelInterface
         $this->container = new $class();
         $this->container->set('kernel', $this);
 
-        if ($fresh && 'cli' !== php_sapi_name()) {
+        if (!$fresh && 'cli' !== php_sapi_name()) {
             $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
         }
     }