Skip to content

Commit d4fded7

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

File tree

6 files changed

+125
-5
lines changed

6 files changed

+125
-5
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface
3434
* @param ContainerInterface $container The dependency injection container
3535
* @param TemplateFinderInterface $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 Twig environment directly as it depends on the
4040
// template locator (via the loader) which might be a cached one.
@@ -51,6 +51,10 @@ public function __construct(ContainerInterface $container, TemplateFinderInterfa
5151
*/
5252
public function warmUp($cacheDir)
5353
{
54+
if (null === $this->finder) {
55+
return;
56+
}
57+
5458
$twig = $this->container->get('twig');
5559

5660
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 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+
protected 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' => ['engines' => ['twig']],
101+
'router' => ['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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy