diff --git a/src/Package.php b/src/Package.php index 940c144..a999085 100644 --- a/src/Package.php +++ b/src/Package.php @@ -41,6 +41,8 @@ class Package public ?string $publishableProviderName = null; + public array $optimizeCommands = []; + public function name(string $name): static { $this->name = $name; @@ -218,6 +220,25 @@ public function hasRoutes(...$routeFileNames): static return $this; } + public function hasOptimization(?string $optimize = null, ?string $clear = null, ?string $key = null): static + { + $this->optimizeCommands[] = [ + 'optimize' => $optimize, + 'clear' => $clear, + 'key' => $key + ]; + + if (! in_array($optimize, $this->consoleCommands)) { + $this->consoleCommands[] = $optimize; + } + + if (! in_array($clear, $this->consoleCommands)) { + $this->consoleCommands[] = $clear; + } + + return $this; + } + public function basePath(string $directory = null): string { if ($directory === null) { diff --git a/src/PackageServiceProvider.php b/src/PackageServiceProvider.php index 4a737c0..7e611e5 100644 --- a/src/PackageServiceProvider.php +++ b/src/PackageServiceProvider.php @@ -106,6 +106,12 @@ public function boot() $this->package->basePath('/../resources/dist') => public_path("vendor/{$this->package->shortName()}"), ], "{$this->package->shortName()}-assets"); } + + if (! empty($this->package->optimizeCommands) && method_exists($this, 'optimizes')) { + foreach($this->package->optimizeCommands as $optimizeCmd) { + $this->optimizes($optimizeCmd['optimize'], $optimizeCmd['clear'], $optimizeCmd['key'] ?? null); + } + } } if (! empty($this->package->commands)) { diff --git a/tests/PackageServiceProviderTests/PackageOptimizationTest.php b/tests/PackageServiceProviderTests/PackageOptimizationTest.php new file mode 100644 index 0000000..5c099fc --- /dev/null +++ b/tests/PackageServiceProviderTests/PackageOptimizationTest.php @@ -0,0 +1,57 @@ +name('laravel-package-tools'); + + if (version_compare(app()->version(), '11.27.1', '>=')) { + $package->hasOptimization(TestCommand::class, OtherTestCommand::class, 'laravel-package-tools'); + } + } +} + +uses(ConfigurePackageOptimizationTest::class); + +it('can call optimize commands', function () { + if (version_compare(app()->version(), '11.27.1', '<')) { + $this->markTestSkipped('Laravel 11+ functionality'); + } + + $this + ->artisan('test-command') + ->assertExitCode(0); +}); + +it('can call optimize:clear commands', function () { + if (version_compare(app()->version(), '11.27.1', '<')) { + $this->markTestSkipped('Laravel 11+ functionality'); + } + + $this + ->artisan('other-test-command') + ->assertExitCode(0); +}); + +it('registered optimize with laravel', function() { + if (version_compare(app()->version(), '11.27.1', '<')) { + $this->markTestSkipped('Laravel 11+ functionality'); + } + + $this->artisan('optimize')->expectsOutputToContain('laravel-package-tools'); +}); + +it('registered optimize:clear with laravel', function() { + if (version_compare(app()->version(), '11.27.1', '<')) { + $this->markTestSkipped('Laravel 11+ functionality'); + } + + $this->artisan('optimize:clear')->expectsOutputToContain('laravel-package-tools'); +});