Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Allow setting aliases & hidden via command name passed to the constructor #60739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Allow setting aliases and the hidden flag via the command name passed to the constructor

7.3
---

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ public function __construct(?string $name = null)
if (self::class !== (new \ReflectionMethod($this, 'getDefaultName'))->class) {
trigger_deprecation('symfony/console', '7.3', 'Overriding "Command::getDefaultName()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', static::class);

$defaultName = static::getDefaultName();
$name = static::getDefaultName();
} else {
$defaultName = $attribute?->name;
$name = $attribute?->name;
}
}

if (null === $name && null !== $name = $defaultName) {
if (null !== $name) {
$aliases = explode('|', $name);

if ('' === $name = array_shift($aliases)) {
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ public function testGetSetAliases()
$this->assertEquals(['name1'], $command->getAliases(), '->setAliases() sets the aliases');
}

/**
* @testWith ["name|alias1|alias2", "name", ["alias1", "alias2"], false]
* ["|alias1|alias2", "alias1", ["alias2"], true]
*/
public function testSetAliasesAndHiddenViaName(string $name, string $expectedName, array $expectedAliases, bool $expectedHidden)
{
$command = new Command($name);

self::assertSame($expectedName, $command->getName());
self::assertSame($expectedHidden, $command->isHidden());
self::assertSame($expectedAliases, $command->getAliases());
}

public function testGetSynopsis()
{
$command = new \TestCommand();
Expand Down
Loading