Skip to content

Commit

Permalink
[11.x] Optimize commands registration
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgaal committed Sep 27, 2024
1 parent 09aaeb4 commit 5e8231e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 14 deletions.
25 changes: 17 additions & 8 deletions src/Illuminate/Foundation/Console/OptimizeClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Foundation\Console;

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

#[AsCommand(name: 'optimize:clear')]
Expand Down Expand Up @@ -31,15 +32,23 @@ public function handle()
{
$this->components->info('Clearing cached bootstrap files.');

collect([
'cache' => fn () => $this->callSilent('cache:clear') == 0,
'compiled' => fn () => $this->callSilent('clear-compiled') == 0,
'config' => fn () => $this->callSilent('config:clear') == 0,
'events' => fn () => $this->callSilent('event:clear') == 0,
'routes' => fn () => $this->callSilent('route:clear') == 0,
'views' => fn () => $this->callSilent('view:clear') == 0,
])->each(fn ($task, $description) => $this->components->task($description, $task));
foreach ($this->getOptimizeClearTasks() as $description => $command) {
$this->components->task($description, fn () => $this->callSilently($command) == 0);
}

$this->newLine();
}

public function getOptimizeClearTasks(): array
{
return [
'cache' => 'cache:clear',
'compiled' => 'clear-compiled',
'config' => 'config:clear',
'events' => 'event:clear',
'routes' => 'route:clear',
'views' => 'view:clear',
...ServiceProvider::$optimizeClearingCommands,
];
}
}
21 changes: 15 additions & 6 deletions src/Illuminate/Foundation/Console/OptimizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Foundation\Console;

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

#[AsCommand(name: 'optimize')]
Expand Down Expand Up @@ -31,13 +32,21 @@ public function handle()
{
$this->components->info('Caching framework bootstrap, configuration, and metadata.');

collect([
'config' => fn () => $this->callSilent('config:cache') == 0,
'events' => fn () => $this->callSilent('event:cache') == 0,
'routes' => fn () => $this->callSilent('route:cache') == 0,
'views' => fn () => $this->callSilent('view:cache') == 0,
])->each(fn ($task, $description) => $this->components->task($description, $task));
foreach ($this->getOptimizeTasks() as $description => $command) {
$this->components->task($description, fn () => $this->callSilently($command) == 0);
}

$this->newLine();
}

protected function getOptimizeTasks(): array
{
return [
'config' => 'config:cache',
'events' => 'event:cache',
'routes' => 'route:cache',
'views' => 'view:cache',
...ServiceProvider::$optimizingCommands,
];
}
}
25 changes: 25 additions & 0 deletions src/Illuminate/Support/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ abstract class ServiceProvider
*/
public static $publishGroups = [];

/**
* Commands that should be run during the optimize command.
*
* @var array<string, string>
*/
public static array $optimizingCommands = [];

/**
* Commands that should be run during the optimize:clear command.
*
* @var array<string, string>
*/
public static array $optimizeClearingCommands = [];

/**
* The migration paths available for publishing.
*
Expand Down Expand Up @@ -537,4 +551,15 @@ public static function addProviderToBootstrapFile(string $provider, ?string $pat

return true;
}

protected function registerOptimizeCommands(string $key, string $optimize = null, string $optimizeClear = null): void
{
if ($optimize) {
static::$optimizingCommands[$key] = $optimize;
}

if ($optimizeClear) {
static::$optimizeClearingCommands[$key] = $optimizeClear;
}
}
}
37 changes: 37 additions & 0 deletions tests/Integration/Foundation/Console/OptimizeClearCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\Tests\Integration\Foundation\Console;

use Illuminate\Foundation\Console\ClosureCommand;
use Illuminate\Support\ServiceProvider;
use Illuminate\Tests\Integration\Generators\TestCase;

class OptimizeClearCommandTest extends TestCase
{
protected function getPackageProviders($app): array
{
return [ServiceProviderWithOptimizeClear::class];
}

public function testCanListenToOptimizingEvent(): void
{
$this->artisan('optimize:clear')
->assertSuccessful()
->expectsOutputToContain('my package');
}
}

class ServiceProviderWithOptimizeClear extends ServiceProvider
{
public function boot(): void
{
$this->commands([
new ClosureCommand('my_package:clear', fn () => 0),
]);

$this->registerOptimizeCommands(
key: 'my package',
optimizeClear: 'my_package:clear',
);
}
}
37 changes: 37 additions & 0 deletions tests/Integration/Foundation/Console/OptimizeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\Tests\Integration\Foundation\Console;

use Illuminate\Foundation\Console\ClosureCommand;
use Illuminate\Support\ServiceProvider;
use Illuminate\Tests\Integration\Generators\TestCase;

class OptimizeCommandTest extends TestCase
{
protected function getPackageProviders($app): array
{
return [ServiceProviderWithOptimize::class];
}

public function testCanListenToOptimizingEvent(): void
{
$this->artisan('optimize')
->assertSuccessful()
->expectsOutputToContain('my package');
}
}

class ServiceProviderWithOptimize extends ServiceProvider
{
public function boot(): void
{
$this->commands([
new ClosureCommand('my_package:cache', fn () => 0),
]);

$this->registerOptimizeCommands(
key: 'my package',
optimize: 'my_package:cache',
);
}
}

0 comments on commit 5e8231e

Please sign in to comment.