Browse Source

[TwigBundle] added a cache warmer to generate all Twig templates cache

To enable this cache warmer, you must add a "cache-warner" option
to twig:config:

        <twig:config cache-warmer="true">
Fabien Potencier 14 years ago
parent
commit
8f6e8a4691

+ 62 - 0
src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php

@@ -0,0 +1,62 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\TwigBundle\CacheWarmer;
+
+use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * 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.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class TemplateCacheCacheWarmer extends CacheWarmer
+{
+    protected $container;
+
+    public function __construct(ContainerInterface $container)
+    {
+        // 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;
+    }
+
+    /**
+     * Warms up the cache.
+     *
+     * @param string $cacheDir The cache directory
+     */
+    public function warmUp($cacheDir)
+    {
+        $templates = include $cacheDir.'/templates.php';
+
+        $twig = $this->container->get('twig');
+        foreach (array_keys($templates) as $template) {
+            $twig->loadTemplate($template);
+        }
+    }
+
+    /**
+     * Checks whether this warmer is optional or not.
+     *
+     * @return Boolean always true
+     */
+    public function isOptional()
+    {
+        return true;
+    }
+}

+ 8 - 0
src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

@@ -108,6 +108,14 @@ class TwigExtension extends Extension
             }
         }
 
+        if (isset($config['cache-warmer'])) {
+            $config['cache_warmer'] = $config['cache-warmer'];
+        }
+
+        if (isset($config['cache_warmer']) && $config['cache_warmer']) {
+            $container->getDefinition('templating.cache_warmer.templates_cache')->addTag('kernel.cache_warmer');
+        }
+
         $container->setParameter('twig.options', array_replace($container->getParameter('twig.options'), $config));
     }
 

+ 9 - 0
src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd

@@ -21,6 +21,7 @@
         <xsd:attribute name="auto-reload" type="xsd:string" />
         <xsd:attribute name="base-template-class" type="xsd:string" />
         <xsd:attribute name="autoescape" type="xsd:string" />
+        <xsd:attribute name="cache-warmer" type="cache_warmer" />
     </xsd:complexType>
 
     <xsd:complexType name="form">
@@ -38,4 +39,12 @@
     <xsd:complexType name="extension">
         <xsd:attribute name="id" type="xsd:string" />
     </xsd:complexType>
+
+    <xsd:simpleType name="cache_warmer">
+        <xsd:restriction base="xsd:string">
+            <xsd:enumeration value="false" />
+            <xsd:enumeration value="true" />
+            <xsd:enumeration value="full" />
+        </xsd:restriction>
+    </xsd:simpleType>
 </xsd:schema>

+ 5 - 0
src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

@@ -17,6 +17,7 @@
             <parameter>TwigBundle::form.html.twig</parameter>
         </parameter>
         <parameter key="templating.engine.twig.class">Symfony\Bundle\TwigBundle\TwigEngine</parameter>
+        <parameter key="templating.cache_warmer.templates_cache.class">Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheCacheWarmer</parameter>
     </parameters>
 
     <services>
@@ -25,6 +26,10 @@
             <argument>%twig.options%</argument>
         </service>
 
+        <service id="templating.cache_warmer.templates_cache" class="%templating.cache_warmer.templates_cache.class%" public="false">
+            <argument type="service" id="service_container" />
+        </service>
+
         <service id="twig.loader" class="%twig.loader.class%">
             <argument type="service" id="templating.locator" />
         </service>