Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TwigBundle] Adds bundle view path in a delayed compiler pass #30527

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 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;

use Symfony\Component\Config\Resource\FileExistenceResource;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Registers the bundles view paths.
*/
final class BundleViewPathPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$twigFilesystemLoaderDefinition = $container->findDefinition('twig.loader.filesystem');

foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
if (file_exists($dir = $bundle['path'].'/Resources/views')) {
$namespace = $this->normalizeBundleName($name);
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$dir, $namespace]);
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$dir, '!'.$namespace]);
}
$container->addResource(new FileExistenceResource($dir));
}
}

private function normalizeBundleName(string $name): string
{
if ('Bundle' === substr($name, -6)) {
$name = substr($name, 0, -6);
}

return $name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ public function load(array $configs, ContainerBuilder $container)
foreach ($paths as $path) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, $namespace]);
}

if ($paths) {
// the last path must be the bundle views directory
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, '!'.$namespace]);
}
}

if (file_exists($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) {
Expand Down Expand Up @@ -187,11 +182,6 @@ private function getBundleTemplatePaths(ContainerBuilder $container, array $conf
$bundleHierarchy[$name][] = $defaultOverrideBundlePath;
}
$container->addResource(new FileExistenceResource($defaultOverrideBundlePath));

if (file_exists($dir = $bundle['path'].'/Resources/views')) {
$bundleHierarchy[$name][] = $dir;
}
$container->addResource(new FileExistenceResource($dir));
}

return $bundleHierarchy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This is a layout
This is user-defined BarBundle layout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This is a layout
This is TwigBundle layout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is base BarBundle layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ThirdParty\BarExtensionBundle;

use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use ThirdParty\BarExtensionBundle\DependencyInjection\Compiler\OverrideBarBundlePathPass;

class BarExtensionBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new OverrideBarBundlePathPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ThirdParty\BarExtensionBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use ThirdParty\BarExtensionBundle\BarExtensionBundle;

class OverrideBarBundlePathPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$twigLoaderFilesystemDefinition = $container->findDefinition('twig.loader.filesystem');

// Replaces native Bar templates
$barExtensionBundleRefl = new \ReflectionClass(BarExtensionBundle::class);
if ($barExtensionBundleRefl->isUserDefined()) {
$barExtensionBundlePath = \dirname((string) $barExtensionBundleRefl->getFileName());
$barExtensionBundleTwigPath = $barExtensionBundlePath.'/Resources/views';
$twigLoaderFilesystemDefinition->addMethodCall(
'addPath',
[$barExtensionBundleTwigPath, 'Bar']
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is BarExtensionBundle override of BarBundle layout
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection;

use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\BundleViewPathPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExtensionPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\RuntimeLoaderPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\TwigExtension;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
Expand All @@ -22,6 +24,7 @@
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use ThirdParty\BarExtensionBundle\DependencyInjection\Compiler\OverrideBarBundlePathPass;

class TwigExtensionTest extends TestCase
{
Expand Down Expand Up @@ -177,10 +180,13 @@ public function testTwigLoaderPaths($format)
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'full', $format);
$this->loadFromFile($container, 'extra', $format);
$container->addCompilerPass(new ExtensionPass());
$container->addCompilerPass(new BundleViewPathPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -16);
$this->compileContainer($container);

$def = $container->getDefinition('twig.loader.native_filesystem');
$def = $container->findDefinition('twig.loader.filesystem');
$paths = [];

foreach ($def->getMethodCalls() as $call) {
if ('addPath' === $call[0] && false === strpos($call[1][0], 'Form')) {
$paths[] = $call[1];
Expand All @@ -194,9 +200,9 @@ public function testTwigLoaderPaths($format)
['namespaced_path2', 'namespace2'],
['namespaced_path3', 'namespace3'],
[__DIR__.'/Fixtures/templates/bundles/TwigBundle', 'Twig'],
[__DIR__.'/Fixtures/templates'],
[realpath(__DIR__.'/../..').'/Resources/views', 'Twig'],
[realpath(__DIR__.'/../..').'/Resources/views', '!Twig'],
[__DIR__.'/Fixtures/templates'],
], $paths);
}

Expand All @@ -212,9 +218,11 @@ public function testLegacyTwigLoaderPaths($format)
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'full', $format);
$this->loadFromFile($container, 'extra', $format);
$container->addCompilerPass(new ExtensionPass());
$container->addCompilerPass(new BundleViewPathPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -16);
$this->compileContainer($container);

$def = $container->getDefinition('twig.loader.native_filesystem');
$def = $container->findDefinition('twig.loader.filesystem');
$paths = [];
foreach ($def->getMethodCalls() as $call) {
if ('addPath' === $call[0] && false === strpos($call[1][0], 'Form')) {
Expand All @@ -230,10 +238,82 @@ public function testLegacyTwigLoaderPaths($format)
['namespaced_path3', 'namespace3'],
[__DIR__.'/../Fixtures/templates/Resources/TwigBundle/views', 'Twig'],
[__DIR__.'/Fixtures/templates/bundles/TwigBundle', 'Twig'],
[realpath(__DIR__.'/../..').'/Resources/views', 'Twig'],
[realpath(__DIR__.'/../..').'/Resources/views', '!Twig'],
[__DIR__.'/../Fixtures/templates/Resources/views'],
[__DIR__.'/Fixtures/templates'],
[realpath(__DIR__.'/../..').'/Resources/views', 'Twig'],
[realpath(__DIR__.'/../..').'/Resources/views', '!Twig'],
], $paths);
}

