From 245c860ab451dea6a71233c3fd2cc72f4b13e9e2 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Thu, 28 Jun 2018 14:05:35 -0400 Subject: [PATCH] Fixed caching of templates in default path on cache warmup --- .../TwigBundle/Resources/config/twig.xml | 1 + .../Bundle/TwigBundle/TemplateIterator.php | 23 ++++++++++++------- .../bundles/BarBundle/layout.html.twig | 1 + .../TwigBundle/Tests/TemplateIteratorTest.php | 3 ++- 4 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/bundles/BarBundle/layout.html.twig diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 5d014e930c53e..3fa98b8bef936 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -41,6 +41,7 @@ %kernel.root_dir% + %twig.default_path% diff --git a/src/Symfony/Bundle/TwigBundle/TemplateIterator.php b/src/Symfony/Bundle/TwigBundle/TemplateIterator.php index cc93e27b5577b..611c448232030 100644 --- a/src/Symfony/Bundle/TwigBundle/TemplateIterator.php +++ b/src/Symfony/Bundle/TwigBundle/TemplateIterator.php @@ -25,17 +25,20 @@ class TemplateIterator implements \IteratorAggregate private $rootDir; private $templates; private $paths; + private $defaultPath; /** - * @param KernelInterface $kernel A KernelInterface instance - * @param string $rootDir The directory where global templates can be stored - * @param array $paths Additional Twig paths to warm + * @param KernelInterface $kernel A KernelInterface instance + * @param string $rootDir The directory where global templates can be stored + * @param array $paths Additional Twig paths to warm + * @param string $defaultPath The directory where global templates can be stored */ - public function __construct(KernelInterface $kernel, $rootDir, array $paths = array()) + public function __construct(KernelInterface $kernel, $rootDir, array $paths = array(), $defaultPath = null) { $this->kernel = $kernel; $this->rootDir = $rootDir; $this->paths = $paths; + $this->defaultPath = $defaultPath; } /** @@ -47,7 +50,10 @@ public function getIterator() return $this->templates; } - $this->templates = $this->findTemplatesInDirectory($this->rootDir.'/Resources/views'); + $this->templates = array_merge( + $this->findTemplatesInDirectory($this->rootDir.'/Resources/views'), + $this->findTemplatesInDirectory($this->defaultPath, null, array('bundles')) + ); foreach ($this->kernel->getBundles() as $bundle) { $name = $bundle->getName(); if ('Bundle' === substr($name, -6)) { @@ -57,7 +63,8 @@ public function getIterator() $this->templates = array_merge( $this->templates, $this->findTemplatesInDirectory($bundle->getPath().'/Resources/views', $name), - $this->findTemplatesInDirectory($this->rootDir.'/'.$bundle->getName().'/views', $name) + $this->findTemplatesInDirectory($this->rootDir.'/'.$bundle->getName().'/views', $name), + $this->findTemplatesInDirectory($this->defaultPath.'/bundles/'.$bundle->getName(), $name) ); } @@ -76,14 +83,14 @@ public function getIterator() * * @return array */ - private function findTemplatesInDirectory($dir, $namespace = null) + private function findTemplatesInDirectory($dir, $namespace = null, array $excludeDirs = array()) { if (!is_dir($dir)) { return array(); } $templates = array(); - foreach (Finder::create()->files()->followLinks()->in($dir) as $file) { + foreach (Finder::create()->files()->followLinks()->in($dir)->exclude($excludeDirs) as $file) { $templates[] = (null !== $namespace ? '@'.$namespace.'/' : '').str_replace('\\', '/', $file->getRelativePathname()); } diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/bundles/BarBundle/layout.html.twig b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/bundles/BarBundle/layout.html.twig new file mode 100644 index 0000000000000..bb07ecfe55a36 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/templates/bundles/BarBundle/layout.html.twig @@ -0,0 +1 @@ +This is a layout diff --git a/src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php b/src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php index 636d5796f874f..ba52cfb66fd34 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php @@ -25,13 +25,14 @@ public function testGetIterator() $kernel->expects($this->any())->method('getBundles')->will($this->returnValue(array( $bundle, ))); - $iterator = new TemplateIterator($kernel, __DIR__.'/Fixtures/templates', array(__DIR__.'/Fixtures/templates/Foo' => 'Foo')); + $iterator = new TemplateIterator($kernel, __DIR__.'/Fixtures/templates', array(__DIR__.'/Fixtures/templates/Foo' => 'Foo'), __DIR__.'/DependencyInjection/Fixtures/templates'); $sorted = iterator_to_array($iterator); sort($sorted); $this->assertEquals( array( '@Bar/index.html.twig', + '@Bar/layout.html.twig', '@Foo/index.html.twig', 'layout.html.twig', 'sub/sub.html.twig',