Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TwigBundle] Fixed caching of templates in default path on cache warmup #27764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<argument type="service" id="kernel" />
<argument>%kernel.root_dir%</argument>
<argument type="collection" /> <!-- Twig paths -->
<argument>%twig.default_path%</argument>
</service>

<service id="twig.template_cache_warmer" class="Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer">
Expand Down
23 changes: 15 additions & 8 deletions src/Symfony/Bundle/TwigBundle/TemplateIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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)) {
Expand All @@ -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)
);
}

Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a layout
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down