8000 bug #9112 Prepend Child Bundle paths before the parent (trsteel88) · symfony/symfony@cfa99a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit cfa99a9

Browse files
committed
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
2 parents f8965b6 + 17aaf84 commit cfa99a9

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,26 @@ public function load(array $configs, ContainerBuilder $container)
6969
}
7070

7171
// register bundles as Twig namespaces
72-
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
72+
$bundles = $this->getBundlesByChildPriority($container);
73+
foreach ($bundles as $bundle => $bundleReflection) {
74+
/** @var \Symfony\Component\HttpKernel\Bundle\BundleInterface $bundleInstance */
75+
$bundleInstance = $bundleReflection->newInstance();
76+
77+
if (null !== $parentBundle = $bundleInstance->getParent()) {
78+
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views')) {
79+
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $parentBundle);
80+
}
81+
82+
if (is_dir($dir = $bundleInstance->getPath().'/Resources/views')) {
83+
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $parentBundle);
84+
}
85+
}
86+
7387
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views')) {
7488
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
7589
}
7690

77-
$reflection = new \ReflectionClass($class);
78-
if (is_dir($dir = dirname($reflection->getFilename()).'/Resources/views')) {
91+
if (is_dir($dir = $bundleInstance->getPath().'/Resources/views')) {
7992
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
8093
}
8194
}
@@ -130,12 +143,36 @@ public function load(array $configs, ContainerBuilder $container)
130143
));
131144
}
132145

146+
/**
147+
* @param ContainerBuilder $container
148+
* @return array | \ReflectionClass[]
149+
*/
150+
private function getBundlesByChildPriority(ContainerBuilder $container)
151+
{
152+
$childBundles = array();
153+
$parentBundles = array();
154+
155+
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
156+
$reflection = new \ReflectionClass($class);
157+
158+
$bundleInstance = $reflection->newInstance();
159+
if (null === $bundleInstance->getParent()) {
160+
$parentBundles[$bundle] = $reflection;
161+
} else {
162+
$childBundles[$bundle] = $reflection;
163+
}
164+
}
165+
166+
return array_merge($childBundles, $parentBundles);
167+
}
168+
133169
private function addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle)
134170
{
135171
$name = $bundle;
136172
if ('Bundle' === substr($name, -6)) {
137173
$name = substr($name, 0, -6);
138174
}
175+
139176
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir, $name));
140177
}
141178

0 commit comments

Comments
 (0)
0