Skip to content

Commit 1ce56c8

Browse files
authored
[11.x] Fix command alias registration and usage. (#50617)
* Fix command alias parsing * Allow aliases to be run directly * Add missing `AsCommand` attributes * lint * Add missing attribute
1 parent e6d01af commit 1ce56c8

19 files changed

+120
-7
lines changed

src/Illuminate/Cache/Console/CacheTableCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Console\MigrationGeneratorCommand;
66
use Symfony\Component\Console\Attribute\AsCommand;
77

8-
#[AsCommand(name: 'make:cache-table')]
8+
#[AsCommand(name: 'make:cache-table', aliases: ['cache:table'])]
99
class CacheTableCommand extends MigrationGeneratorCommand
1010
{
1111
/**

src/Illuminate/Console/Application.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ protected function addToParent(SymfonyCommand $command)
238238
public function resolve($command)
239239
{
240240
if (is_subclass_of($command, SymfonyCommand::class) && ($commandName = $command::getDefaultName())) {
241-
$this->commandMap[$commandName] = $command;
241+
foreach (explode('|', $commandName) as $name) {
242+
$this->commandMap[$name] = $command;
243+
}
242244

243245
return null;
244246
}

src/Illuminate/Console/Scheduling/ScheduleClearCacheCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Illuminate\Console\Scheduling;
44

55
use Illuminate\Console\Command;
6+
use Symfony\Component\Console\Attribute\AsCommand;
67

8+
#[AsCommand(name: 'schedule:clear-cache')]
79
class ScheduleClearCacheCommand extends Command
810
{
911
/**

src/Illuminate/Database/Console/DbCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\ConfigurationUrlParser;
7+
use Symfony\Component\Console\Attribute\AsCommand;
78
use Symfony\Component\Process\Process;
89
use UnexpectedValueException;
910

11+
#[AsCommand(name: 'db')]
1012
class DbCommand extends Command
1113
{
1214
/**

src/Illuminate/Database/Console/Migrations/FreshCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
use Illuminate\Console\ConfirmableTrait;
77
use Illuminate\Contracts\Events\Dispatcher;
88
use Illuminate\Database\Events\DatabaseRefreshed;
9+
use Symfony\Component\Console\Attribute\AsCommand;
910
use Symfony\Component\Console\Input\InputOption;
1011

12+
#[AsCommand(name: 'migrate:fresh')]
1113
class FreshCommand extends Command
1214
{
1315
use ConfirmableTrait;

src/Illuminate/Database/Console/Migrations/InstallCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Database\Migrations\MigrationRepositoryInterface;
7+
use Symfony\Component\Console\Attribute\AsCommand;
78
use Symfony\Component\Console\Input\InputOption;
89

10+
#[AsCommand(name: 'migrate:install')]
911
class InstallCommand extends Command
1012
{
1113
/**

src/Illuminate/Database/Console/Migrations/MigrateCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use Illuminate\Database\SQLiteDatabaseDoesNotExistException;
1111
use Illuminate\Database\SqlServerConnection;
1212
use PDOException;
13+
use Symfony\Component\Console\Attribute\AsCommand;
1314
use Throwable;
1415

1516
use function Laravel\Prompts\confirm;
1617

18+
#[AsCommand(name: 'migrate')]
1719
class MigrateCommand extends BaseCommand implements Isolatable
1820
{
1921
use ConfirmableTrait;

src/Illuminate/Database/Console/Migrations/MigrateMakeCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Illuminate\Database\Migrations\MigrationCreator;
77
use Illuminate\Support\Composer;
88
use Illuminate\Support\Str;
9+
use Symfony\Component\Console\Attribute\AsCommand;
910

11+
#[AsCommand(name: 'make:migration')]
1012
class MigrateMakeCommand extends BaseCommand implements PromptsForMissingInput
1113
{
1214
/**

src/Illuminate/Database/Console/Migrations/RefreshCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
use Illuminate\Console\ConfirmableTrait;
77
use Illuminate\Contracts\Events\Dispatcher;
88
use Illuminate\Database\Events\DatabaseRefreshed;
9+
use Symfony\Component\Console\Attribute\AsCommand;
910
use Symfony\Component\Console\Input\InputOption;
1011

12+
#[AsCommand(name: 'migrate:refresh')]
1113
class RefreshCommand extends Command
1214
{
1315
use ConfirmableTrait;

src/Illuminate/Database/Console/Migrations/ResetCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Illuminate\Console\ConfirmableTrait;
66
use Illuminate\Database\Migrations\Migrator;
7+
use Symfony\Component\Console\Attribute\AsCommand;
78
use Symfony\Component\Console\Input\InputOption;
89

10+
#[AsCommand(name: 'migrate:reset')]
911
class ResetCommand extends BaseCommand
1012
{
1113
use ConfirmableTrait;

src/Illuminate/Database/Console/Migrations/RollbackCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Illuminate\Console\ConfirmableTrait;
66
use Illuminate\Database\Migrations\Migrator;
7+
use Symfony\Component\Console\Attribute\AsCommand;
78
use Symfony\Component\Console\Input\InputOption;
89

10+
#[AsCommand('migrate:rollback')]
911
class RollbackCommand extends BaseCommand
1012
{
1113
use ConfirmableTrait;

src/Illuminate/Database/Console/Migrations/StatusCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Illuminate\Database\Migrations\Migrator;
66
use Illuminate\Support\Collection;
7+
use Symfony\Component\Console\Attribute\AsCommand;
78
use Symfony\Component\Console\Input\InputOption;
89

10+
#[AsCommand(name: 'migrate:status')]
911
class StatusCommand extends BaseCommand
1012
{
1113
/**

src/Illuminate/Database/Console/PruneCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
use Illuminate\Database\Events\ModelsPruned;
1313
use Illuminate\Support\Str;
1414
use InvalidArgumentException;
15+
use Symfony\Component\Console\Attribute\AsCommand;
1516
use Symfony\Component\Finder\Finder;
1617

18+
#[AsCommand(name: 'model:prune')]
1719
class PruneCommand extends Command
1820
{
1921
/**

src/Illuminate/Notifications/Console/NotificationTableCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Console\MigrationGeneratorCommand;
66
use Symfony\Component\Console\Attribute\AsCommand;
77

8-
#[AsCommand(name: 'make:notifications-table')]
8+
#[AsCommand(name: 'make:notifications-table', aliases: ['notifications:table'])]
99
class NotificationTableCommand extends MigrationGeneratorCommand
1010
{
1111
/**

src/Illuminate/Queue/Console/BatchesTableCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use function Illuminate\Filesystem\join_paths;
99

10-
#[AsCommand(name: 'make:queue-batches-table')]
10+
#[AsCommand(name: 'make:queue-batches-table', aliases: ['queue:batches-table'])]
1111
class BatchesTableCommand extends MigrationGeneratorCommand
1212
{
1313
/**

src/Illuminate/Queue/Console/FailedTableCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use function Illuminate\Filesystem\join_paths;
99

10-
#[AsCommand(name: 'make:queue-failed-table')]
10+
#[AsCommand(name: 'make:queue-failed-table', aliases: ['queue:failed-table'])]
1111
class FailedTableCommand extends MigrationGeneratorCommand
1212
{
1313
/**

src/Illuminate/Queue/Console/TableCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use function Illuminate\Filesystem\join_paths;
99

10-
#[AsCommand(name: 'make:queue-table')]
10+
#[AsCommand(name: 'make:queue-table', aliases: ['queue:table'])]
1111
class TableCommand extends MigrationGeneratorCommand
1212
{
1313
/**

src/Illuminate/Session/Console/SessionTableCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use function Illuminate\Filesystem\join_paths;
99

10-
#[AsCommand(name: 'make:session-table')]
10+
#[AsCommand(name: 'make:session-table', aliases: ['session:table'])]
1111
class SessionTableCommand extends MigrationGeneratorCommand
1212
{
1313
/**

tests/Console/ConsoleApplicationTest.php

+89
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
use Illuminate\Console\Command;
77
use Illuminate\Contracts\Events\Dispatcher;
88
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
9+
use Illuminate\Events\Dispatcher as EventsDispatcher;
10+
use Illuminate\Foundation\Application as FoundationApplication;
911
use Illuminate\Tests\Console\Fixtures\FakeCommandWithInputPrompting;
1012
use Mockery as m;
1113
use PHPUnit\Framework\TestCase;
14+
use Symfony\Component\Console\Attribute\AsCommand;
1215
use Symfony\Component\Console\Command\Command as SymfonyCommand;
16+
use Symfony\Component\Console\Exception\CommandNotFoundException;
17+
use Throwable;
1318

1419
class ConsoleApplicationTest extends TestCase
1520
{
@@ -51,6 +56,68 @@ public function testResolveAddsCommandViaApplicationResolution()
5156
$this->assertEquals($command, $result);
5257
}
5358

59+
public function testResolvingCommandsWithAliasViaAttribute()
60+
{
61+
$container = new FoundationApplication();
62+
$app = new Application($container, new EventsDispatcher($container), $container->version());
63+
$app->resolve(CommandWithAliasViaAttribute::class);
64+
$app->setContainerCommandLoader();
65+
66+
$this->assertInstanceOf(CommandWithAliasViaAttribute::class, $app->get('command-name'));
67+
$this->assertInstanceOf(CommandWithAliasViaAttribute::class, $app->get('command-alias'));
68+
$this->assertArrayHasKey('command-name', $app->all());
69+
$this->assertArrayHasKey('command-alias', $app->all());
70+
}
71+
72+
public function testResolvingCommandsWithAliasViaProperty()
73+
{
74+
$container = new FoundationApplication();
75+
$app = new Application($container, new EventsDispatcher($container), $container->version());
76+
$app->resolve(CommandWithAliasViaProperty::class);
77+
$app->setContainerCommandLoader();
78+
79+
$this->assertInstanceOf(CommandWithAliasViaProperty::class, $app->get('command-name'));
80+
$this->assertInstanceOf(CommandWithAliasViaProperty::class, $app->get('command-alias'));
81+
$this->assertArrayHasKey('command-name', $app->all());
82+
$this->assertArrayHasKey('command-alias', $app->all());
83+
}
84+
85+
public function testResolvingCommandsWithNoAliasViaAttribute()
86+
{
87+
$container = new FoundationApplication();
88+
$app = new Application($container, new EventsDispatcher($container), $container->version());
89+
$app->resolve(CommandWithNoAliasViaAttribute::class);
90+
$app->setContainerCommandLoader();
91+
92+
$this->assertInstanceOf(CommandWithNoAliasViaAttribute::class, $app->get('command-name'));
93+
try {
94+
$app->get('command-alias');
95+
$this->fail();
96+
} catch (Throwable $e) {
97+
$this->assertInstanceOf(CommandNotFoundException::class, $e);
98+
}
99+
$this->assertArrayHasKey('command-name', $app->all());
100+
$this->assertArrayNotHasKey('command-alias', $app->all());
101+
}
102+
103+
public function testResolvingCommandsWithNoAliasViaProperty()
104+
{
105+
$container = new FoundationApplication();
106+
$app = new Application($container, new EventsDispatcher($container), $container->version());
107+
$app->resolve(CommandWithNoAliasViaProperty::class);
108+
$app->setContainerCommandLoader();
109+
110+
$this->assertInstanceOf(CommandWithNoAliasViaProperty::class, $app->get('command-name'));
111+
try {
112+
$app->get('command-alias');
113+
$this->fail();
114+
} catch (Throwable $e) {
115+
$this->assertInstanceOf(CommandNotFoundException::class, $e);
116+
}
117+
$this->assertArrayHasKey('command-name', $app->all());
118+
$this->assertArrayNotHasKey('command-alias', $app->all());
119+
}
120+
54121
public function testCallFullyStringCommandLine()
55122
{
56123
$app = new Application(
@@ -124,3 +191,25 @@ protected function getMockConsole(array $methods)
124191
])->getMock();
125192
}
126193
}
194+
195+
#[AsCommand('command-name')]
196+
class CommandWithNoAliasViaAttribute extends Command
197+
{
198+
//
199+
}
200+
#[AsCommand('command-name', aliases: ['command-alias'])]
201+
class CommandWithAliasViaAttribute extends Command
202+
{
203+
//
204+
}
205+
206+
class CommandWithNoAliasViaProperty extends Command
207+
{
208+
public $name = 'command-name';
209+
}
210+
211+
class CommandWithAliasViaProperty extends Command
212+
{
213+
public $name = 'command-name';
214+
public $aliases = ['command-alias'];
215+
}

0 commit comments

Comments
 (0)