Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters