From 56892819e35ab2417ac989f55fc6faf93c61996e Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Thu, 25 May 2017 12:05:01 +0200 Subject: [PATCH] [DI] Avoid private call to Container::has() --- .../DependencyInjection/Dumper/PhpDumper.php | 8 ++++++++ .../Tests/Dumper/PhpDumperTest.php | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 28cad05b5952d..4431418d85848 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -1191,9 +1191,17 @@ private function wrapServiceConditionals($value, $code) $conditions = array(); foreach ($services as $service) { + if ($this->container->hasDefinition($service) && !$this->container->getDefinition($service)->isPublic()) { + continue; + } + $conditions[] = sprintf("\$this->has('%s')", $service); } + if (!$conditions) { + return $code; + } + // re-indent the wrapped code $code = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code))); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index c63d5ec18315c..2ad701aa6d327 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Dumper\PhpDumper; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; use Symfony\Component\DependencyInjection\Reference; @@ -406,4 +407,21 @@ public function testDumpContainerBuilderWithFrozenConstructorIncludingPrivateSer $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_private_frozen.php', $dumper->dump()); } + + public function testPrivateWithIgnoreOnInvalidReference() + { + require_once self::$fixturesPath.'/includes/classes.php'; + + $container = new ContainerBuilder(); + $container->register('not_invalid', 'BazClass') + ->setPublic(false); + $container->register('bar', 'BarClass') + ->addMethodCall('setBaz', array(new Reference('not_invalid', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))); + + $dumper = new PhpDumper($container); + eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Private_With_Ignore_On_Invalid_Reference'))); + + $container = new \Symfony_DI_PhpDumper_Test_Private_With_Ignore_On_Invalid_Reference(); + $this->assertInstanceOf('BazClass', $container->get('bar')->getBaz()); + } }