Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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">
  • Loading branch information
fabpot committed Jan 24, 2011
1 parent 206b49a commit 8f6e8a4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
@@ -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;
}
}
Expand Up @@ -108,6 +108,14 @@ protected function doConfigLoad(array $config, ContainerBuilder $container)
}
}

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));
}

Expand Down
Expand Up @@ -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">
Expand All @@ -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 changes: 5 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Expand Up @@ -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>
Expand All @@ -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>
Expand Down

0 comments on commit 8f6e8a4

Please sign in to comment.