8000 Merge branch '4.3' into 4.4 · symfony/symfony@399e0fb · GitHub
[go: up one dir, main page]

Skip to content

Commit 399e0fb

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

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
@@ -557,6 +557,10 @@ public function getNamespaces()
557557
{
558558
$namespaces = [];
559559
foreach ($this->all() as $command) {
560+
if ($command->isHidden()) {
561+
continue;
562+
}
563+
560564
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));
561565

562566
foreach ($command->getAliases() as $alias) {
@@ -654,6 +658,11 @@ public function find($name)
654658
$message = sprintf('Command "%s" is not defined.', $name);
655659

656660
if ($alternatives = $this->findAlternatives($name, $allCommands)) {
661+
// remove hidden commands
662+
$alternatives = array_filter($alternatives, function ($name) {
663+
return !$this->get($name)->isHidden();
664+
});
665+
657666
if (1 == \count($alternatives)) {
658667
$message .= "\n\nDid you mean this?\n ";
659668
} else {
@@ -662,7 +671,7 @@ public function find($name)
662671
$message .= implode("\n ", $alternatives);
663672
}
664673

665-
throw new CommandNotFoundException($message, $alternatives);
674+
throw new CommandNotFoundException($message, array_values($alternatives));
666675
}
667676

668677
// filter out aliases for commands which are already on the list
@@ -685,13 +694,18 @@ public function find($name)
685694
}
686695
$abbrevs = array_map(function ($cmd) use ($commandList, $usableWidth, $maxLen) {
687696
if (!$commandList[$cmd] instanceof Command) {
688-
return $cmd;
697+
$commandList[$cmd] = $this->commandLoader->get($cmd);
689698
}
699+
700+
if ($commandList[$cmd]->isHidden()) {
701+
return false;
702+
}
703+
690704
$abbrev = str_pad($cmd, $maxLen, ' ').' '.$commandList[$cmd]->getDescription();
691705

692706
return Helper::strlen($abbrev) > $usableWidth ? Helper::substr($abbrev, 0, $usableWidth - 3).'...' : $abbrev;
693707
}, array_values($commands));
694-
$suggestions = $this->getAbbreviationSuggestions($abbrevs);
708+
$suggestions = $this->getAbbreviationSuggestions(array_filter($abbrevs));
695709

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

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
@@ -15,9 +15,11 @@
1515
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
1616
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1717
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
18+
use Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass;
1819
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
1920
use Symfony\Component\DependencyInjection\ContainerBuilder;
2021
use Symfony\Component\DependencyInjection\Definition;
22+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2123
use Symfony\Component\DependencyInjection\Reference;
2224
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
2325
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
@@ -131,4 +133,25 @@ public function testWithNonExistingSetterAndBinding()
131133
$pass = new ResolveBindingsPass();
132134
$pass->process($container);
133135
}
136+
137+
public function testSyntheticServiceWithBind()
138+
{
139+
$container = new ContainerBuilder();
140+
$argument = new BoundArgument('bar');
141+
142+
$container->register('foo', 'stdClass')
143+
->addArgument(new Reference('synthetic.service'));
144+
145+
$container->register('synthetic.service')
146+
->setSynthetic(true)
147+
->setBindings(['$apiKey' => $argument]);
148+
149+
$container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class)
150+
->setBindings(['$apiKey' => $argument]);
151+
152+
(new ResolveBindingsPass())->process($container);
153+
(new DefinitionErrorExceptionPass())->process($container);
154+
155+
$this->assertSame([1 => 'bar'], $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
156+
}
134157
}

0 commit comments

Comments
 (0)
0