8000 bug #27764 [TwigBundle] Fixed caching of templates in default path on… · symfony/symfony@eaeb124 · GitHub
[go: up one dir, main page]

Skip to content

Commit eaeb124

Browse files
committed
bug #27764 [TwigBundle] Fixed caching of templates in default path on cache warmup (yceruto)
This PR was merged into the 3.4 branch. Discussion ---------- [TwigBundle] Fixed caching of templates in default path on cache warmup | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - **Reproducer** ```bash $ git clone https://github.com/symfony/demo && cd demo $ bin/console cache:clear $ find var/cache/dev/twig -printf "%y\n" | awk '/f/{f++}/d/{d++}END{printf "%d directories, %d files\n", d, f}' ... ``` **Before:** ```bash ... 131 directories, 167 files ``` Twig `paths` config: https://github.com/symfony/symfony/blob/44ce4dd62536037f9cea4db0146541b4ab867d34/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php#L104 `%kernel.root_dir%/Resources/views`: https://github.com/symfony/symfony/blob/2b01d594818cf872bc481b3a1fe0210da29fab69/src/Symfony/Bundle/TwigBundle/TemplateIterator.php#L50 **After:** ```bash ... 141 directories, 193 files ``` Adding `%twig.default_path%`. Commits ------- 245c860 Fixed caching of templates in default path on cache warmup
2 parents 8851c14 + 245c860 commit eaeb124

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<argument type="service" id="kernel" />
4242
<argument>%kernel.root_dir%</argument>
4343
<argument type="collection" /> <!-- Twig paths -->
44+
<argument>%twig.default_path%</argument>
4445
</service>
4546

4647
<service id="twig.template_cache_warmer" class="Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer">

src/Symfony/Bundle/TwigBundle/TemplateIterator.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ class TemplateIterator implements \IteratorAggregate
2525
private $rootDir;
2626
private $templates;
2727
private $paths;
28+
private $defaultPath;
2829

2930
/**
30-
* @param KernelInterface $kernel A KernelInterface instance
31-
* @param string $rootDir The directory where global templates can be stored
32-
* @param array $paths Additional Twig paths to warm
31+
* @param KernelInterface $kernel A KernelInterface instance
32+
* @param string $rootDir The directory where global templates can be stored
33+
* @param array $paths Additional Twig paths to warm
34+
* @param string $defaultPath The directory where global templates can be stored
3335
*/
34-
public function __construct(KernelInterface $kernel, $rootDir, array $paths = array())
36+
public function __construct(KernelInterface $kernel, $rootDir, array $paths = array(), $defaultPath = null)
3537
{
3638
$this->kernel = $kernel;
3739
$this->rootDir = $rootDir;
3840
$this->paths = $paths;
41+
$this->defaultPath = $defaultPath;
3942
}
4043

4144
/**
@@ -47,7 +50,10 @@ public function getIterator()
4750
return $this->templates;
4851
}
4952

50-
$this->templates = $this->findTemplatesInDirectory($this->rootDir.'/Resources/views');
53+
$this->templates = array_merge(
54+
$this->findTemplatesInDirectory($this->rootDir.'/Resources/views'),
55+
$this->findTemplatesInDirectory($this->defaultPath, null, array('bundles'))
56+
);
5157
foreach ($this->kernel->getBundles() as $bundle) {
5258
$name = $bundle->getName();
5359
if ('Bundle' === substr($name, -6)) {
@@ -57,7 +63,8 @@ public function getIterator()
5763
$this->templates = array_merge(
5864
$this->templates,
5965
$this->findTemplatesInDirectory($bundle->getPath().'/Resources/views', $name),
60-
$this->findTemplatesInDirectory($this->rootDir.'/'.$bundle->getName().'/views', $name)
66+
$this->findTemplatesInDirectory($this->rootDir.'/'.$bundle->getName().'/views', $name),
67+
$this->findTemplatesInDirectory($this->defaultPath.'/bundles/'.$bundle->getName(), $name)
6168
);
6269
}
6370

@@ -76,14 +83,14 @@ public function getIterator()
7683
*
7784
* @return array
7885
*/
79-
private function findTemplatesInDirectory($dir, $namespace = null)
86+
private function findTemplatesInDirectory($dir, $namespace = null, array $excludeDirs = array())
8087
{
8188
if (!is_dir($dir)) {
8289
return array();
8390
}
8491

8592
$templates = array();
86-
foreach (Finder::create()->files()->followLinks()->in($dir) as $file) {
93+
foreach (Finder::create()->files()->followLinks()->in($dir)->exclude($excludeDirs) as $file) {
8794
$templates[] = (null !== $namespace ? '@'.$namespace.'/' : '').str_replace('\\', '/', $file->getRelativePathname());
8895
}
8996

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a layout

src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ public function testGetIterator()
2525
$kernel->expects($this->any())->method('getBundles')->will($this->returnValue(array(
2626
$bundle,
2727
)));
28-
$iterator = new TemplateIterator($kernel, __DIR__.'/Fixtures/templates', array(__DIR__.'/Fixtures/templates/Foo' => 'Foo'));
28+
$iterator = new TemplateIterator($kernel, __DIR__.'/Fixtures/templates', array(__DIR__.'/Fixtures/templates/Foo' => 'Foo'), __DIR__.'/DependencyInjection/Fixtures/templates');
2929

3030
$sorted = iterator_to_array($iterator);
3131
sort($sorted);
3232
$this->assertEquals(
3333
array(
3434
'@Bar/index.html.twig',
35+
'@Bar/layout.html.twig',
3536
'@Foo/index.html.twig',
3637
'layout.html.twig',
3738
'sub/sub.html.twig',

0 commit comments

Comments
 (0)
0