8000 Merge branch '3.4' into 4.3 · symfony/symfony@8033fc5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8033fc5

Browse files
Merge branch '3.4' into 4.3
* 3.4: Do not include hidden commands in suggested alternatives [DependencyInjection] Fix wrong exception when service is synthetic
2 parents fbfdebc + 944cda7 commit 8033fc5

File tree

6 files changed

+68
-6
lines changed

6 files changed

+68
-6
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ public function getNamespaces()
547547
{
548548
$namespaces = [];
549549
foreach ($this->all() as $command) {
550+
if ($command->isHidden()) {
551+
continue;
552+
}
553+
550554
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));
551555

552556
foreach ($command->getAliases() as $alias) {
@@ -640,6 +644,11 @@ public function find($name)
640644
$message = sprintf('Command "%s" is not defined.', $name);
641645

642646
if ($alternatives = $this->findAlternatives($name, $allCommands)) {
647+
// remove hidden commands
648+
$alternatives = array_filter($alternatives, function ($name) {
649+
return !$this->get($name)->isHidden();
650+
});
651+
643652
if (1 == \count($alternatives)) {
644653
$message .= "\n\nDid you mean this?\n ";
645654
} else {
@@ -648,7 +657,7 @@ public function find($name)
648657
$message .= implode("\n ", $alternatives);
649658
}
650659

651-
throw new CommandNotFoundException($message, $alternatives);
660+
throw new CommandNotFoundException($message, array_values($alternatives));
652661
}
653662

654663
// filter out aliases for commands which are already on the list
@@ -672,13 +681,18 @@ public function find($name)
672681
}
673682
$abbrevs = array_map(function ($cmd) use ($commandList, $usableWidth, $maxLen) {
674683
if (!$commandList[$cmd] instanceof Command) {
675-
return $cmd;
684+
$commandList[$cmd] = $this->commandLoader->get($cmd);
676685
}
686+
687+
if ($commandList[$cmd]->isHidden()) {
688+
return false;
689+
}
690+
677691
$abbrev = str_pad($cmd, $maxLen, ' ').' '.$commandList[$cmd]->getDescription();
678692

679693
return Helper::strlen($abbrev) > $usableWidth ? Helper::substr($abbrev, 0, $usableWidth - 3).'...' : $abbrev;
680694
}, array_values($commands));
681-
$suggestions = $this->getAbbreviationSuggestions($abbrevs);
695+
$suggestions = $this->getAbbreviationSuggestions(array_filter($abbrevs));
682696

683697
throw new CommandNotFoundException(sprintf("Command \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $name, $suggestions), array_values($commands));
684698
}

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public static function setUpBeforeClass(): void
7575
require_once self::$fixturesPath.'/FooWithoutAliasCommand.php';
7676
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering.php';
7777
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering2.php';
78+
require_once self::$fixturesPath.'/FooHiddenCommand.php';
7879
}
7980

8081
protected function normalizeLineBreaks($text)
@@ -664,6 +665,7 @@ public function testFindAlternativesOutput()
664665
$application->add(new \Foo1Command());
665666
$application->add(new \Foo2Command());
666667
$application->add(new \Foo3Command());
668+
$application->add(new \FooHiddenCommand());
667669

668670
$expectedAlternatives = [
669671
'afoobar',
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
7+
class FooHiddenCommand extends Command
8+
{
9+
protected function configure()
10+
{
11+
$this
12+
->setName('foo:hidden')
13+
->setAliases(['afoohidden'])
14+
->setHidden(true)
15+
;
16+
}
17+
18+
protected function execute(InputInterface $input, OutputInterface $output)
19+
{
20+
}
21+
}

src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ protected function processValue($value, $isRoot = false)
113113
*/
114114
protected function getConstructor(Definition $definition, $required)
115115
{
116+
if ($definition->isSynthetic()) {
117+
return null;
118+
}
119+
116120
if (\is_string($factory = $definition->getFactory())) {
117121
if (!\function_exists($factory)) {
118122
throw new RuntimeException(sprintf('Invalid service "%s": function "%s" does not exist.', $this->currentId, $factory));

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,10 @@ public function testSetterInjection()
568568
);
569569
}
570570

571-
/**
572-
* @exceptedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "setLogger()" does not exist.
573-
*/
574571
public function testWithNonExistingSetterAndAutowiring()
575572
{
576573
$this->expectException(RuntimeException::class);
574+
$this->expectExceptionMessage('Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass": method "setLogger()" does not exist.');
577575
$container = new ContainerBuilder();
578576

579577
$definition = $container->register(CaseSensitiveClass::class, CaseSensitiveClass::class)->setAutowired(true);

src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveBindingsPassTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
1616
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
17+
use Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass;
1718
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
1920
use Symfony\Component\DependencyInjection\Definition;
21+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2022
use Symfony\Component\DependencyInjection\Reference;
2123
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
2224
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
@@ -127,4 +129,25 @@ public function testWithNonExistingSetterAndBinding()
127129
$pass = new ResolveBindingsPass();
128130
$pass->process($container);
129131
}
132+
133+
public function testSyntheticServiceWithBind()
134+
{
135+
$container = new ContainerBuilder();
136+
$argument = new BoundArgument('bar');
137+
138+
$container->register('foo', 'stdClass')
139+
->addArgument(new Reference('synthetic.service'));
140+
141+
$container->register('synthetic.service')
142+
->setSynthetic(true)
143+
->setBindings(['$apiKey' => $argument]);
144+
145+
$container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class)
146+
->setBindings(['$apiKey' => $argument]);
147+
148+
(new ResolveBindingsPass())->process($container);
149+
(new DefinitionErrorExceptionPass())->process($container);
150+
151+
$this->assertSame([1 => 'bar'], $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
152+
}
130153
}

0 commit comments

Comments
 (0)
0