8000 [DI] Deprecate (un)setting pre-defined services · symfony/symfony@fdb2140 · GitHub
[go: up one dir, main page]

Skip to content

Commit fdb2140

Browse files
ro0NLnicolas-grekas
authored andcommitted
[DI] Deprecate (un)setting pre-defined services
1 parent ed6a2ed commit fdb2140

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public function testEvaluateWithoutAvailableRequest()
3838
$loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader');
3939
$engine = new PhpEngine(new TemplateNameParser(), $container, $loader, new GlobalVariables($container));
4040

41-
$container->set('request_stack', null);
42-
41+
$this->assertFalse($container->has('request_stack'));
4342
$globals = $engine->getGlobals();
4443
$this->assertEmpty($globals['app']->getRequest());
4544
}

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,13 @@ public function set($id, $service)
190190
@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);
191191
unset($this->privates[$id]);
192192
} else {
193-
@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);
193+
@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);
194+
}
195+
} elseif (isset($this->methodMap[$id])) {
196+
if (null === $service) {
197+
@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);
198+
} else {
199+
@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);
194200
}
195201
}
196202
}

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ public function testSetReplacesAlias()
163163
$this->assertSame($foo, $c->get('alias'), '->set() replaces an existing alias');
164164
}
165165

166+
/**
167+
* @group legacy
168+
* @expectedDeprecation Unsetting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
169+
*/
170+
public function testSetWithNullResetPredefinedService()
171+
{
172+
$sc = new Container();
173+
$sc->set('foo', new \stdClass());
174+
$sc->set('foo', null);
175+
$this->assertFalse($sc->has('foo'), '->set() with null service resets the service');
176+
177+
$sc = new ProjectServiceContainer();
178+
$sc->set('bar', null);
179+
$this->assertTrue($sc->has('bar'), '->set() with null service resets the pre-defined service');
180+
}
181+
166182
public function testGet()
167183
{
168184
$sc = new ProjectServiceContainer();
@@ -172,9 +188,6 @@ public function testGet()
172188
$this->assertSame($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
173189
$this->assertSame($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
174190

175-
$sc->set('bar', $bar = new \stdClass());
176-
$this->assertSame($bar, $sc->get('bar'), '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()');
177-
178191
try {
179192
$sc->get('');
180193
$this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
@@ -337,7 +350,7 @@ public function testInitialized()
337350
$this->assertFalse($sc->initialized('bar'), '->initialized() returns false if a service is defined, but not currently loaded');
338351
$this->assertFalse($sc->initialized('alias'), '->initialized() returns false if an aliased service is not initialized');
339352

340-
$sc->set('bar', new \stdClass());
353+
$sc->get('bar');
341354
$this->assertTrue($sc->initialized('alias'), '->initialized() returns true for alias if aliased service is initialized');
342355
}
343356

@@ -426,7 +439,7 @@ public function testUnsetInternalPrivateServiceIsDeprecated()
426439

427440
/**
428441
* @group legacy
429-
* @expectedDeprecation Setting the "internal" 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.
442+
* @expectedDeprecation Setting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
430443
*/
431444
public function testChangeInternalPrivateServiceIsDeprecated()
432445
{
@@ -453,6 +466,19 @@ public function testRequestAnInternalSharedPrivateServiceIsDeprecated()
453466
$c = new ProjectServiceContainer();
454467
$c->get('internal');
455468
}
469+
470+
/**
471+
* @group legacy
472+
* @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
473+
*/
474+
public function testReplacingAPreDefinedServiceIsDeprecated()
475+
{
476+
$c = new ProjectServiceContainer();
477+
$c->set('bar', new \stdClass());
478+
$c->set('bar', $bar = new \stdClass());
479+
480+
$this->assertSame($bar, $c->get('bar'), '->set() replaces a pre-defined service');
481+
}
456482
}
457483

458484
class ProjectServiceContainer extends Container
@@ -490,7 +516,7 @@ protected function getInternalService()
490516

491517
protected function getBarService()
492518
{
493-
return $this->__bar;
519+
return $this->services['bar'] = $this->__bar;
494520
}
495521

496522
protected function getFooBarService()

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,13 @@ public function provideInvalidFactories()
241241
public function testAliases()
242242
{
243243
$container = include self::$fixturesPath.'/containers/container9.php';
244+
$container->setParameter('foo_bar', 'foo_bar');
244245
$container->compile();
245246
$dumper = new PhpDumper($container);
246247
eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Aliases')));
247248

248249
$container = new \Symfony_DI_PhpDumper_Test_Aliases();
249-
$container->set('foo', $foo = new \stdClass());
250-
$this->assertSame($foo, $container->get('foo'));
250+
$foo = $container->get('foo');
251251
$this->assertSame($foo, $container->get('alias_for_foo'));
252252
$this->assertSame($foo, $container->get('alias_for_alias'));
253253
}
@@ -264,6 +264,10 @@ public function testFrozenContainerWithoutAliases()
264264
$this->assertFalse($container->has('foo'));
265265
}
266266

267+
/**
268+
* @group legacy
269+
* @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
270+
*/
267271
public function testOverrideServiceWhenUsingADumpedContainer()
268272
{
269273
require_once self::$fixturesPath.'/php/services9.php';
@@ -276,6 +280,10 @@ public function testOverrideServiceWhenUsingADumpedContainer()
276280
$this->assertSame($bar, $container->get('bar'), '->set() overrides an already defined service');
277281
}
278282

283+
/**
284+
* @group legacy
285+
* @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
286+
*/
279287
public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
280288
{
281289
require_once self::$fixturesPath.'/php/services9.php';

src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
require_once __DIR__.'/../includes/classes.php';
4+
require_once __DIR__.'/../includes/foo.php';
45

56
use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
67
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;

0 commit comments

Comments
 (0)
0