Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f96952

Browse files
committed
[DI] Removed deprecated setting private/pre-defined services
1 parent 344c8bb commit 9f96952

File tree

4 files changed

+39
-84
lines changed

4 files changed

+39
-84
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ CHANGELOG
1919
* removed `ContainerBuilder::addClassResource()`, use the `addObjectResource()` or the `getReflectionClass()` method instead.
2020
* removed support for top-level anonymous services
2121
* removed silent behavior for unused attributes and elements
22+
* removed support for setting and accessing private services in `Container`
23+
* removed support for setting pre-defined services in `Container`
2224

2325
3.4.0
2426
-----

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,25 @@ public function set($id, $service)
151151
throw new InvalidArgumentException('You cannot set service "service_container".');
152152
}
153153

154+
if (isset($this->privates[$id])) {
155+
throw new InvalidArgumentException(sprintf('You cannot set the private service "%s".', $id));
156+
}
157+
158+
if (isset($this->methodMap[$id])) {
159+
throw new InvalidArgumentException(sprintf('You cannot set the pre-defined service "%s".', $id));
160+
}
161+
154162
if (isset($this->aliases[$id])) {
155163
unset($this->aliases[$id]);
156164
}
157165

158-
$this->services[$id] = $service;
159-
160166
if (null === $service) {
161167
unset($this->services[$id]);
162-
}
163168

164-
if (isset($this->privates[$id])) {
165-
if (null === $service) {
166-
@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);
167-
unset($this->privates[$id]);
168-
} else {
169-
@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);
170-
}
171-
} elseif (isset($this->methodMap[$id])) {
172-
if (null === $service) {
173-
@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);
174-
} else {
175-
@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);
176-
}
169+
return;
177170
}
171+
172+
$this->services[$id] = $service;
178173
}
179174

180175
/**
@@ -187,7 +182,7 @@ public function set($id, $service)
187182
public function has($id)
188183
{
189184
if (isset($this->privates[$id])) {
190-
@trigger_error(sprintf('Checking for the existence of the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
185+
return false;
191186
}
192187
if ('service_container' === $id) {
193188
return true;
@@ -226,7 +221,11 @@ public function has($id)
226221
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
227222
{
228223
if (isset($this->privates[$id])) {
229-
@trigger_error(sprintf('Requesting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
224+
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
225+
throw new ServiceNotFoundException($id);
226+
}
227+
228+
return;
230229
}
231230
if ('service_container' === $id) {
232231
return $this;
@@ -295,7 +294,7 @@ public function initialized($id)
295294
}
296295

297296
if (isset($this->privates[$id])) {
298-
@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);
297+
return false;
299298
}
300299

301300
if (isset($this->aliases[$id])) {

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

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public function testSet()
147147
public function testSetWithNullResetTheService()
148148
{
149149
$sc = new Container();
150+
$sc->set('foo', new \stdClass());
150151
$sc->set('foo', null);
151152
$this->assertFalse($sc->has('foo'), '->set() with null service resets the service');
152153
}
@@ -159,22 +160,6 @@ public function testSetReplacesAlias()
159160
$this->assertSame($foo, $c->get('alias'), '->set() replaces an existing alias');
160161
}
161162

162-
/**
163-
* @group legacy
164-
* @expectedDeprecation Unsetting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
165-
*/
166-
public function testSetWithNullResetPredefinedService()
167-
{
168-
$sc = new Container();
169-
$sc->set('foo', new \stdClass());
170-
$sc->set('foo', null);
171-
$this->assertFalse($sc->has('foo'), '->set() with null service resets the service');
172-
173-
$sc = new ProjectServiceContainer();
174-
$sc->set('bar', null);
175-
$this->assertTrue($sc->has('bar'), '->set() with null service resets the pre-defined service');
176-
}
177-
178163
public function testGet()
179164
{
180165
$sc = new ProjectServiceContainer();
@@ -275,15 +260,11 @@ public function testInitialized()
275260
$this->assertTrue($sc->initialized('alias'), '->initialized() returns true for alias if aliased service is initialized');
276261
}
277262

