8000 [TwigBundle] Fix bug where namespaced paths don't take parent bundles in account by wesleylancel · Pull Request #19586 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBundle] Fix bug where namespaced paths don't take parent bundles in account #19586

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
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Build all paths first, then add to loader
  • Loading branch information
wesleylancel committed Jan 6, 2017
commit 4e4702cb83fb2d22bdbd96565d658790bde28ef0
87 changes: 56 additions & 31 deletions src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,19 @@ public function load(array $configs, ContainerBuilder $container)
}
}

// register bundles as Twig namespaces
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) {
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $name);
}
$bundleHierarchy = $this->getBundleHierarchy($container);

if (is_dir($dir = $bundle['path'].'/Resources/views')) {
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $name);
foreach ($bundleHierarchy as $name => $bundle) {
$namespace = $this->normalizeBundleName($name);

foreach ($bundle['children'] as $child) {
foreach ($bundleHierarchy[$child]['paths'] as $path) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
}
}

if (null !== $bundle['parent']) {
$this->prependParentPaths($twigFilesystemLoaderDefinition, $container, $name, $bundle['parent']);
foreach ($bundle['paths'] as $path) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
}
}

Expand Down Expand Up @@ -143,36 +144,60 @@ public function load(array $configs, ContainerBuilder $container)
));
}

private function addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle)
private function getBundleHierarchy(ContainerBuilder $container)
{
$name = $this->normalizeBundleName($bundle);
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir, $name));
}
$bundleHierarchy = array();

private function prependTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle)
{
$name = $this->normalizeBundleName($bundle);
$twigFilesystemLoaderDefinition->addMethodCall('prependPath', array($dir, $name));
}
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
8000 if (!array_key_exists($name, $bundleHierarchy)) {
$bundleHierarchy[$name] = array(
'paths' => array(),
'parents' => array(),
'children' => array(),
);
}

private function prependParentPaths($twigFilesystemLoaderDefinition, ContainerBuilder $container, $name, $parentName)
{
$bundleMetadata = $container->getParameter('kernel.bundles_metadata');
$bundle = $bundleMetadata[$name];
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to maintainers: when merging to newer branches, we need to take care of the FileExistenceResource objects here (git conflicts should remind us about this btw)

$bundleHierarchy[$name]['paths'][] = $dir;
}

if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) {
$this->prependTwigPath($twigFilesystemLoaderDefinition, $dir, $parentName);
}
if (is_dir($dir = $bundle['path'].'/Resources/views')) {
$bundleHierarchy[$name]['paths'][] = $dir;
}

if (is_dir($dir = $bundle['path'].'/Resources/views')) {
$this->prependTwigPath($twigFilesystemLoaderDefinition, $dir, $parentName);
}
if (null === $bundle['parent']) {
continue;
}

$bundleHierarchy[$name]['parents'][] = $bundle['parent'];

if (!array_key_exists($bundle['parent'], $bundleHierarchy)) {
$bundleHierarchy[$bundle['parent']] = array(
'paths' => array(),
'parents' => array(),
'children' => array(),
);
}

$bundleHierarchy[$bundle['parent']]['children'] = array_merge($bundleHierarchy[$name]['children'], array($name), $bundleHierarchy[$bundle['parent']]['children']);

$parentBundle = $bundleMetadata[$parentName];
foreach ($bundleHierarchy[$bundle['parent']]['parents'] as $parent) {
$bundleHierarchy[$name]['parents'][] = $parent;
$bundleHierarchy[$parent]['children'] = array_merge($bundleHierarchy[$name]['children'], array($name), $bundleHierarchy[$parent]['children']);
}

if (null !== $parentBundle['parent']) {
$this->prependParentPaths($twigFilesystemLoaderDefinition, $container, $name, $parentBundle['parent']);
foreach ($bundleHierarchy[$name]['children'] as $child) {
$bundleHierarchy[$child]['parents'] = array_merge($bundleHierarchy[$child]['parents'], $bundleHierarchy[$name]['parents']);
}
}

return $bundleHierarchy;
}

private function addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle)
{
$name = $this->normalizeBundleName($bundle);
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir, $name));
}

private function normalizeBundleName($name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a layout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a layout
6D40
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,9 @@ public function testTwigLoaderPaths($format)

$def = $container->getDefinition('twig.loader.filesystem');
$paths = array();
$prependPaths = array();
foreach ($def->getMethodCalls() as $call) {
if ('addPath' === $call[0] && false === strpos($call[1][0], 'Form')) {
$paths[] = $call[1];
} elseif ('prependPath' === $call[0]) {
$prependPaths[] = $call[1];
}
}

Expand All @@ -204,18 +201,24 @@ public function testTwigLoaderPaths($format)
array('namespaced_path1', 'namespace1'),
array('namespaced_path2', 'namespace2'),
array('namespaced_path3', 'namespace3'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildChildChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'Twig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'Twig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'Twig'),
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'Twig'),
array(__DIR__.'/Fixtures/Resources/TwigBundle/views', 'Twig'),
array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'ChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
array(__DIR__.'/Fixtures/Resources/views'),
), $paths);

$this->assertEquals(array(
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'Twig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildTwig'),
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'Twig'),
), $prependPaths);
}

public function getFormats()
Expand Down Expand Up @@ -269,8 +272,15 @@ private function createContainer()
'TwigBundle' => 'Symfony\\Bundle\\TwigBundle\\TwigBundle',
'ChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildTwigBundle\\ChildTwigBundle',
'ChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildTwigBundle\\ChildChildTwigBundle',
'ChildChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildTwigBundle\\ChildChildChildTwigBundle',
'ChildChildChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildChildTwigBundle\\ChildChildChildChildTwigBundle',
),
'kernel.bundles_metadata' => array(
'ChildChildChildChildTwigBundle' => array(
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildChildTwigBundle\\ChildChildChildChildTwigBundle',
'parent' => 'ChildChildChildTwigBundle',
'path' => __DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle',
),
'TwigBundle' => array(
'namespace' => 'Symfony\\Bundle\\TwigBundle',
'parent' => null,
Expand All @@ -281,6 +291,11 @@ private function createContainer()
'parent' => 'TwigBundle',
'path' => __DIR__.'/Fixtures/Bundle/ChildTwigBundle',
),
'ChildChildChildTwigBundle' => array(
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildTwigBundle\\ChildChildChildTwigBundle',
'parent' => 'ChildChildTwigBundle',
'path' => __DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle',
),
'ChildChildTwigBundle' => array(
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildTwigBundle\\ChildChildTwigBundle',
'parent' => 'ChildTwigBundle',
Expand Down
0