From 5fcc831c310d56a093051479eae3797606b98201 Mon Sep 17 00:00:00 2001 From: Riley Aven Date: Mon, 28 Oct 2024 19:24:24 -0400 Subject: [PATCH 1/2] Ability to optimize command --- src/Package.php | 21 +++++++ src/PackageServiceProvider.php | 6 ++ .../PackageOptimizationTest.php | 57 +++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 tests/PackageServiceProviderTests/PackageOptimizationTest.php diff --git a/src/Package.php b/src/Package.php index 11258be..f9ff096 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'); +}); From ee9dfca13fbabb61a3a3d24fa5200490d7aab7d8 Mon Sep 17 00:00:00 2001 From: Riley19280 Date: Mon, 28 Oct 2024 23:25:07 +0000 Subject: [PATCH 2/2] Fix styling --- src/Package.php | 2 +- src/PackageServiceProvider.php | 2 +- .../PackageOptimizationTest.php | 8 +++----- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Package.php b/src/Package.php index f9ff096..8b00c61 100644 --- a/src/Package.php +++ b/src/Package.php @@ -225,7 +225,7 @@ public function hasOptimization(?string $optimize = null, ?string $clear = null, $this->optimizeCommands[] = [ 'optimize' => $optimize, 'clear' => $clear, - 'key' => $key + 'key' => $key, ]; if (! in_array($optimize, $this->consoleCommands)) { diff --git a/src/PackageServiceProvider.php b/src/PackageServiceProvider.php index 7e611e5..47217f1 100644 --- a/src/PackageServiceProvider.php +++ b/src/PackageServiceProvider.php @@ -108,7 +108,7 @@ public function boot() } if (! empty($this->package->optimizeCommands) && method_exists($this, 'optimizes')) { - foreach($this->package->optimizeCommands as $optimizeCmd) { + foreach ($this->package->optimizeCommands as $optimizeCmd) { $this->optimizes($optimizeCmd['optimize'], $optimizeCmd['clear'], $optimizeCmd['key'] ?? null); } } diff --git a/tests/PackageServiceProviderTests/PackageOptimizationTest.php b/tests/PackageServiceProviderTests/PackageOptimizationTest.php index 5c099fc..fdec443 100644 --- a/tests/PackageServiceProviderTests/PackageOptimizationTest.php +++ b/tests/PackageServiceProviderTests/PackageOptimizationTest.php @@ -1,10 +1,8 @@ assertExitCode(0); }); -it('registered optimize with laravel', function() { +it('registered optimize with laravel', function () { if (version_compare(app()->version(), '11.27.1', '<')) { $this->markTestSkipped('Laravel 11+ functionality'); } @@ -48,10 +46,10 @@ public function configurePackage(Package $package) $this->artisan('optimize')->expectsOutputToContain('laravel-package-tools'); }); -it('registered optimize:clear with laravel', function() { +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'); + $this->artisan('optimize:clear')->expectsOutputToContain('laravel-package-tools'); });