Skip to content

Commit fb2a0f2

Browse files
committed
[DI] Check for privates before shared services
1 parent c977d54 commit fb2a0f2

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

Container.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,17 @@ public function set($id, $service)
200200
public function has($id)
201201
{
202202
for ($i = 2;;) {
203+
if (isset($this->privates[$id])) {
204+
@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);
205+
}
206+
203207
if ('service_container' === $id
204208
|| isset($this->aliases[$id])
205209
|| isset($this->services[$id])
206210
) {
207211
return true;
208212
}
209213

210-
if (isset($this->privates[$id])) {
211-
@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);
212-
}
213-
214214
if (isset($this->methodMap[$id])) {
215215
return true;
216216
}
@@ -262,6 +262,10 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
262262
if (isset($this->aliases[$id])) {
263263
$id = $this->aliases[$id];
264264
}
265+
if (isset($this->privates[$id])) {
266+
@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);
267+
}
268+
265269
// Re-use shared service instance if it exists.
266270
if (isset($this->services[$id])) {
267271
return $this->services[$id];
@@ -300,9 +304,6 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
300304

301305
return;
302306
}
303-
if (isset($this->privates[$id])) {
304-
@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);
305-
}
306307

307308
$this->loading[$id] = true;
308309

ContainerBuilder.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,6 @@ public function compile()
573573
$compiler->compile($this);
574574

575575
foreach ($this->definitions as $id => $definition) {
576-
if (!$definition->isPublic()) {
577-
$this->privates[$id] = true;
578-
}
579576
if ($this->trackResources && $definition->isLazy() && ($class = $definition->getClass()) && class_exists($class)) {
580577
$this->addClassResource(new \ReflectionClass($class));
581578
}

Tests/ContainerTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function testGetServiceIds()
126126

127127
$sc = new ProjectServiceContainer();
128128
$sc->set('foo', $obj = new \stdClass());
129-
$this->assertEquals(array('service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()');
129+
$this->assertEquals(array('service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'internal_dependency', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()');
130130
}
131131

132132
/**
@@ -397,7 +397,8 @@ public function testUnsetInternalPrivateServiceIsDeprecated()
397397
public function testChangeInternalPrivateServiceIsDeprecated()
398398
{
399399
$c = new ProjectServiceContainer();
400-
$c->set('internal', new \stdClass());
400+
$c->set('internal', $internal = new \stdClass());
401+
$this->assertSame($c->get('internal'), $internal);
401402
}
402403

403404
/**
@@ -407,7 +408,8 @@ public function testChangeInternalPrivateServiceIsDeprecated()
407408
public function testCheckExistenceOfAnInternalPrivateServiceIsDeprecated()
408409
{
409410
$c = new ProjectServiceContainer();
410-
$c->has('internal');
411+
$c->get('internal_dependency');
412+
$this->assertTrue($c->has('internal'));
411413
}
412414

413415
/**
@@ -417,6 +419,7 @@ public function testCheckExistenceOfAnInternalPrivateServiceIsDeprecated()
417419
public function testRequestAnInternalSharedPrivateServiceIsDeprecated()
418420
{
419421
$c = new ProjectServiceContainer();
422+
$c->get('internal_dependency');
420423
$c->get('internal');
421424
}
422425
}
@@ -435,6 +438,7 @@ class ProjectServiceContainer extends Container
435438
'circular' => 'getCircularService',
436439
'throw_exception' => 'getThrowExceptionService',
437440
'throws_exception_on_service_configuration' => 'getThrowsExceptionOnServiceConfigurationService',
441+
'internal_dependency' => 'getInternalDependencyService',
438442
);
439443

440444
public function __construct()
@@ -451,7 +455,7 @@ public function __construct()
451455

452456
protected function getInternalService()
453457
{
454-
return $this->__internal;
458+
return $this->services['internal'] = $this->__internal;
455459
}
456460

457461
protected function getBarService()
@@ -485,6 +489,15 @@ protected function getThrowsExceptionOnServiceConfigurationService()
485489

486490
throw new \Exception('Something was terribly wrong while trying to configure the service!');
487491
}
492+
493+
protected function getInternalDependencyService()
494+
{
495+
$this->services['internal_dependency'] = $instance = new \stdClass();
496+
497+
$instance->internal = isset($this->services['internal']) ? $this->services['internal'] : $this->getInternalService();
498+
499+
return $instance;
500+
}
488501
}
489502

490503
class LegacyProjectServiceContainer extends Container

0 commit comments

Comments
 (0)