8000 feature #28487 [FrameworkBundle] Ignore backslashes in service ids wh… · symfony/symfony@cb13594 · GitHub
[go: up one dir, main page]

Skip to content

Commit cb13594

Browse files
feature #28487 [FrameworkBundle] Ignore backslashes in service ids when using debug:container and debug:autowiring (respinoza)
This PR was merged into the 4.2-dev branch. Discussion ---------- [FrameworkBundle] Ignore backslashes in service ids when using debug:container and debug:autowiring | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28143 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Commits ------- 0aba355 [FrameworkBundle] Ignore backslashes in service ids when using `debug:container` and `debug:autowiring`
2 parents 00e5cd9 + 0aba355 commit cb13594

File tree

7 files changed

+58
-8
lines changed

7 files changed

+58
-8
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* Added new "auto" mode for `framework.session.cookie_secure` to turn it on when HTTPS is used
1414
* Removed the `framework.messenger.encoder` and `framework.messenger.decoder` options. Use the `framework.messenger.serializer.id` option to replace the Messenger serializer.
1515
* Deprecated the `ContainerAwareCommand` class in favor of `Symfony\Component\Console\Command\Command`
16+
* Made `debug:container` and `debug:autowiring` ignore backslashes in service ids
1617

1718
4.1.0
1819
-----

src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,23 +219,27 @@ private function findProperServiceName(InputInterface $input, SymfonyStyle $io,
219219
throw new InvalidArgumentException(sprintf('No services found that match "%s".', $name));
220220
}
221221

222-
$default = 1 === \count($matchingServices) ? $matchingServices[0] : null;
222+
if (1 === \count($matchingServices)) {
223+
return $matchingServices[0];
224+
}
223225

224-
return $io->choice('Select one of the following services to display its information', $matchingServices, $default);
226+
return $io->choice('Select one of the following services to display its information', $matchingServices);
225227
}
226228

227229
private function findServiceIdsContaining(ContainerBuilder $builder, $name)
228230
{
229231
$serviceIds = $builder->getServiceIds();
230-
$foundServiceIds = array();
232+
$foundServiceIds = $foundServiceIdsIgnoringBackslashes = array();
231233
foreach ($serviceIds as $serviceId) {
232-
if (false === stripos($serviceId, $name)) {
233-
continue;
234+
if (false !== stripos(str_replace('\\', '', $serviceId), $name)) {
235+
$foundServiceIdsIgnoringBackslashes[] = $serviceId;
236+
}
237+
if (false !== stripos($serviceId, $name)) {
238+
$foundServiceIds[] = $serviceId;
234239
}
235-
$foundServiceIds[] = $serviceId;
236240
}
237241

238-
return $foundServiceIds;
242+
return $foundServiceIds ?: $foundServiceIdsIgnoringBackslashes;
239243
}
240244

241245
/**

src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6666

6767
if ($search = $input->getArgument('search')) {
6868
$serviceIds = array_filter($serviceIds, function ($serviceId) use ($search) {
69-
return false !== stripos($serviceId, $search);
69+
return false !== stripos(str_replace('\\', '', $serviceId), $search);
7070
});
7171

7272
if (empty($serviceIds)) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures;
4+
5+
class BackslashClass
6+
{
7+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ContainerDebugCommandTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
1313

1414
use Symfony\Bundle\FrameworkBundle\Console\Application;
15+
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BackslashClass;
1516
use Symfony\Component\Console\Tester\ApplicationTester;
1617

1718
/**
@@ -63,4 +64,27 @@ public function testPrivateAlias()
6364
$this->assertContains('public', $tester->getDisplay());
6465
$this->assertContains('private_alias', $tester->getDisplay());
6566
}
67+
68+
/**
69+
* @dataProvider provideIgnoreBackslashWhenFindingService
70+
*/
71+
public function testIgnoreBackslashWhenFindingService(string $validServiceId)
72+
{
73+
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml'));
74+
75+
$application = new Application(static::$kernel);
76+
$application->setAutoExit(false);
77+
78+
$tester = new ApplicationTester($application);
79+
$tester->run(array('command' => 'debug:container', 'name' => $validServiceId));
80+
$this->assertNotContains('No services found', $tester->getDisplay());
81+
}
82+
83+
public function provideIgnoreBackslashWhenFindingService()
84+
{
85+
return array(
86+
array(BackslashClass::class),
87+
array('FixturesBackslashClass'),
88+
);
89+
}
6690
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public function testSearchArgument()
4747
$this->assertNotContains('Symfony\Component\Routing\RouterInterface', $tester->getDisplay());
4848
}
4949

50+
public function testSearchIgnoreBackslashWhenFindingService()
51+
{
52+
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml'));
53+
54+
$application = new Application(static::$kernel);
55+
$application->setAutoExit(false);
56+
57+
$tester = new ApplicationTester($application);
58+
$tester->run(array('command' => 'debug:autowiring', 'search' => 'HttpKernelHttpKernelInterface'));
59+
$this->assertContains('Symfony\Component\HttpKernel\HttpKernelInterface', $tester->getDisplay());
60+
}
61+
5062
public function testSearchNoResults()
5163
{
5264
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml'));

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ContainerDebug/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ services:
88
private_alias:
99
alias: public
1010
public: false
11+
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BackslashClass:
12+
class: Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BackslashClass

0 commit comments

Comments
 (0)
0