14
14
use Symfony \Component \HttpKernel \CacheWarmer \CacheWarmer ;
15
15
use Symfony \Component \HttpKernel \KernelInterface ;
16
16
use Symfony \Component \Finder \Finder ;
17
- use Symfony \Component \Config \FileLocatorInterface ;
18
- use Symfony \Bundle \FrameworkBundle \Templating \Template ;
19
17
use Symfony \Bundle \FrameworkBundle \Templating \TemplateNameParser ;
20
18
21
19
/**
25
23
*/
26
24
class TemplatePathsCacheWarmer extends CacheWarmer
27
25
{
28
- protected $ locator ;
26
+ const TEMPLATES_PATH_IN_BUNDLE = '/Resources/views ' ;
27
+
29
28
protected $ kernel ;
30
29
protected $ rootDir ;
31
30
protected $ parser ;
@@ -34,14 +33,12 @@ class TemplatePathsCacheWarmer extends CacheWarmer
34
33
* Constructor.
35
34
*
36
35
* @param KernelInterface $kernel A KernelInterface instance
37
- * @param FileLocatorInterface $locator A FileLocatorInterface instance
38
36
* @param TemplateNameParser $parser A TemplateNameParser instance
39
37
* @param string $rootDir The directory where global templates can be stored
40
38
*/
41
- public function __construct (KernelInterface $ kernel , FileLocatorInterface $ locator , TemplateNameParser $ parser , $ rootDir )
39
+ public function __construct (KernelInterface $ kernel , TemplateNameParser $ parser , $ rootDir )
42
40
{
43
41
$ this ->kernel = $ kernel ;
44
- $ this ->locator = $ locator ;
45
42
$ this ->parser = $ parser ;
46
43
$ this ->rootDir = $ rootDir ;
47
44
}
@@ -52,8 +49,14 @@ public function __construct(KernelInterface $kernel, FileLocatorInterface $locat
52
49
* @param string $cacheDir The cache directory
53
50
*/
54
51
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 );
57
60
58
61
$ this ->writeCacheFile ($ cacheDir .'/templates.php ' , sprintf ('<?php return %s; ' , var_export ($ templates , true )));
59
62
}
@@ -68,35 +71,31 @@ public function isOptional()
68
71
return false ;
69
72
}
70
73
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 )
72
83
{
73
- $ prefix = '/Resources/views ' ;
74
84
$ templates = array ();
75
- foreach ($ this ->kernel ->getBundles () as $ name => $ bundle ) {
76
- if (!is_dir ($ dir = $ bundle ->getPath ().$ prefix )) {
77
- continue ;
78
- }
79
85
86
+ if (is_dir ($ dir )) {
80
87
$ finder = new Finder ();
81
88
foreach ($ finder ->files ()->followLinks ()->in ($ dir ) as $ file ) {
82
89
$ template = $ this ->parser ->parseFromFilename ($ file ->getRelativePathname ());
83
90
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
+ }
95
94
$ templates [$ template ->getSignature ()] = $ file ->getRealPath ();
96
95
}
97
96
}
98
97
}
99
98
100
- return $ templates ;
99
+ return $ templates ;
101
100
}
102
101
}
0 commit comments