From e0eb2472295c614ccb8f942cd3bc68d19b706de0 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sat, 20 May 2017 16:32:28 +0200 Subject: [PATCH] [DI] Deprecate Container::initialized() for privates --- .../DependencyInjection/CHANGELOG.md | 5 ++++ .../DependencyInjection/Container.php | 4 +++ .../Tests/ContainerTest.php | 25 +++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index a85de7dff882..6ae0daa20efb 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +3.4.0 +----- + + * deprecated the ability to check for the initialization of a private service with the `Container::initialized()` method + 3.3.0 ----- diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 05e038ac5cb6..08ceec494883 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -370,6 +370,10 @@ public function initialized($id) $id = $this->aliases[$id]; } + if (isset($this->privates[$id])) { + @trigger_error(sprintf('Checking for the initialization of the "%s" private service is deprecated since Symfony 3.4 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); + } + return isset($this->services[$id]); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index 99419201c638..9a49060fa5e6 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -147,7 +147,7 @@ public function testGetServiceIds() $sc = new ProjectServiceContainer(); $sc->set('foo', $obj = new \stdClass()); - $this->assertEquals(array('service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()'); + $this->assertEquals(array('service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'internal_dependency', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()'); } /** @@ -363,6 +363,17 @@ public function testInitialized() $this->assertTrue($sc->initialized('alias'), '->initialized() returns true for alias if aliased service is initialized'); } + /** + * @group legacy + * @expectedDeprecation Checking for the initialization of the "internal" private service is deprecated since Symfony 3.4 and won't be supported anymore in Symfony 4.0. + */ + public function testInitializedWithPrivateService() + { + $sc = new ProjectServiceContainer(); + $sc->get('internal_dependency'); + $this->assertTrue($sc->initialized('internal')); + } + public function testReset() { $c = new Container(); @@ -504,6 +515,7 @@ class ProjectServiceContainer extends Container 'circular' => 'getCircularService', 'throw_exception' => 'getThrowExceptionService', 'throws_exception_on_service_configuration' => 'getThrowsExceptionOnServiceConfigurationService', + 'internal_dependency' => 'getInternalDependencyService', ); public function __construct() @@ -520,7 +532,7 @@ public function __construct() protected function getInternalService() { - return $this->__internal; + return $this->services['internal'] = $this->__internal; } protected function getBarService() @@ -554,6 +566,15 @@ protected function getThrowsExceptionOnServiceConfigurationService() throw new \Exception('Something was terribly wrong while trying to configure the service!'); } + + protected function getInternalDependencyService() + { + $this->services['internal_dependency'] = $instance = new \stdClass(); + + $instance->internal = isset($this->services['internal']) ? $this->services['internal'] : $this->getInternalService(); + + return $instance; + } } class LegacyProjectServiceContainer extends Container