278-
/**
279-
* @group legacy
280-
* @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.
281-
*/
282263
public function testInitializedWithPrivateService()
283264
{
284265
$sc = new ProjectServiceContainer();
285266
$sc->get('internal_dependency');
286-
$this->assertTrue($sc->initialized('internal'));
267+
$this->assertFalse($sc->initialized('internal'));
287268
}
288269

289270
public function testReset()
@@ -360,59 +341,51 @@ public function testThatCloningIsNotSupported()
360341
}
361342

362343
/**
363-
* @group legacy
364-
* @expectedDeprecation Unsetting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
344+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
345+
* @expectedExceptionMessage You cannot set the private service "internal".
365346
*/
366-
public function testUnsetInternalPrivateServiceIsDeprecated()
347+
public function testUnsetInternalPrivateService()
367348
{
368349
$c = new ProjectServiceContainer();
369350
$c->set('internal', null);
370351
}
371352

372353
/**
373-
* @group legacy
374-
* @expectedDeprecation Setting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
354+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
355+
* @expectedExceptionMessage You cannot set the private service "internal".
375356
*/
376-
public function testChangeInternalPrivateServiceIsDeprecated()
357+
public function testChangeInternalPrivateService()
377358
{
378359
$c = new ProjectServiceContainer();
379-
$c->set('internal', $internal = new \stdClass());
380-
$this->assertSame($c->get('internal'), $internal);
360+
$c->set('internal', new \stdClass());
381361
}
382362

383-
/**
384-
* @group legacy
385-
* @expectedDeprecation Checking for the existence of the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
386-
*/
387-
public function testCheckExistenceOfAnInternalPrivateServiceIsDeprecated()
363+
public function testCheckExistenceOfAnInternalPrivateService()
388364
{
389365
$c = new ProjectServiceContainer();
390366
$c->get('internal_dependency');
391-
$this->assertTrue($c->has('internal'));
367+
$this->assertFalse($c->has('internal'));
392368
}
393369

394370
/**
395-
* @group legacy
396-
* @expectedDeprecation Requesting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
371+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
372+
* @expectedExceptionMessage You have requested a non-existent service "internal".
397373
*/
398-
public function testRequestAnInternalSharedPrivateServiceIsDeprecated()
374+
public function testRequestAnInternalSharedPrivateService()
399375
{
400376
$c = new ProjectServiceContainer();
401377
$c->get('internal_dependency');
402378
$c->get('internal');
403379
}
404380

405381
/**
406-
* @group legacy
407-
* @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
382+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
383+
* @expectedExceptionMessage You cannot set the pre-defined service "bar".
408384
*/
409-
public function testReplacingAPreDefinedServiceIsDeprecated()
385+
public function testReplacingAPreDefinedService()
410386
{
411387
$c = new ProjectServiceContainer();
412388
$c->set('bar', new \stdClass());
413-
$c->set('bar', $bar = new \stdClass());
414-
415-
$this->assertSame($bar, $c->get('bar'), '->set() replaces a pre-defined service');
416389
}
417390
}
418391

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ public function testFrozenContainerWithoutAliases()
267267
}
268268

269269
/**
270-
* @group legacy
271-
* @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
270+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
271+
* @expectedExceptionMessage You cannot set the pre-defined service "bar".
272272
*/
273273
public function testOverrideServiceWhenUsingADumpedContainer()
274274
{
@@ -277,25 +277,6 @@ public function testOverrideServiceWhenUsingADumpedContainer()
277277

278278
$container = new \ProjectServiceContainer();
279279
$container->set('bar', $bar = new \stdClass());
280-
$container->setParameter('foo_bar', 'foo_bar');
281-
282-
$this->assertSame($bar, $container->get('bar'), '->set() overrides an already defined service');
283-
}
284-
285-
/**
286-
* @group legacy
287-
* @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
288-
*/
289-
public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
290-
{
291-
require_once self::$fixturesPath.'/php/services9.php';
292-
require_once self::$fixturesPath.'/includes/foo.php';
293-
require_once self::$fixturesPath.'/includes/classes.php';
294-
295-
$container = new \ProjectServiceContainer();
296-
$container->set('bar', $bar = new \stdClass());
297-
298-
$this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
299280
}
300281

301282
/**

0 commit comments

Comments
 (0)