Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 788f63d

Browse filesBrowse files
committed
[FrameworkBundle] Simplify the over-complicated template cache warmer
1 parent 79bc233 commit 788f63d
Copy full SHA for 788f63d

File tree

Expand file treeCollapse file tree

2 files changed

+25
-27
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+25
-27
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TemplatePathsCacheWarmer.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TemplatePathsCacheWarmer.php
+25-26Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
1515
use Symfony\Component\HttpKernel\KernelInterface;
1616
use Symfony\Component\Finder\Finder;
17-
use Symfony\Component\Config\FileLocatorInterface;
18-
use Symfony\Bundle\FrameworkBundle\Templating\Template;
1917
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
2018

2119
/**
@@ -25,7 +23,8 @@
2523
*/
2624
class TemplatePathsCacheWarmer extends CacheWarmer
2725
{
28-
protected $locator;
26+
const TEMPLATES_PATH_IN_BUNDLE = '/Resources/views';
27+
2928
protected $kernel;
3029
protected $rootDir;
3130
protected $parser;
@@ -34,14 +33,12 @@ class TemplatePathsCacheWarmer extends CacheWarmer
3433
* Constructor.
3534
*
3635
* @param KernelInterface $kernel A KernelInterface instance
37-
* @param FileLocatorInterface $locator A FileLocatorInterface instance
3836
* @param TemplateNameParser $parser A TemplateNameParser instance
3937
* @param string $rootDir The directory where global templates can be stored
4038
*/
41-
public function __construct(KernelInterface $kernel, FileLocatorInterface $locator, TemplateNameParser $parser, $rootDir)
39+
public function __construct(KernelInterface $kernel, TemplateNameParser $parser, $rootDir)
4240
{
4341
$this->kernel = $kernel;
44-
$this->locator = $locator;
4542
$this->parser = $parser;
4643
$this->rootDir = $rootDir;
4744
}
@@ -52,8 +49,14 @@ public function __construct(KernelInterface $kernel, FileLocatorInterface $locat
5249
* @param string $cacheDir The cache directory
5350
*/
5451
public function warmUp($cacheDir)
55-
{
56-
$templates = $this->computeTemplatePaths();
52+
{
53+
$templates = array();
54+
55+
foreach ($this->kernel->getBundles() as $name => $bundle) {
56+
$templates += $this->findTemplatesIn($bundle->getPath().self::TEMPLATES_PATH_IN_BUNDLE, $name);
57+
}
58+
59+
$templates += $this->findTemplatesIn($this->rootDir);
5760

5861
$this->writeCacheFile($cacheDir.'/templates.php', sprintf('<?php return %s;', var_export($templates, true)));
5962
}
@@ -68,35 +71,31 @@ public function isOptional()
6871
return false;
6972
}
7073

71-
protected function computeTemplatePaths()
74+
/**
75+
* Find templates in the given directory
76+
*
77+
* @param string $dir The folder where to look for templates
78+
* @param string $bundle The name of the bundle (null when out of a bundle)
79+
*
80+
* @return array An array of template paths
81+
*/
82+
protected function findTemplatesIn($dir, $bundle = null)
7283
{
73-
$prefix = '/Resources/views';
7484
$templates = array();
75-
foreach ($this->kernel->getBundles() as $name => $bundle) {
76-
if (!is_dir($dir = $bundle->getPath().$prefix)) {
77-
continue;
78-
}
7985

86+
if (is_dir($dir)) {
8087
$finder = new Finder();
8188
foreach ($finder->files()->followLinks()->in($dir) as $file) {
8289
$template = $this->parser->parseFromFilename($file->getRelativePathname());
8390
if (false !== $template) {
84-
$template->set('bundle', $name);
85-
$templates[$template->getSignature()] = $this->locator->locate($template->getPath(), $this->rootDir);
86-
}
87-
}
88-
}
89-
90-
if (is_dir($this->rootDir)) {
91-
$finder = new Finder();
92-
foreach ($finder->files()->followLinks()->in($this->rootDir) as $file) {
93-
$template = $this->parser->parseFromFilename($file->getRelativePathname());
94-
if (false !== $template) {
91+
if (null !== $bundle) {
92+
$template->set('bundle', $bundle);
93+
}
9594
$templates[$template->getSignature()] = $file->getRealPath();
9695
}
9796
}
9897
}
9998

100-
return $templates;
99+
return $templates;
101100
}
102101
}

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
<service id="templating.cache_warmer.template_paths" class="%templating.cache_warmer.template_paths.class%" public="false">
4343
<argument type="service" id="kernel" />
44-
<argument type="service" id="file_locator" />
4544
<argument type="service" id="templating.name_parser" />
4645
<argument>%kernel.root_dir%/views</argument>
4746
</service>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.