Skip to content

Commit

Permalink
[11.x] Allow exceptions to the optimize and optimize:clear comman…
Browse files Browse the repository at this point in the history
…ds (#54070)

* Allow exceptions to the optimize commands

* Fix incorrect method name

* Add tests

* Update OptimizeClearCommand.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
jonerickson and taylorotwell authored Jan 9, 2025
1 parent 6694ebc commit ceaedb4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/Illuminate/Foundation/Console/OptimizeClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Illuminate\Foundation\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'optimize:clear')]
class OptimizeClearCommand extends Command
Expand Down Expand Up @@ -32,7 +34,17 @@ public function handle()
{
$this->components->info('Clearing cached bootstrap files.');

foreach ($this->getOptimizeClearTasks() as $description => $command) {
$exceptions = Collection::wrap(explode(',', $this->option('except')))
->map(fn ($except) => trim($except))
->filter()
->unique()
->flip();

$tasks = Collection::wrap($this->getOptimizeClearTasks())
->reject(fn ($command, $key) => $exceptions->hasAny([$command, $key]))
->toArray();

foreach ($tasks as $description => $command) {
$this->components->task($description, fn () => $this->callSilently($command) == 0);
}

Expand All @@ -56,4 +68,16 @@ public function getOptimizeClearTasks()
...ServiceProvider::$optimizeClearCommands,
];
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getOptions()
{
return [
['except', 'e', InputOption::VALUE_OPTIONAL, 'The commands to skip'],
];
}
}
26 changes: 25 additions & 1 deletion src/Illuminate/Foundation/Console/OptimizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Illuminate\Foundation\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'optimize')]
class OptimizeCommand extends Command
Expand Down Expand Up @@ -32,7 +34,17 @@ public function handle()
{
$this->components->info('Caching framework bootstrap, configuration, and metadata.');

foreach ($this->getOptimizeTasks() as $description => $command) {
$exceptions = Collection::wrap(explode(',', $this->option('except')))
->map(fn ($except) => trim($except))
->filter()
->unique()
->flip();

$tasks = Collection::wrap($this->getOptimizeTasks())
->reject(fn ($command, $key) => $exceptions->hasAny([$command, $key]))
->toArray();

foreach ($tasks as $description => $command) {
$this->components->task($description, fn () => $this->callSilently($command) == 0);
}

Expand All @@ -54,4 +66,16 @@ protected function getOptimizeTasks()
...ServiceProvider::$optimizeCommands,
];
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getOptions()
{
return [
['except', 'e', InputOption::VALUE_OPTIONAL, 'Do not run the commands matching the key or name'],
];
}
}
14 changes: 14 additions & 0 deletions tests/Integration/Foundation/Console/OptimizeClearCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ public function testCanListenToOptimizingEvent(): void
->assertSuccessful()
->expectsOutputToContain('ServiceProviderWithOptimizeClear');
}

public function testCanExcludeCommandsByKey(): void
{
$this->artisan('optimize:clear', ['--except' => 'my package'])
->assertSuccessful()
->doesntExpectOutputToContain('my package');
}

public function testCanExcludeCommandsByCommand(): void
{
$this->artisan('optimize:clear', ['--except' => 'my_package:cache'])
->assertSuccessful()
->doesntExpectOutputToContain('my_package:cache');
}
}

class ServiceProviderWithOptimizeClear extends ServiceProvider
Expand Down
14 changes: 14 additions & 0 deletions tests/Integration/Foundation/Console/OptimizeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ public function testCanListenToOptimizingEvent(): void
->assertSuccessful()
->expectsOutputToContain('my package');
}

public function testCanExcludeCommandsByKey(): void
{
$this->artisan('optimize', ['--except' => 'my package'])
->assertSuccessful()
->doesntExpectOutputToContain('my package');
}

public function testCanExcludeCommandsByCommand(): void
{
$this->artisan('optimize', ['--except' => 'my_package:cache'])
->assertSuccessful()
->doesntExpectOutputToContain('my_package:cache');
}
}

class ServiceProviderWithOptimize extends ServiceProvider
Expand Down

0 comments on commit ceaedb4

Please sign in to comment.