bug #9112 Prepend Child Bundle paths before the parent (trsteel88)

This PR was submitted for the 2.3-dev branch but it was merged into the 2.3 branch instead (closes #9112).

Discussion
----------

Prepend Child Bundle paths before the parent

This fixes #9085

Say you have AcmeDemoBundle and AppDemoBundle. AcmeDemoBundle is the parent of AppDemoBundle.

If you load templates using @AcmeDemoBundle/ControllerDir/template.html.twig it means that you cannot override the template in AppDemoBundle. The patch below prepends the AppDemoBundle Resources directory to the AcmeDemo namespace.

The namespace directories would not result in:

```
[AcmeDemo] => Array(
    [0] => [absolute-dir-here]/src/App/DemoBundle/Resources/views
    [1] => [absolute-dir-here]/app/Resources/AcmeDemoBundle/views
    [2] => [absolute-dir-here]/src/Acme/DemoBundle/Resources/views
)
```

Commits
-------

19fad88 Prepend Child Bundle paths before the parent
This commit is contained in:
Fabien Potencier 2013-12-31 19:00:11 +01:00
commit cfa99a96dd

View File

@ -69,13 +69,26 @@ class TwigExtension extends Extension
}
// register bundles as Twig namespaces
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
$bundles = $this->getBundlesByChildPriority($container);
foreach ($bundles as $bundle => $bundleReflection) {
/** @var \Symfony\Component\HttpKernel\Bundle\BundleInterface $bundleInstance */
$bundleInstance = $bundleReflection->newInstance();
if (null !== $parentBundle = $bundleInstance->getParent()) {
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views')) {
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $parentBundle);
}
if (is_dir($dir = $bundleInstance->getPath().'/Resources/views')) {
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $parentBundle);
}
}
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views')) {
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
}
$reflection = new \ReflectionClass($class);
if (is_dir($dir = dirname($reflection->getFilename()).'/Resources/views')) {
if (is_dir($dir = $bundleInstance->getPath().'/Resources/views')) {
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
}
}
@ -130,12 +143,36 @@ class TwigExtension extends Extension
));
}
/**
* @param ContainerBuilder $container
* @return array | \ReflectionClass[]
*/
private function getBundlesByChildPriority(ContainerBuilder $container)
{
$childBundles = array();
$parentBundles = array();
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
$reflection = new \ReflectionClass($class);
$bundleInstance = $reflection->newInstance();
if (null === $bundleInstance->getParent()) {
$parentBundles[$bundle] = $reflection;
} else {
$childBundles[$bundle] = $reflection;
}
}
return array_merge($childBundles, $parentBundles);
}
private function addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle)
{
$name = $bundle;
if ('Bundle' === substr($name, -6)) {
$name = substr($name, 0, -6);
}
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir, $name));
}