diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 0d7e9dafef483..0997f78b46749 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -235,6 +235,11 @@ public function has($id) */ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) { + $triggerPrivateDeprecations = true; + if (func_num_args() > 2) { + $triggerPrivateDeprecations = func_get_arg(2); + } + // Attempt to retrieve the service by checking first aliases then // available services. Service IDs are case insensitive, however since // this method can be called thousands of times during a request, avoid @@ -281,7 +286,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE return; } - if (isset($this->privates[$id])) { + if ($triggerPrivateDeprecations && isset($this->privates[$id])) { @trigger_error(sprintf('Requesting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); } diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 210b13eb84601..706e81a0f77b6 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -1377,13 +1377,13 @@ private function getServiceCall($id, Reference $reference = null) } if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) { - return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id); + return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE, false)', $id); } else { if ($this->container->hasAlias($id)) { $id = (string) $this->container->getAlias($id); } - return sprintf('$this->get(\'%s\')', $id); + return sprintf('$this->get(\'%s\', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)', $id); } } diff --git a/src/Symfony/Component/DependencyInjection/ExpressionLanguageProvider.php b/src/Symfony/Component/DependencyInjection/ExpressionLanguageProvider.php index ce6d69522e191..998a9ea0ae35b 100644 --- a/src/Symfony/Component/DependencyInjection/ExpressionLanguageProvider.php +++ b/src/Symfony/Component/DependencyInjection/ExpressionLanguageProvider.php @@ -28,9 +28,9 @@ public function getFunctions() { return array( new ExpressionFunction('service', function ($arg) { - return sprintf('$this->get(%s)', $arg); + return sprintf('$this->get(%s, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)', $arg); }, function (array $variables, $value) { - return $variables['container']->get($value); + return $variables['container']->get($value, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false); }), new ExpressionFunction('parameter', function ($arg) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index d155a9ac763e2..9b19ebd06d740 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -145,6 +145,34 @@ public function testServicesWithAnonymousFactories() $this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories'); } + public function testPrivateServices() + { + $fooDefinition = new Definition('stdClass'); + $fooDefinition->setProperty('value', 'something'); + $fooDefinition->setPublic(false); + + $fooUserDefinition = new Definition('stdClass'); + $fooUserDefinition->setProperty('foo', new Reference('foo')); + + $builder = new ContainerBuilder(); + $builder->setResourceTracking(false); + + $builder->addDefinitions(array( + 'foo' => $fooDefinition, + 'foo_user' => $fooUserDefinition, + )); + + $dumper = new PhpDumper($builder); + $class = 'Symfony_DI_PhpDumper_Test_Private_Service'; + eval('?>'.$dumper->dump(array('class' => $class))); + + $container = new $class(); + + $expected = new \stdClass(); + $expected->value = 'something'; + $this->assertEquals($expected, $container->get('foo_user')->foo); + } + public function testAddServiceIdWithUnsupportedCharacters() { $class = 'Symfony_DI_PhpDumper_Test_Unsupported_Characters'; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php index f77ae5dea1cdb..a4a9cecf14200 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php @@ -72,7 +72,7 @@ public function __construct() */ protected function getBarService() { - $a = $this->get('foo.baz'); + $a = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false); $this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar')); @@ -93,7 +93,7 @@ protected function getBazService() { $this->services['baz'] = $instance = new \Baz(); - $instance->setFoo($this->get('foo_with_inline')); + $instance->setFoo($this->get('foo_with_inline', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)); return $instance; } @@ -110,7 +110,7 @@ protected function getConfiguredServiceService() { $this->services['configured_service'] = $instance = new \stdClass(); - $this->get('configurator_service')->configureStdClass($instance); + $this->get('configurator_service', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->configureStdClass($instance); return $instance; } @@ -127,7 +127,7 @@ protected function getConfiguredServiceSimpleService() { $this->services['configured_service_simple'] = $instance = new \stdClass(); - $this->get('configurator_service_simple')->configureStdClass($instance); + $this->get('configurator_service_simple', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->configureStdClass($instance); return $instance; } @@ -198,7 +198,7 @@ protected function getDeprecatedServiceService() */ protected function getFactoryServiceService() { - return $this->services['factory_service'] = $this->get('foo.baz')->getInstance(); + return $this->services['factory_service'] = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->getInstance(); } /** @@ -211,7 +211,7 @@ protected function getFactoryServiceService() */ protected function getFactoryServiceSimpleService() { - return $this->services['factory_service_simple'] = $this->get('factory_simple')->getInstance(); + return $this->services['factory_service_simple'] = $this->get('factory_simple', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->getInstance(); } /** @@ -224,11 +224,11 @@ protected function getFactoryServiceSimpleService() */ protected function getFooService() { - $a = $this->get('foo.baz'); + $a = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false); $this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo')), true, $this); - $instance->setBar($this->get('bar')); + $instance->setBar($this->get('bar', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)); $instance->initialize(); $instance->foo = 'bar'; $instance->moo = $a; @@ -279,7 +279,7 @@ protected function getFooWithInlineService() { $this->services['foo_with_inline'] = $instance = new \Foo(); - $instance->setBar($this->get('inlined')); + $instance->setBar($this->get('inlined', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)); return $instance; } @@ -298,15 +298,15 @@ protected function getMethodCall1Service() $this->services['method_call1'] = $instance = new \Bar\FooClass(); - $instance->setBar($this->get('foo')); - $instance->setBar($this->get('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE)); + $instance->setBar($this->get('foo', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)); + $instance->setBar($this->get('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE, false)); if ($this->has('foo3')) { - $instance->setBar($this->get('foo3', ContainerInterface::NULL_ON_INVALID_REFERENCE)); + $instance->setBar($this->get('foo3', ContainerInterface::NULL_ON_INVALID_REFERENCE, false)); } if ($this->has('foobaz')) { - $instance->setBar($this->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE)); + $instance->setBar($this->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE, false)); } - $instance->setBar(($this->get("foo")->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default")))); + $instance->setBar(($this->get("foo", ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default")))); return $instance; } @@ -321,7 +321,7 @@ protected function getMethodCall1Service() */ protected function getNewFactoryServiceService() { - $this->services['new_factory_service'] = $instance = $this->get('new_factory')->getInstance(); + $this->services['new_factory_service'] = $instance = $this->get('new_factory', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->getInstance(); $instance->foo = 'bar'; @@ -370,7 +370,7 @@ protected function getConfiguratorServiceService() { $this->services['configurator_service'] = $instance = new \ConfClass(); - $instance->setFoo($this->get('baz')); + $instance->setFoo($this->get('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)); return $instance; } @@ -425,7 +425,7 @@ protected function getInlinedService() { $this->services['inlined'] = $instance = new \Bar(); - $instance->setBaz($this->get('baz')); + $instance->setBaz($this->get('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)); $instance->pub = 'pub'; return $instance; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index bcbd4a67b6b9d..ef66287915aff 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -70,7 +70,7 @@ public function compile() */ protected function getBarService() { - $a = $this->get('foo.baz'); + $a = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false); $this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar')); @@ -91,7 +91,7 @@ protected function getBazService() { $this->services['baz'] = $instance = new \Baz(); - $instance->setFoo($this->get('foo_with_inline')); + $instance->setFoo($this->get('foo_with_inline', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)); return $instance; } @@ -107,7 +107,7 @@ protected function getBazService() protected function getConfiguredServiceService() { $a = new \ConfClass(); - $a->setFoo($this->get('baz')); + $a->setFoo($this->get('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)); $this->services['configured_service'] = $instance = new \stdClass(); @@ -186,7 +186,7 @@ protected function getDeprecatedServiceService() */ protected function getFactoryServiceService() { - return $this->services['factory_service'] = $this->get('foo.baz')->getInstance(); + return $this->services['factory_service'] = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->getInstance(); } /** @@ -212,11 +212,11 @@ protected function getFactoryServiceSimpleService() */ protected function getFooService() { - $a = $this->get('foo.baz'); + $a = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false); $this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this); - $instance->setBar($this->get('bar')); + $instance->setBar($this->get('bar', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)); $instance->initialize(); $instance->foo = 'bar'; $instance->moo = $a; @@ -267,7 +267,7 @@ protected function getFooWithInlineService() $this->services['foo_with_inline'] = $instance = new \Foo(); - $a->setBaz($this->get('baz')); + $a->setBaz($this->get('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)); $a->pub = 'pub'; $instance->setBar($a); @@ -289,9 +289,9 @@ protected function getMethodCall1Service() $this->services['method_call1'] = $instance = new \Bar\FooClass(); - $instance->setBar($this->get('foo')); + $instance->setBar($this->get('foo', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)); $instance->setBar(NULL); - $instance->setBar(($this->get("foo")->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default")))); + $instance->setBar(($this->get("foo", ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default")))); return $instance; }