Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 69583b2

Browse filesBrowse files
committed
bug #22866 [DI] Check for privates before shared services (ro0NL)
This PR was merged into the 3.2 branch. Discussion ---------- [DI] Check for privates before shared services | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22801 (comment), #22801 (comment) | License | MIT | Doc PR | symfony/symfony-docs#... <!--highly recommended for new features--> cc @stof Commits ------- 4f683a9 [DI] Check for privates before shared services
2 parents cf458fa + 4f683a9 commit 69583b2
Copy full SHA for 69583b2

File tree

5 files changed

+39
-16
lines changed
Filter options

5 files changed

+39
-16
lines changed

‎UPGRADE-3.2.md

Copy file name to clipboardExpand all lines: UPGRADE-3.2.md
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ DependencyInjection
6464
* Calling `get()` on a `ContainerBuilder` instance before compiling the
6565
container is deprecated and will throw an exception in Symfony 4.0.
6666

67+
* Setting or unsetting a private service with the `Container::set()` method is
68+
deprecated. Only public services can be set or unset in Symfony 4.0.
69+
70+
* Checking the existence of a private service with the `Container::has()`
71+
method is deprecated and will return `false` in Symfony 4.0.
72+
73+
* Requesting a private service with the `Container::get()` method is deprecated
74+
and will no longer be supported in Symfony 4.0.
75+
6776
ExpressionLanguage
6877
-------------------
6978

‎src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ class WebProfilerExtensionTest extends TestCase
2727
*/
2828
private $container;
2929

30-
public static function assertSaneContainer(Container $container, $message = '')
30+
public static function assertSaneContainer(Container $container, $message = '', $knownPrivates = array())
3131
{
3232
$errors = array();
3333
foreach ($container->getServiceIds() as $id) {
34+
if (in_array($id, $knownPrivates, true)) { // to be removed in 4.0
35+
continue;
36+
}
3437
try {
3538
$container->get($id);
3639
} catch (\Exception $e) {
@@ -98,7 +101,7 @@ public function testToolbarConfig($toolbarEnabled, $interceptRedirects, $listene
98101

99102
$this->assertSame($listenerInjected, $this->container->has('web_profiler.debug_toolbar'));
100103

101-
$this->assertSaneContainer($this->getDumpedContainer());
104+
$this->assertSaneContainer($this->getDumpedContainer(), '', array('web_profiler.csp.handler'));
102105

103106
if ($listenerInjected) {
104107
$this->assertSame($listenerEnabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled());

‎src/Symfony/Component/DependencyInjection/Container.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Container.php
+8-7Lines 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

‎src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
-3Lines 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
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
+17-4Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.