8000 [DI] Avoid private call to Container::has() · symfony/symfony@5689281 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5689281

Browse files
committed
[DI] Avoid private call to Container::has()
1 parent b0b7a36 commit 5689281

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,9 +1191,17 @@ private function wrapServiceConditionals($value, $code)
11911191

11921192
$conditions = array();
11931193
foreach ($services as $service) {
1194+
if ($this->container->hasDefinition($service) && !$this->container->getDefinition($service)->isPublic()) {
1195+
continue;
1196+
}
1197+
11941198
$conditions[] = sprintf("\$this->has('%s')", $service);
11951199
}
11961200

1201+
if (!$conditions) {
1202+
return $code;
1203+
}
1204+
11971205
// re-indent the wrapped code
11981206
$code = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code)));
11991207

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Component\Config\FileLocator;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\ContainerInterface;
1819
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
1920
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2021
use Symfony\Component\DependencyInjection\Reference;
@@ -406,4 +407,21 @@ public function testDumpContainerBuilderWithFrozenConstructorIncludingPrivateSer
406407

407408
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_private_frozen.php', $dumper->dump());
408409
}
410+
411+
public function testPrivateWithIgnoreOnInvalidReference()
412+
{
413+
require_once self::$fixturesPath.'/includes/classes.php';
414+
415+
$container = new ContainerBuilder();
416+
$container->register('not_invalid', 'BazClass')
417+
->setPublic(false);
418+
$container->register('bar', 'BarClass')
419+
->addMethodCall('setBaz', array(new Reference('not_invalid', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
420+
421+
$dumper = new PhpDumper($container);
422+
eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Private_With_Ignore_On_Invalid_Reference')));
423+
424+
$container = new \Symfony_DI_PhpDumper_Test_Private_With_Ignore_On_Invalid_Reference();
425+
$this->assertInstanceOf('BazClass', $container->get('bar')->getBaz());
426+
}
409427
}

0 commit comments

Comments
 (0)
0