diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 5d014e930c..3fa98b8bef 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 cc93e27b55..611c448232 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 @@ class TemplateIterator implements \IteratorAggregate 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 @@ class TemplateIterator implements \IteratorAggregate $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 @@ class TemplateIterator implements \IteratorAggregate * * @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 0000000000..bb07ecfe55 --- /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 636d5796f8..ba52cfb66f 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php @@ -25,13 +25,14 @@ class TemplateIteratorTest extends TestCase $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',