8000 [DI] Deprecate Container::set for nulls, initialized and alias services · symfony/symfony@bcb4b45 · GitHub
[go: up one dir, main page]

Skip to content

Commit bcb4b45

Browse files
ro0NLnicolas-grekas
authored andcommitted
[DI] Deprecate Container::set for nulls, initialized and alias services
1 parent ed6a2ed commit bcb4b45

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
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: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,20 @@ public function testSetWithNullResetTheService()
155155
$this->assertFalse($sc->has('foo'), '->set() with null service resets the service');
156156
}
157157

158-
public function testSetReplacesAlias()
158+
/**
159+
* @group legacy
160+
* @expectedDeprecation Unsetting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
161+
*/
162+
public function testSetWithNullResetPredefinedService()
159163
{
160-
$c = new ProjectServiceContainer();
164+
$sc = new Container();
165+
$sc->set('foo', new \stdClass());
166+
$sc->set('foo', null);
167+
$this->assertFalse($sc->has('foo'), '->set() with null service resets the service');
161168

162-
$c->set('alias', $foo = new \stdClass());
163-
$this->assertSame($foo, $c->get('alias'), '->set() replaces an existing alias');
169+
$sc = new ProjectServiceContainer();
170+
$sc->set('bar', null);
171+
$this->assertTrue($sc->has('bar'), '->set() with null service resets the pre-defined service');
164172
}
165173

166174
public function testGet()
@@ -172,9 +180,6 @@ public function testGet()
172180
$this->assertSame($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
173181
$this->assertSame($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
174182

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-
178183
try {
179184
$sc->get('');
180185
$this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
@@ -337,7 +342,7 @@ public function testInitialized()
337342
$this->assertFalse($sc->initialized('bar'), '->initialized() returns false if a service is defined, but not currently loaded');
338343
$this->assertFalse($sc->initialized('alias'), '->initialized() returns false if an aliased service is not initialized');
339344

340-
$sc->set('bar', new \stdClass());
345+
$sc->get('bar');
341346
$this->assertTrue($sc->initialized('alias'), '->initialized() returns true for alias if aliased service is initialized');
342347
}
343348

@@ -426,7 +431,7 @@ public function testUnsetInternalPrivateServiceIsDeprecated()
426431

427432
/**
428433
* @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.
434+
* @expectedDeprecation Setting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
430435
*/
431436
public function testChangeInternalPrivateServiceIsDeprecated()
432437
{
@@ -453,6 +458,19 @@ public function testRequestAnInternalSharedPrivateServiceIsDeprecated()
453458
$c = new ProjectServiceContainer();
454459
$c->get('internal');
455460
}
461+
462+
/**
463+
* @group legacy
464+
* @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
465+
*/
466+
public function testReplacingAPreDefinedServiceIsDeprecated()
467+
{
468+
$c = new ProjectServiceContainer();
469+
$c->set('bar', new \stdClass());
470+
$c->set('bar', $bar = new \stdClass());
471+
472+
$this->assertSame($bar, $c->get('bar'), '->set() replaces a pre-defined service');
473+
}
456474
}
457475

458476
class ProjectServiceContainer extends Container
@@ -490,7 +508,7 @@ protected function getInternalService()
490508

491509
protected function getBarService()
492510
{
493-
return $this->__bar;
511+
return $this->services['bar'] = $this->__bar;
494512
}
495513

496514
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( 10000 )
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