From e5d0934b87ee4d7bb5fe8948e19a4849dbadcb30 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 6 Oct 2017 19:34:39 +0200 Subject: [PATCH] [DI] Turn private defs to non-public ones before removing passes --- .../Compiler/PassConfig.php | 6 +++ .../Compiler/ResolveChildDefinitionsPass.php | 23 ----------- .../Compiler/ResolvePrivatesPass.php | 40 +++++++++++++++++++ .../ResolveChildDefinitionsPassTest.php | 20 ---------- .../Compiler/ResolvePrivatesPassTest.php | 39 ++++++++++++++++++ 5 files changed, 85 insertions(+), 43 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Compiler/ResolvePrivatesPass.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolvePrivatesPassTest.php diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php b/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php index 6c0448ecb4e8..bfae5ae6d677 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php @@ -71,6 +71,12 @@ public function __construct() new CheckArgumentsValidityPass(false), )); + $this->beforeRemovingPasses = array( + -100 => array( + new ResolvePrivatesPass(), + ), + ); + $this->removingPasses = array(array( new RemovePrivateAliasesPass(), new ReplaceAliasByActualDefinitionPass(), diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveChildDefinitionsPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveChildDefinitionsPass.php index 460fb55e472b..a124e472aca8 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveChildDefinitionsPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveChildDefinitionsPass.php @@ -12,7 +12,6 @@ namespace Symfony\Component\DependencyInjection\Compiler; use Symfony\Component\DependencyInjection\ChildDefinition; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Exception\ExceptionInterface; use Symfony\Component\DependencyInjection\Exception\RuntimeException; @@ -26,28 +25,6 @@ */ class ResolveChildDefinitionsPass extends AbstractRecursivePass { - /** - * {@inheritdoc} - */ - public function process(ContainerBuilder $container) - { - parent::process($container); - - foreach ($container->getDefinitions() as $definition) { - if ($definition->isPrivate()) { - $definition->setPublic(false); - $definition->setPrivate(true); - } - } - - foreach ($container->getAliases() as $alias) { - if ($alias->isPrivate()) { - $alias->setPublic(false); - $alias->setPrivate(true); - } - } - } - protected function processValue($value, $isRoot = false) { if (!$value instanceof Definition) { diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolvePrivatesPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolvePrivatesPass.php new file mode 100644 index 000000000000..1bd993458a40 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolvePrivatesPass.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** + * @author Nicolas Grekas + */ +class ResolvePrivatesPass implements CompilerPassInterface +{ + /** + * {@inheritdoc} + */ + public function process(ContainerBuilder $container) + { + foreach ($container->getDefinitions() as $id => $definition) { + if ($definition->isPrivate()) { + $definition->setPublic(false); + $definition->setPrivate(true); + } + } + + foreach ($container->getAliases() as $id => $alias) { + if ($alias->isPrivate()) { + $alias->setPublic(false); + $alias->setPrivate(true); + } + } + } +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveChildDefinitionsPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveChildDefinitionsPassTest.php index c8135b965e80..23a1915fd777 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveChildDefinitionsPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveChildDefinitionsPassTest.php @@ -397,26 +397,6 @@ public function testSetAutoconfiguredOnServiceIsParent() $this->assertFalse($container->getDefinition('child1')->isAutoconfigured()); } - public function testPrivateHasHigherPrecedenceThanPublic() - { - $container = new ContainerBuilder(); - - $container->register('foo', 'stdClass') - ->setPublic(true) - ->setPrivate(true) - ; - - $container->setAlias('bar', 'foo') - ->setPublic(false) - ->setPrivate(false) - ; - - $this->process($container); - - $this->assertFalse($container->getDefinition('foo')->isPublic()); - $this->assertFalse($container->getAlias('bar')->isPublic()); - } - /** * @group legacy */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolvePrivatesPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolvePrivatesPassTest.php new file mode 100644 index 000000000000..89af24f2c9c8 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolvePrivatesPassTest.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Compiler; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\Compiler\ResolvePrivatesPass; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +class ResolvePrivatesPassTest extends TestCase +{ + public function testPrivateHasHigherPrecedenceThanPublic() + { + $container = new ContainerBuilder(); + + $container->register('foo', 'stdClass') + ->setPublic(true) + ->setPrivate(true) + ; + + $container->setAlias('bar', 'foo') + ->setPublic(false) + ->setPrivate(false) + ; + + (new ResolvePrivatesPass())->process($container); + + $this->assertFalse($container->getDefinition('foo')->isPublic()); + $this->assertFalse($container->getAlias('bar')->isPublic()); + } +}