Skip to content

Commit cad16ac

Browse files
committed
feature #14764 [TwigBundle] Warmup twig templates in non-standard paths (kbond)
This PR was submitted for the 2.7 branch but it was merged into the 2.8 branch instead (closes #14764). Discussion ---------- [TwigBundle] Warmup twig templates in non-standard paths | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #12507 | License | MIT | Doc PR | symfony/symfony-docs#5391 Commits ------- 96cce38 Warmup twig templates in non-standard paths (closes #12507)
2 parents ffed0ce + 96cce38 commit cad16ac

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace Symfony\Bundle\TwigBundle\CacheWarmer;
1313

14+
use Symfony\Component\Finder\Finder;
1415
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
1516
use Symfony\Component\DependencyInjection\ContainerInterface;
1617
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
18+
use Symfony\Component\Templating\TemplateReference;
1719

1820
/**
1921
* Generates the Twig cache for all templates.
@@ -27,21 +29,24 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface
2729
{
2830
protected $container;
2931
protected $finder;
32+
private $paths;
3033

3134
/**
3235
* Constructor.
3336
*
3437
* @param ContainerInterface $container The dependency injection container
3538
* @param TemplateFinderInterface $finder The template paths cache warmer
39+
* @param array $paths Additional twig paths to warm
3640
*/
37-
public function __construct(ContainerInterface $container, TemplateFinderInterface $finder)
41+
public function __construct(ContainerInterface $container, TemplateFinderInterface $finder, array $paths = array())
3842
{
3943
// We don't inject the Twig environment directly as it depends on the
4044
// template locator (via the loader) which might be a cached one.
4145
// The cached template locator is available once the TemplatePathsCacheWarmer
4246
// has been warmed up
4347
$this->container = $container;
4448
$this->finder = $finder;
49+
$this->paths = $paths;
4550
}
4651

4752
/**
@@ -53,7 +58,13 @@ public function warmUp($cacheDir)
5358
{
5459
$twig = $this->container->get('twig');
5560

56-
foreach ($this->finder->findAllTemplates() as $template) {
61+
$templates = $this->finder->findAllTemplates();
62+
63+
foreach ($this->paths as $path => $namespace) {
64+
$templates = array_merge($templates, $this->findTemplatesInFolder($namespace, $path));
65+
}
66+
67+
foreach ($templates as $template) {
5768
if ('twig' !== $template->get('engine')) {
5869
continue;
5970
}
@@ -75,4 +86,32 @@ public function isOptional()
7586
{
7687
return true;
7788
}
89+
90+
/**
91+
* Find templates in the given directory.
92+
*
93+
* @param string $namespace The namespace for these templates
94+
* @param string $dir The folder where to look for templates
95+
*
96+
* @return array An array of templates of type TemplateReferenceInterface
97+
*/
98+
private function findTemplatesInFolder($namespace, $dir)
99+
{
100+
if (!is_dir($dir)) {
101+
return array();
102+
}
103+
104+
$templates = array();
105+
$finder = new Finder();
106+
107+
foreach ($finder->files()->followLinks()->in($dir) as $file) {
108+
$name = $file->getRelativePathname();
109+
$templates[] = new TemplateReference(
110+
$namespace ? sprintf('@%s/%s', $namespace, $name) : $name,
111+
'twig'
112+
);
113+
}
114+
115+
return $templates;
116+
}
78117
}

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public function load(array $configs, ContainerBuilder $container)
7777
}
7878
}
7979

80+
$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']);
81+
8082
// register bundles as Twig namespaces
8183
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
8284
$dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views';

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<service id="twig.cache_warmer" class="%twig.cache_warmer.class%" public="false">
4949
<argument type="service" id="service_container" />
5050
<argument type="service" id="templating.finder" />
51+
<argument type="collection" /> <!-- Twig paths -->
5152
</service>
5253

5354
<service id="twig.loader.native_filesystem" class="Twig_Loader_Filesystem" public="false">

0 commit comments

Comments
 (0)