Skip to content

Commit 39cbdd6

Browse files
committed
feature #26921 [DI][FrameworkBundle] Hide service ids that start with a dot (nicolas-grekas)
This PR was merged into the 4.1-dev branch. Discussion ---------- [DI][FrameworkBundle] Hide service ids that start with a dot | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #26630 | License | MIT | Doc PR | - Now that services are private by default, `debug:container` should display them by default. In fact, the public/private concept should not trigger a difference in visibility for this command. Instead, I propose to handle service ids that start with a dot as hidden services. PR should be ready. (For reference, I tried using a tag instead in #26891, but that doesn't work in practice when working on the implementation.) Commits ------- cea051ee5e [DI][FrameworkBundle] Hide service ids that start with a dot
2 parents a9d4fc2 + adf989d commit 39cbdd6

13 files changed

+21
-18
lines changed

Compiler/AutowirePass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private function doProcessValue($value, $isRoot = false)
9090

9191
if (ContainerBuilder::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE === $value->getInvalidBehavior()) {
9292
// since the error message varies by referenced id and $this->currentId, so should the id of the dummy errored definition
93-
$this->container->register($id = sprintf('_errored.%s.%s', $this->currentId, (string) $value), $value->getType())
93+
$this->container->register($id = sprintf('.errored.%s.%s', $this->currentId, (string) $value), $value->getType())
9494
->addError($message);
9595

9696
return new TypedReference($id, $value->getType(), $value->getInvalidBehavior());

Compiler/ResolveInstanceofConditionalsPass.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
7575
foreach ($instanceofDefs as $key => $instanceofDef) {
7676
/** @var ChildDefinition $instanceofDef */
7777
$instanceofDef = clone $instanceofDef;
78-
$instanceofDef->setAbstract(true)->setParent($parent ?: 'abstract.instanceof.'.$id);
79-
$parent = 'instanceof.'.$interface.'.'.$key.'.'.$id;
78+
$instanceofDef->setAbstract(true)->setParent($parent ?: '.abstract.instanceof.'.$id);
79+
$parent = '.instanceof.'.$interface.'.'.$key.'.'.$id;
8080
$container->setDefinition($parent, $instanceofDef);
8181
$instanceofTags[] = $instanceofDef->getTags();
8282

@@ -95,7 +95,7 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
9595

9696
if ($parent) {
9797
$bindings = $definition->getBindings();
98-
$abstract = $container->setDefinition('abstract.instanceof.'.$id, $definition);
98+
$abstract = $container->setDefinition('.abstract.instanceof.'.$id, $definition);
9999

100100
// cast Definition to ChildDefinition
101101
$definition->setBindings(array());

Compiler/ResolveInvalidReferencesPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private function processValue($value, $rootLevel = 0, $level = 0)
105105
$e = new ServiceNotFoundException($id, $this->currentId);
106106

107107
// since the error message varies by $id and $this->currentId, so should the id of the dummy errored definition
108-
$this->container->register($id = sprintf('_errored.%s.%s', $this->currentId, $id), $value->getType())
108+
$this->container->register($id = sprintf('.errored.%s.%s', $this->currentId, $id), $value->getType())
109109
->addError($e->getMessage());
110110

111111
return new TypedReference($id, $value->getType(), $value->getInvalidBehavior());

Compiler/ServiceLocatorTagPass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function processValue($value, $isRoot = false)
5454

5555
$value->setArguments($arguments);
5656

57-
$id = 'service_locator.'.ContainerBuilder::hash($value);
57+
$id = '.service_locator.'.ContainerBuilder::hash($value);
5858

5959
if ($isRoot) {
6060
if ($id !== $this->currentId) {
@@ -91,7 +91,7 @@ public static function register(ContainerBuilder $container, array $refMap, $cal
9191
->setPublic(false)
9292
->addTag('container.service_locator');
9393

94-
if (!$container->has($id = 'service_locator.'.ContainerBuilder::hash($locator))) {
94+
if (!$container->has($id = '.service_locator.'.ContainerBuilder::hash($locator))) {
9595
$container->setDefinition($id, $locator);
9696
}
9797

Container.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ private function make(string $id, int $invalidBehavior)
262262

263263
$alternatives = array();
264264
foreach ($this->getServiceIds() as $knownId) {
265+
if ('' === $knownId || '.' === $knownId[0]) {
266+
continue;
267+
}
265268
$lev = levenshtein($id, $knownId);
266269
if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) {
267270
$alternatives[] = $knownId;

Loader/Configurator/ServicesConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ final public function set(?string $id, string $class = null): ServiceConfigurato
7979
throw new \LogicException('Anonymous services must have a class name.');
8080
}
8181

82-
$id = sprintf('%d_%s', ++$this->anonymousCount, preg_replace('/^.*\\\\/', '', $class).'~'.$this->anonymousHash);
82+
$id = sprintf('.%d_%s', ++$this->anonymousCount, preg_replace('/^.*\\\\/', '', $class).'~'.$this->anonymousHash);
8383
$definition->setPublic(false);
8484
} else {
8585
$definition->setPublic($defaults->isPublic());

Loader/XmlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ private function processAnonymousServices(\DOMDocument $xml, $file, $defaults)
409409
foreach ($nodes as $node) {
410410
if ($services = $this->getChildren($node, 'service')) {
411411
// give it a unique name
412-
$id = sprintf('%d_%s', ++$count, preg_replace('/^.*\\\\/', '', $services[0]->getAttribute('class')).'~'.$suffix);
412+
$id = sprintf('.%d_%s', ++$count, preg_replace('/^.*\\\\/', '', $services[0]->getAttribute('class')).'~'.$suffix);
413413
$node->setAttribute('id', $id);
414414
$node->setAttribute('service', $id);
415415

Loader/YamlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ private function resolveServices($value, $file, $isParameter = false)
708708
$instanceof = $this->instanceof;
709709
$this->instanceof = array();
710710

711-
$id = sprintf('%d_%s', ++$this->anonymousServicesCount, preg_replace('/^.*\\\\/', '', isset($argument['class']) ? $argument['class'] : '').$this->anonymousServicesSuffix);
711+
$id = sprintf('.%d_%s', ++$this->anonymousServicesCount, preg_replace('/^.*\\\\/', '', isset($argument['class']) ? $argument['class'] : '').$this->anonymousServicesSuffix);
712712
$this->parseDefinition($id, $argument, $file, array());
713713

714714
if (!$this->container->hasDefinition($id)) {

Tests/Compiler/AutowirePassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,6 @@ public function testErroredServiceLocator()
882882

883883
$erroredDefinition = new Definition(MissingClass::class);
884884

885-
$this->assertEquals($erroredDefinition->addError('Cannot autowire service "some_locator": it has type "Symfony\Component\DependencyInjection\Tests\Compiler\MissingClass" but this class was not found.'), $container->getDefinition('_errored.some_locator.'.MissingClass::class));
885+
$this->assertEquals($erroredDefinition->addError('Cannot autowire service "some_locator": it has type "Symfony\Component\DependencyInjection\Tests\Compiler\MissingClass" but this class was not found.'), $container->getDefinition('.errored.some_locator.'.MissingClass::class));
886886
}
887887
}

Tests/Compiler/ResolveInstanceofConditionalsPassTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testProcess()
2929

3030
(new ResolveInstanceofConditionalsPass())->process($container);
3131

32-
$parent = 'instanceof.'.parent::class.'.0.foo';
32+
$parent = '.instanceof.'.parent::class.'.0.foo';
3333
$def = $container->getDefinition('foo');
3434
$this->assertEmpty($def->getInstanceofConditionals());
3535
$this->assertInstanceOf(ChildDefinition::class, $def);
@@ -260,7 +260,7 @@ public function testMergeReset()
260260

261261
(new ResolveInstanceofConditionalsPass())->process($container);
262262

263-
$abstract = $container->getDefinition('abstract.instanceof.bar');
263+
$abstract = $container->getDefinition('.abstract.instanceof.bar');
264264

265265
$this->assertEmpty($abstract->getArguments());
266266
$this->assertEmpty($abstract->getMethodCalls());

Tests/Fixtures/config/anonymous.expected.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ services:
88
class: Bar\FooClass
99
public: true
1010
arguments: [!tagged listener]
11-
2_stdClass~%s:
11+
.2_stdClass~%s:
1212
class: stdClass
1313
public: false
1414
tags:

Tests/Fixtures/php/services_subscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public function isCompiled()
5454
public function getRemovedIds()
5555
{
5656
return array(
57+
'.service_locator.ljJrY4L' => true,
58+
'.service_locator.ljJrY4L.foo_service' => true,
5759
'Psr\\Container\\ContainerInterface' => true,
5860
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
5961
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => true,
60-
'service_locator.ljJrY4L' => true,
61-
'service_locator.ljJrY4L.foo_service' => true,
6262
);
6363
}
6464

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ public function testAnonymousServices()
555555
$this->assertCount(1, $args);
556556
$this->assertInstanceOf(Reference::class, $args[0]);
557557
$this->assertTrue($container->has((string) $args[0]));
558-
$this->assertRegExp('/^\d+_Bar[._A-Za-z0-9]{7}$/', (string) $args[0]);
558+
$this->assertRegExp('/^\.\d+_Bar[._A-Za-z0-9]{7}$/', (string) $args[0]);
559559

560560
$anonymous = $container->getDefinition((string) $args[0]);
561561
$this->assertEquals('Bar', $anonymous->getClass());
@@ -567,7 +567,7 @@ public function testAnonymousServices()
567567
$this->assertInternalType('array', $factory);
568568
$this->assertInstanceOf(Reference::class, $factory[0]);
569569
$this->assertTrue($container->has((string) $factory[0]));
570-
$this->assertRegExp('/^\d+_Quz[._A-Za-z0-9]{7}$/', (string) $factory[0]);
570+
$this->assertRegExp('/^\.\d+_Quz[._A-Za-z0-9]{7}$/', (string) $factory[0]);
571571
$this->assertEquals('constructFoo', $factory[1]);
572572

573573
$anonymous = $container->getDefinition((string) $factory[0]);

0 commit comments

Comments
 (0)