From e2b8edf4cc07b63409bcc4d0dac5a81b26db7591 Mon Sep 17 00:00:00 2001 From: mugennsou Date: Sat, 16 Jul 2022 23:42:30 +0900 Subject: [PATCH] fix: Remove the commands registered by #[AsCommand] update remove commands test case --- src/Kernel.php | 24 ++++++++++++++----- .../app/Commands/FakeDefaultCommand.php | 2 ++ .../Commands/FakeEnvironmentValueCommand.php | 2 ++ .../app/Commands/FakeFooCommand.php | 2 ++ .../app/Commands/FakeRemovedCommand.php | 2 ++ 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Kernel.php b/src/Kernel.php index c9dd04a8..2dcf8e6d 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -181,7 +181,9 @@ protected function commands(): void Artisan::starting( function ($artisan) use ($toRemoveCommands) { $reflectionClass = new ReflectionClass(Artisan::class); - $commands = collect($artisan->all()) + + $commandsProperty = $reflectionClass->getParentClass()->getProperty('commands'); + $commands = collect($commandsProperty->getValue($artisan)) ->filter( function ($command) use ($toRemoveCommands) { return ! in_array(get_class($command), $toRemoveCommands, true); @@ -189,12 +191,22 @@ function ($command) use ($toRemoveCommands) { ) ->toArray(); - $property = $reflectionClass->getParentClass() - ->getProperty('commands'); + $commandsProperty->setAccessible(true); + $commandsProperty->setValue($artisan, $commands); + $commandsProperty->setAccessible(false); + + $commandMapProperty = $reflectionClass->getProperty('commandMap'); + $commandMap = collect($commandMapProperty->getValue($artisan)) + ->filter( + function ($command) use ($toRemoveCommands) { + return ! in_array($command, $toRemoveCommands, true); + } + ) + ->toArray(); - $property->setAccessible(true); - $property->setValue($artisan, $commands); - $property->setAccessible(false); + $commandMapProperty->setAccessible(true); + $commandMapProperty->setValue($artisan, $commandMap); + $commandMapProperty->setAccessible(false); } ); diff --git a/tests/Application/app/Commands/FakeDefaultCommand.php b/tests/Application/app/Commands/FakeDefaultCommand.php index 1a6f10b6..28fd9218 100644 --- a/tests/Application/app/Commands/FakeDefaultCommand.php +++ b/tests/Application/app/Commands/FakeDefaultCommand.php @@ -3,7 +3,9 @@ namespace App\Commands; use LaravelZero\Framework\Commands\Command; +use Symfony\Component\Console\Attribute\AsCommand; +#[AsCommand(name: 'fake:default')] class FakeDefaultCommand extends Command { protected $name = 'fake:default'; diff --git a/tests/Application/app/Commands/FakeEnvironmentValueCommand.php b/tests/Application/app/Commands/FakeEnvironmentValueCommand.php index 3294aad8..e54a6d73 100644 --- a/tests/Application/app/Commands/FakeEnvironmentValueCommand.php +++ b/tests/Application/app/Commands/FakeEnvironmentValueCommand.php @@ -3,7 +3,9 @@ namespace App\Commands; use LaravelZero\Framework\Commands\Command; +use Symfony\Component\Console\Attribute\AsCommand; +#[AsCommand(name: 'fake:environmentValue')] class FakeEnvironmentValueCommand extends Command { protected $name = 'fake:environmentValue'; diff --git a/tests/Application/app/Commands/FakeFooCommand.php b/tests/Application/app/Commands/FakeFooCommand.php index 4969ee4c..086ffa90 100644 --- a/tests/Application/app/Commands/FakeFooCommand.php +++ b/tests/Application/app/Commands/FakeFooCommand.php @@ -3,7 +3,9 @@ namespace App\Commands; use LaravelZero\Framework\Commands\Command; +use Symfony\Component\Console\Attribute\AsCommand; +#[AsCommand(name: 'fake:foo')] class FakeFooCommand extends Command { protected $signature = 'fake:foo {foo? : The bar}'; diff --git a/tests/Application/app/Commands/FakeRemovedCommand.php b/tests/Application/app/Commands/FakeRemovedCommand.php index 22f7e75a..aef4b2ac 100644 --- a/tests/Application/app/Commands/FakeRemovedCommand.php +++ b/tests/Application/app/Commands/FakeRemovedCommand.php @@ -3,7 +3,9 @@ namespace App\Commands; use LaravelZero\Framework\Commands\Command; +use Symfony\Component\Console\Attribute\AsCommand; +#[AsCommand(name: 'fake:removed')] class FakeRemovedCommand extends Command { protected $name = 'fake:removed';