From d314b1ff628295ef085f93463bfcc125fd65e6b3 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 4 Oct 2017 09:45:46 +0200 Subject: [PATCH 1/2] [DI] Allow setting any public non-initialized services --- .../DependencyInjection/Container.php | 7 ++++--- .../Tests/ContainerTest.php | 20 ++++++++++++++++--- .../Tests/Dumper/PhpDumperTest.php | 9 ++++++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 6eceb0defe..2d43ba7a60 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -190,6 +190,7 @@ class Container implements ResettableContainerInterface unset($this->aliases[$id]); } + $wasSet = isset($this->services[$id]); $this->services[$id] = $service; if (null === $service) { @@ -203,11 +204,11 @@ class Container implements ResettableContainerInterface } 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.', $id), E_USER_DEPRECATED); } - } elseif (isset($this->methodMap[$id])) { + } elseif ($wasSet && isset($this->methodMap[$id])) { if (null === $service) { - @trigger_error(sprintf('Unsetting the "%s" pre-defined service is deprecated since Symfony 3.3 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); + @trigger_error(sprintf('Unsetting the "%s" service after it\'s been initialized is deprecated since Symfony 3.3 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); } else { - @trigger_error(sprintf('Setting the "%s" pre-defined service is deprecated since Symfony 3.3 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); + @trigger_error(sprintf('Setting the "%s" service after it\'s been initialized is deprecated since Symfony 3.3 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); } } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index e2478ea22b..5cd988714a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -186,15 +186,29 @@ class ContainerTest extends TestCase /** * @group legacy - * @expectedDeprecation Unsetting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. + * @expectedDeprecation Unsetting the "bar" service after it's been initialized is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. */ - public function testSetWithNullResetPredefinedService() + public function testSetWithNullOnInitializedPredefinedService() { $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->get('bar'); + $sc->set('bar', null); + $this->assertTrue($sc->has('bar'), '->set() with null service resets the pre-defined service'); + } + + public function testSetWithNullOnUninitializedPredefinedService() + { + $sc = new Container(); + $sc->set('foo', new \stdClass()); + $sc->get('foo', null); + $sc->set('foo', null); + $this->assertFalse($sc->has('foo'), '->set() with null service resets the service'); + $sc = new ProjectServiceContainer(); $sc->set('bar', null); $this->assertTrue($sc->has('bar'), '->set() with null service resets the pre-defined service'); @@ -481,7 +495,7 @@ class ContainerTest extends TestCase /** * @group legacy - * @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. + * @expectedDeprecation Setting the "bar" service after it's been initialized is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. */ public function testReplacingAPreDefinedServiceIsDeprecated() { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index a1422bf96a..d832a220c7 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -269,7 +269,7 @@ class PhpDumperTest extends TestCase /** * @group legacy - * @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. + * @expectedDeprecation Setting the "bar" service after it's been initialized is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. */ public function testOverrideServiceWhenUsingADumpedContainer() { @@ -277,15 +277,16 @@ class PhpDumperTest extends TestCase require_once self::$fixturesPath.'/includes/foo.php'; $container = new \ProjectServiceContainer(); - $container->set('bar', $bar = new \stdClass()); $container->setParameter('foo_bar', 'foo_bar'); + $container->get('bar'); + $container->set('bar', $bar = new \stdClass()); $this->assertSame($bar, $container->get('bar'), '->set() overrides an already defined service'); } /** * @group legacy - * @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. + * @expectedDeprecation Setting the "bar" service after it's been initialized is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0. */ public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne() { @@ -294,6 +295,8 @@ class PhpDumperTest extends TestCase require_once self::$fixturesPath.'/includes/classes.php'; $container = new \ProjectServiceContainer(); + $container->setParameter('foo_bar', 'foo_bar'); + $container->get('bar'); $container->set('bar', $bar = new \stdClass()); $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service'); From b1290da21bf0aec570e7852ebcdd032ff86a5074 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 4 Oct 2017 11:26:14 +0200 Subject: [PATCH 2/2] [Config] Fix dumped files invalidation by OPCache --- src/Symfony/Component/Config/ConfigCache.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Symfony/Component/Config/ConfigCache.php b/src/Symfony/Component/Config/ConfigCache.php index a34328ee8f..814ae7fb3b 100644 --- a/src/Symfony/Component/Config/ConfigCache.php +++ b/src/Symfony/Component/Config/ConfigCache.php @@ -149,6 +149,10 @@ class ConfigCache implements ConfigCacheInterface // discard chmod failure (some filesystem may not support it) } } + + if (\function_exists('opcache_invalidate') && ini_get('opcache.enable')) { + @opcache_invalidate($this->file, true); + } } /**