diff --git a/src/Package/Concerns/HasCommand.php b/src/Package/Concerns/HasCommand.php new file mode 100644 index 0000000..b0c706f --- /dev/null +++ b/src/Package/Concerns/HasCommand.php @@ -0,0 +1,83 @@ +shouldLoadDefaultCommandFolder() + || $this->getRegister('command')->isNotEmpty(); + } + + public function withCommand(string|array $command = []): static + { + $this->ensureRegistersInitialize('command'); + + if (filled($command)) { + $this->register( + 'command', + $command, + ); + } + + $this->shouldIncludeCommandFromFolder = true; + + return $this; + } + + public function excludeCommand(string|array $path): static + { + $this->exclude('command', $path); + + return $this; + } + + public function getRegisteredCommand(): Collection + { + $files = $this->getDefaultCommandFiles(); + + return $files + ->merge($this->getRegister('command')) + ->filter(fn (string $path) => ! in_array($path, $this->getExclude('command')->all())); + } + + public function getDefaultCommandFiles(): Collection + { + if (! $this->shouldLoadDefaultCommandFolder()) { + return collect(); + } + + return $this->getFilesFrom($this->getBasePath('console/commands')) + ->map(fn (string $path): string => Str::of($path) + ->basename('.php') + ->prepend( + '\\', + $this->getNamespace(), + '\\Console\\Commands\\', + ) + , + ); + + } + + public function preventDefaultCommand(): static + { + $this->preventLoadDefaultCommandFolder = true; + + return $this; + } + + private function shouldLoadDefaultCommandFolder(): bool + { + return ! $this->preventLoadDefaultCommandFolder + && $this->shouldIncludeCommandFromFolder; + } +} diff --git a/src/Package/Concerns/HasCommands.php b/src/Package/Concerns/HasCommands.php deleted file mode 100644 index b01a4e6..0000000 --- a/src/Package/Concerns/HasCommands.php +++ /dev/null @@ -1,86 +0,0 @@ -shouldLoadDefaultCommandsFolder() || filled($this->commands); - } - - private function shouldLoadDefaultCommandsFolder(): bool - { - return ! $this->preventLoadDefaultCommandsFolder && $this->shouldLoadCommandsFromDefaultFolder; - } - - public function preventDefaultCommands(): static - { - $this->preventLoadDefaultCommandsFolder = true; - - return $this; - } - - public function withCommands(string|array ...$command): static - { - if (filled($command)) { - $this->commands = collect($command) - ->flatten() - ->merge($this->commands) - ->all(); - } - - $this->shouldLoadCommandsFromDefaultFolder = true; - - return $this; - } - - public function getRegisteredCommands(): Collection - { - $commands = []; - - if ($this->shouldLoadDefaultCommandsFolder()) { - $commands = $this->loadCommandsFromDefaultFolder(); - } - - return collect($this->commands) - ->merge($commands) - ->filter(fn (string $command) => ! in_array($command, $this->excludedCommands)); - } - - public function loadCommandsFromDefaultFolder() - { - return collect(File::files($this->getBasePath('Console/Commands'))) - ->filter(fn (SplFileInfo $file) => $file->getExtension() === 'php') - ->map(fn (SplFileInfo $file) => str($file) - ->after('Console/') - ->replace('/', '\\') - ->remove('.php') - ->prepend($this->getNamespace().'\\Console\\') - ->toString() - ) - ->all(); - } - - /** - * Un-register a previously registered command - */ - public function unregisterCommand(string $fqcn): static - { - $this->excludedCommands[] = $fqcn; - - return $this; - } -} diff --git a/src/Package/Concerns/HasMigrations.php b/src/Package/Concerns/HasMigration.php similarity index 99% rename from src/Package/Concerns/HasMigrations.php rename to src/Package/Concerns/HasMigration.php index e5b4eee..b04df4c 100644 --- a/src/Package/Concerns/HasMigrations.php +++ b/src/Package/Concerns/HasMigration.php @@ -5,7 +5,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Collection; -trait HasMigrations +trait HasMigration { protected string $migrationPath; diff --git a/src/Package/Contracts/WithCommand.php b/src/Package/Contracts/WithCommand.php new file mode 100644 index 0000000..316a8de --- /dev/null +++ b/src/Package/Contracts/WithCommand.php @@ -0,0 +1,54 @@ +hasCommands()) { - $this->commands($package->getRegisteredCommands()->all()); + if ($package->hasCommand()) { + $this->commands($package->getRegisteredCommand()->all()); } } } diff --git a/tests/Feature/Command/PackageregisterCommandTest.php b/tests/Feature/Command/PackageregisterCommandTest.php new file mode 100644 index 0000000..712167c --- /dev/null +++ b/tests/Feature/Command/PackageregisterCommandTest.php @@ -0,0 +1,51 @@ +setName('package') + ->withCommand([ + \Workbench\App\Console\Commands\Extra\PackageCommandThree::class, + \Workbench\App\Console\Commands\Extra\PackageCommandFour::class, + ]); + } +} + +uses(PackageRegisterCommandTest::class); + +test('package has registered files from folder', function () { + expect($this->package) + ->getRegisteredCommand() + ->toHaveCount(4); + + artisan('list') + ->expectsOutputToContain('package:test-one') + ->expectsOutputToContain('package:test-two') + ->expectsOutputToContain('package:test-three') + ->expectsOutputToContain('package:test-four') + ->assertSuccessful(); +}); + +it('run registered commands', function () { + artisan('package:test-one') + ->expectsOutput('Package test one') + ->assertSuccessful(); + + artisan('package:test-two') + ->expectsOutput('Package test two') + ->assertSuccessful(); + + artisan('package:test-three') + ->expectsOutput('Package test three') + ->assertSuccessful(); + + artisan('package:test-four') + ->expectsOutput('Package test four') + ->assertSuccessful(); +}); diff --git a/tests/Feature/Command/PackageuUnregisterCommandTest.php b/tests/Feature/Command/PackageuUnregisterCommandTest.php new file mode 100644 index 0000000..2e61e63 --- /dev/null +++ b/tests/Feature/Command/PackageuUnregisterCommandTest.php @@ -0,0 +1,58 @@ +setName('unregister-package') + ->withCommand(\Workbench\App\Console\Commands\PackageCommandOne::class) + ->withCommand([ + \Workbench\App\Console\Commands\Extra\PackageCommandThree::class, + \Workbench\App\Console\Commands\Extra\PackageCommandFour::class, + ]) + ->excludeCommand(\Workbench\App\Console\Commands\Extra\PackageCommandThree::class) + ->preventDefaultCommand(); + } +} + +uses(PackageUnregisterCommandTest::class); + +test('package has registered files from folder', function () { + expect($this->package) + ->getRegisteredCommand() + ->toHaveCount(2); + + artisan('list') + ->expectsOutputToContain('package:test-one') + ->doesntExpectOutputToContain('package:test-two') + ->doesntExpectOutputToContain('package:test-three') + ->expectsOutputToContain('package:test-four') + ->assertSuccessful(); +}); + +it('run registered commands', function () { + artisan('package:test-one') + ->expectsOutput('Package test one') + ->assertSuccessful(); + + expect(function () { + artisan('package:test-two') + ->assertFailed(); + })->toThrow(CommandNotFoundException::class) + ->and(function () { + artisan('package:test-three') + ->assertFailed(); + })->toThrow(CommandNotFoundException::class); + + artisan('package:test-four') + ->expectsOutput('Package test four') + ->assertSuccessful(); +}); diff --git a/workbench/app/Console/Commands/PackageCommandOne.php b/workbench/app/Console/Commands/PackageCommandOne.php new file mode 100644 index 0000000..e901e48 --- /dev/null +++ b/workbench/app/Console/Commands/PackageCommandOne.php @@ -0,0 +1,32 @@ +info('Package test one'); + + return self::SUCCESS; + } +} diff --git a/workbench/app/Console/Commands/PackageCommandTwo.php b/workbench/app/Console/Commands/PackageCommandTwo.php new file mode 100644 index 0000000..4872aca --- /dev/null +++ b/workbench/app/Console/Commands/PackageCommandTwo.php @@ -0,0 +1,32 @@ +info('Package test two'); + + return self::SUCCESS; + } +} diff --git a/workbench/app/Console/Commands/extra/PackageCommandFour.php b/workbench/app/Console/Commands/extra/PackageCommandFour.php new file mode 100644 index 0000000..95c29d7 --- /dev/null +++ b/workbench/app/Console/Commands/extra/PackageCommandFour.php @@ -0,0 +1,32 @@ +info('Package test four'); + + return self::SUCCESS; + } +} diff --git a/workbench/app/Console/Commands/extra/PackageCommandThree.php b/workbench/app/Console/Commands/extra/PackageCommandThree.php new file mode 100644 index 0000000..9d9f37d --- /dev/null +++ b/workbench/app/Console/Commands/extra/PackageCommandThree.php @@ -0,0 +1,32 @@ +info('Package test three'); + + return self::SUCCESS; + } +}