8000 Prepend Child Bundle paths before the parent by trsteel88 · Pull Request #9112 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Prepend Child Bundle paths before the parent #9112

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

Closed
wants to merge 8 commits into from
F0BB
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,26 @@ public function load(array $configs, ContainerBuilder $container)
}

// 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);
}
}
Expand Down Expand Up @@ -130,12 +143,36 @@ public function load(array $configs, ContainerBuilder $container)
));
}

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

Expand Down
0