From e27a04b0ff89109a1f467b8e7420cf8188dfe7a7 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sat, 14 Jan 2017 10:13:01 +0100 Subject: [PATCH] [DI] Deprecate Container::set for nulls, initialized and alias services --- .../Tests/Templating/PhpEngineTest.php | 3 +- .../DependencyInjection/Container.php | 30 ++++++----- .../Tests/ContainerTest.php | 53 ++++++++++++++++--- .../Tests/Dumper/PhpDumperTest.php | 11 +++- .../Tests/Fixtures/containers/container9.php | 1 + 5 files changed, 74 insertions(+), 24 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php index b5eeb901342db..2cf3d4a18d1bb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php @@ -38,8 +38,7 @@ public function testEvaluateWithoutAvailableRequest() $loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader'); $engine = new PhpEngine(new TemplateNameParser(), $container, $loader, new GlobalVariables($container)); - $container->set('request_stack', null); - + $this->assertFalse($container->has('request_stack')); $globals = $engine->getGlobals(); $this->assertEmpty($globals['app']->getRequest()); } diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 43b210ea6811b..d614ef408a7cb 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -161,9 +161,6 @@ public function setParameter($name, $value) /** * Sets a service. * - * Setting a service to null resets the service: has() returns false and get() - * behaves in the same way as if the service was never created. - * * @param string $id The service identifier * @param object $service The service instance */ @@ -175,16 +172,6 @@ public function set($id, $service) throw new InvalidArgumentException('You cannot set service "service_container".'); } - if (isset($this->aliases[$id])) { - unset($this->aliases[$id]); - } - - $this->services[$id] = $service; - - if (null === $service) { - unset($this->services[$id]); - } - if (isset($this->privates[$id])) { if (null === $service) { @trigger_error(sprintf('Unsetting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); @@ -192,7 +179,24 @@ public function set($id, $service) } else { @trigger_error(sprintf('Setting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0. A new public service will be created instead.', $id), E_USER_DEPRECATED); } + } elseif (null === $service) { + @trigger_error(sprintf('Unsetting the "%s" service is deprecated since Symfony 3.3 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); + unset($this->services[$id], $this->aliases[$id], $this->methodMap[$id]); + + return; + } + + if (isset($this->aliases[$id])) { + @trigger_error(sprintf('Replacing the "%s" service alias is deprecated since Symfony 3.3 and will set the aliased service instead in Symfony 4.0.', $id), E_USER_DEPRECATED); + unset($this->aliases[$id]); + } elseif (isset($this->services[$id])) { + @trigger_error(sprintf('Replacing the initialized "%s" service is deprecated since Symfony 3.3 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); + } elseif (isset($this->methodMap[$id])) { + @trigger_error(sprintf('Replacing the pre-defined "%s" service is deprecated since Symfony 3.3 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); + unset($this->methodMap[$id]); } + + $this->services[$id] = $service; } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index 67d73f53a0689..e97e16ffc9c1e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -148,13 +148,27 @@ public function testSet() $this->assertSame($foo, $sc->get('foo'), '->set() sets a service'); } + /** + * @group legacy + * @expectedDeprecation Unsetting the "foo" service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. + * @expectedDeprecation Unsetting the "bar" service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. + */ public function testSetWithNullResetTheService() { $sc = new Container(); + $sc->set('foo', new \stdClass()); $sc->set('foo', null); $this->assertFalse($sc->has('foo'), '->set() with null service resets the service'); + + $sc = new ProjectServiceContainer(); + $sc->set('bar', null); + $this->assertFalse($sc->has('bar'), '->set() with null service resets the pre-defined service'); } + /** + * @group legacy + * @expectedDeprecation Replacing the "alias" service alias is deprecated since Symfony 3.3 and will set the aliased service instead in Symfony 4.0. + */ public function testSetReplacesAlias() { $c = new ProjectServiceContainer(); @@ -172,9 +186,6 @@ public function testGet() $this->assertSame($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined'); $this->assertSame($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined'); - $sc->set('bar', $bar = new \stdClass()); - $this->assertSame($bar, $sc->get('bar'), '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()'); - try { $sc->get(''); $this->fail('->get() throws a \InvalidArgumentException exception if the service is empty'); @@ -337,7 +348,7 @@ public function testInitialized() $this->assertFalse($sc->initialized('bar'), '->initialized() returns false if a service is defined, but not currently loaded'); $this->assertFalse($sc->initialized('alias'), '->initialized() returns false if an aliased service is not initialized'); - $sc->set('bar', new \stdClass()); + $sc->get('bar'); $this->assertTrue($sc->initialized('alias'), '->initialized() returns true for alias if aliased service is initialized'); } @@ -453,6 +464,32 @@ public function testRequestAnInternalSharedPrivateServiceIsDeprecated() $c = new ProjectServiceContainer(); $c->get('internal'); } + + /** + * @group legacy + * @expectedDeprecation Replacing the pre-defined "bar" service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. + */ + public function testReplacingAPreDefinedServiceIsDeprecated() + { + $c = new ProjectServiceContainer(); + $c->set('bar', new \stdClass()); + $c->set('bar', $bar = new \stdClass()); + + $this->assertSame($bar, $c->get('bar'), '->set() replaces a pre-defined service'); + } + + /** + * @group legacy + * @expectedDeprecation Replacing the initialized "new" service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. + */ + public function testReplacingAnInitializedServiceIsDeprecated() + { + $c = new ProjectServiceContainer(); + $c->set('new', new \stdClass()); + $c->set('new', $new = new \stdClass()); + + $this->assertEquals($new, $c->get('new'), '->set() replaces an initialized service'); + } } class ProjectServiceContainer extends Container @@ -485,22 +522,22 @@ public function __construct() protected function getInternalService() { - return $this->__internal; + return $this->services['internal'] = $this->__internal; } protected function getBarService() { - return $this->__bar; + return $this->services['bar'] = $this->__bar; } protected function getFooBarService() { - return $this->__foo_bar; + return $this->services['foo_bar'] = $this->__foo_bar; } protected function getFoo_BazService() { - return $this->__foo_baz; + return $this->services['foo.baz'] = $this->__foo_baz; } protected function getCircularService() diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 6efc2e9a3900c..9df16de7ebfe1 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -240,12 +240,13 @@ public function provideInvalidFactories() public function testAliases() { $container = include self::$fixturesPath.'/containers/container9.php'; + $container->setParameter('foo_bar', 'foo_bar'); $container->compile(); $dumper = new PhpDumper($container); eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Aliases'))); $container = new \Symfony_DI_PhpDumper_Test_Aliases(); - $container->set('foo', $foo = new \stdClass()); + $foo = $container->get('foo'); $this->assertSame($foo, $container->get('foo')); $this->assertSame($foo, $container->get('alias_for_foo')); $this->assertSame($foo, $container->get('alias_for_alias')); @@ -263,6 +264,10 @@ public function testFrozenContainerWithoutAliases() $this->assertFalse($container->has('foo')); } + /** + * @group legacy + * @expectedDeprecation Replacing the pre-defined "bar" service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. + */ public function testOverrideServiceWhenUsingADumpedContainer() { require_once self::$fixturesPath.'/php/services9.php'; @@ -275,6 +280,10 @@ public function testOverrideServiceWhenUsingADumpedContainer() $this->assertSame($bar, $container->get('bar'), '->set() overrides an already defined service'); } + /** + * @group legacy + * @expectedDeprecation Replacing the pre-defined "bar" service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. + */ public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne() { require_once self::$fixturesPath.'/php/services9.php'; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php index 91e32b52fe815..6d1cc0cb3a9f2 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php @@ -1,6 +1,7 @@