/**
* @dataProvider getFormats
*/
public function testThirdPartyBundlesMayOverrideBetweenThem($format)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'full', $format);
$this->loadFromFile($container, 'extra', $format);
$container->addCompilerPass(new ExtensionPass());
$container->addCompilerPass(new BundleViewPathPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -16);
// Adds 2 third-party bundles whom second overrides first one's templates
require_once __DIR__.'/Fixtures/third-party/BarExtensionBundle/BarExtensionBundle.php';
require_once __DIR__.'/Fixtures/third-party/BarExtensionBundle/DependencyInjection/Compiler/OverrideBarBundlePathPass.php';
$container->setParameter(
'kernel.bundles',
\array_merge(
$container->getParameter('kernel.bundles'),
[
'BarBundle' => 'ThirdParty\\BarBundle\\BarBundle',
'BarExtensionBundle' => 'ThirdParty\\BarExtensionBundle\\BarExtensionBundle',
]
)
);
$container->setParameter(
'kernel.bundles_metadata',
\array_merge(
$container->getParameter('kernel.bundles_metadata'),
[
'BarBundle' => [
'namespace' => 'ThirdParty\\BarBundle',
'path' => __DIR__.'/Fixtures/third-party/BarBundle',
],
'BarExtensionBundle' => [
'namespace' => 'ThirdParty\\BarExtensionBundle',
'path' => __DIR__.'/Fixtures/third-party/BarExtensionBundle',
],
]
)
);
$container->addCompilerPass(new OverrideBarBundlePathPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);

$this->compileContainer($container);

$def = $container->findDefinition('twig.loader.filesystem');
$paths = [];

foreach ($def->getMethodCalls() as $call) {
if ('addPath' === $call[0] && false === strpos($call[1][0], 'Form')) {
$paths[] = $call[1];
}
}

$this->assertEquals([
['path1'],
['path2'],
['namespaced_path1', 'namespace1'],
['namespaced_path2', 'namespace2'],
['namespaced_path3', 'namespace3'],
[realpath(__DIR__).'/Fixtures/templates/bundles/TwigBundle', 'Twig'],
[realpath(__DIR__).'/Fixtures/templates/bundles/BarBundle', 'Bar'],
[realpath(__DIR__).'/Fixtures/templates'],
[realpath(__DIR__.'/Fixtures/third-party/BarExtensionBundle').'/Resources/views', 'Bar'],
[realpath(__DIR__.'/../..').'/Resources/views', 'Twig'],
[realpath(__DIR__.'/../..').'/Resources/views', '!Twig'],
[realpath(__DIR__).'/Fixtures/third-party/BarBundle/Resources/views', 'Bar'],
[realpath(__DIR__).'/Fixtures/third-party/BarBundle/Resources/views', '!Bar'],
[realpath(__DIR__).'/Fixtures/third-party/BarExtensionBundle/Resources/views', 'BarExtension'],
[realpath(__DIR__).'/Fixtures/third-party/BarExtensionBundle/Resources/views', '!BarExtension'],
], $paths);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/TwigBundle/TwigBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\TwigBundle;

use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\BundleViewPathPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExceptionListenerPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExtensionPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\RuntimeLoaderPass;
Expand All @@ -36,6 +37,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new TwigEnvironmentPass());
$container->addCompilerPass(new TwigLoaderPass());
$container->addCompilerPass(new ExceptionListenerPass());
$container->addCompilerPass(new BundleViewPathPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -16);
$container->addCompilerPass(new RuntimeLoaderPass(), PassConfig::TYPE_BEFORE_REMOVING);
}

Expand Down