8000 [TwigBundle] Fix Twig cache is not properly warmed · symfony/symfony@e704ee4 · GitHub
[go: up one dir, main page]

Skip to content

Commit e704ee4

Browse files
committed
[TwigBundle] Fix Twig cache is not properly warmed
1 parent 619528a commit e704ee4

File tree

6 files changed

+129
-8
lines changed

6 files changed

+129
-8
lines changed

src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface
3131
/**
3232
* Constructor.
3333
*
34-
* @param ContainerInterface $container The dependency injection container
35-
* @param TemplateFinderInterface $finder The template paths cache warmer
34+
* @param ContainerInterface $container The dependency injection container
35+
* @param TemplateFinderInterface|null $finder The template paths cache warmer
3636
*/
37-
public function __construct(ContainerInterface $container, TemplateFinderInterface $finder)
37+
public function __construct(ContainerInterface $container, TemplateFinderInterface $finder = null)
3838
{
3939
// We don't inject the T 10000 wig environment directly as it depends on the
4040
// template locator (via the loader) which might be a cached one.
4141
// The cached template locator is available once the TemplatePathsCacheWarmer
42-
// has been warmed up
42+
// has been warmed up.
43+
// But it can also be null if templating has been disabled.
4344
$this->container = $container;
4445
$this->finder = $finder;
4546
}
@@ -51,6 +52,10 @@ public function __construct(ContainerInterface $container, TemplateFinderInterfa
5152
*/
5253
public function warmUp($cacheDir)
5354
{
55+
if (null === $this->finder) {
56+
return;
57+
}
58+
5459
$twig = $this->container->get('twig');
5560

5661
foreach ($this->finder->findAllTemplates() as $template) {

src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ public function process(ContainerBuilder $container)
6868
$container->getDefinition('twig.extension.debug')->addTag('twig.extension');
6969
}
7070

71-
if ($container->has('templating')) {
72-
$container->getDefinition('twig.cache_warmer')->addTag('kernel.cache_warmer');
73-
} else {
71+
if (!$container->has('templating')) {
7472
$loader = $container->getDefinition('twig.loader.native_filesystem');
7573
$loader->addTag('twig.loader');
7674
$loader->setMethodCalls($container->getDefinition('twig.loader.filesystem')->getMethodCalls());

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646
</service>
4747

4848
<service id="twig.cache_warmer" class="%twig.cache_warmer.class%" public="false">
49+
<tag 8000 name="kernel.cache_warmer" />
4950
<argument type="service" id="service_container" />
50-
<argument type="service" id="templating.finder" />
51+
<argument type="service" id="templating.finder" on-invalid="ignore" />
5152
</service>
5253

5354
<service id="twig.loader.native_filesystem" class="Twig_Loader_Filesystem" public="false">
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\TwigBundle\Tests;
13+
14+
use Symfony\Component\HttpKernel\Kernel;
15+
use Symfony\Component\Config\Loader\LoaderInterface;
16+
use Symfony\Component\Filesystem\Filesystem;
17+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
18+
use Symfony\Bundle\TwigBundle\TwigBundle;
19+
20+
class NewCacheWamingTest extends \PHPUnit_Framework_TestCase
21+
{
22+
public function testCacheIsProperlyWarmedWhenTemplatingIsAvailable()
23+
{
24+
$kernel = new CacheWarmingKernel(true);
25+
$kernel->boot();
26+
27+
$warmer = $kernel->getContainer()->get('cache_warmer');
28+
$warmer->enableOptionalWarmers();
29+
$warmer->warmUp($kernel->getCacheDir());
30+
31+
$this->assertTrue(file_exists($kernel->getCacheDir().'/twig'));
32+
}
33+
34+
public function testCacheIsNotWarmedWhenTemplatingIsDisabled()
35+
{
36+
$kernel = new CacheWarmingKernel(false);
37+
$kernel->boot();
38+
39+
$warmer = $kernel->getContainer()->get('cache_warmer');
40+
$warmer->enableOptionalWarmers();
41+
$warmer->warmUp($kernel->getCacheDir());
42+
43+
$this->assertFalse(file_exists($kernel->getCacheDir().'/twig'));
44+
}
45+
46+
protected function setUp()
47+
{
48+
$this->deleteTempDir();
49+
}
50+
51+
protected function tearDown()
52+
{
53+
$this->deleteTempDir();
54+
}
55+
56+
private function deleteTempDir()
57+
{
58+
if (!file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION.'/CacheWarmingKernel')) {
59+
return;
60+
}
61+
62+
$fs = new Filesystem();
63+
$fs->remove($dir);
64+
}
65+
}
66+
67+
class CacheWarmingKernel extends Kernel
68+
{
69+
private $withTemplating;
70+
71+
public function __construct($withTemplating)
72+
{
73+
$this->withTemplating = $withTemplating;
74+
75+
parent::__construct('dev', true);
76+
}
77+
78+
public function getName()
79+
{
80+
return 'CacheWarming';
81+
}
82+
83+
public function registerBundles()
84+
{
85+
return array(new FrameworkBundle(), new TwigBundle());
86+
}
87+
88+
public function registerContainerConfiguration(LoaderInterface $loader)
89+
{
90+
$loader->load(function ($container) {
91+
$container->loadFromExtension('framework', array(
92+
'secret' => '$ecret',
93+
));
94+
});
95+
96+
if ($this->withTemplating) {
97+
$loader->load(function ($container) {
98+
$container->loadFromExtension('framework', array(
99+
'secret' => '$ecret',
100+
'templating' => array('engines' => array('twig')),
101+
'router' => array('resource' => '%kernel.root_dir%/Resources/config/empty_routing.yml'),
102+
));
103+
});
104+
}
105+
}
106+
107+
public function getCacheDir()
108+
{
109+
return sys_get_temp_dir().'/'.Kernel::VERSION.'/CacheWarmingKernel/cache';
110+
}
111+
112+
public function getLogDir()
113+
{
114+
return sys_get_temp_dir().'/'.Kernel::VERSION.'/CacheWarmingKernel/logs';
115+
}
116+
}

src/Symfony/Bundle/TwigBundle/Tests/Functional/Resources/config/empty_routing.yml

Whitespace-only changes.

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"symfony/dependency-injection": "~2.6,>=2.6.6",
2929
"symfony/expression-language": "~2.4",
3030
"symfony/config": "~2.2",
31+
"symfony/finder": "~2.0,>=2.0.5",
3132
"symfony/routing": "~2.1",
3233
"symfony/templating": "~2.1",
3334
"symfony/yaml": "~2.3",

0 commit comments

Comments
 (0)
0