Skip to content

Commit 531e7f0

Browse files
committed
[11.x] Optimize commands registration
1 parent 09aaeb4 commit 531e7f0

File tree

5 files changed

+145
-14
lines changed

5 files changed

+145
-14
lines changed

src/Illuminate/Foundation/Console/OptimizeClearCommand.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Foundation\Console;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Contracts\Events\Dispatcher;
67
use Symfony\Component\Console\Attribute\AsCommand;
78

89
#[AsCommand(name: 'optimize:clear')]
@@ -22,6 +23,12 @@ class OptimizeClearCommand extends Command
2223
*/
2324
protected $description = 'Remove the cached bootstrap files';
2425

26+
public function __construct(
27+
protected Dispatcher $events,
28+
) {
29+
parent::__construct();
30+
}
31+
2532
/**
2633
* Execute the console command.
2734
*
@@ -31,15 +38,23 @@ public function handle()
3138
{
3239
$this->components->info('Clearing cached bootstrap files.');
3340

34-
collect([
35-
'cache' => fn () => $this->callSilent('cache:clear') == 0,
36-
'compiled' => fn () => $this->callSilent('clear-compiled') == 0,
37-
'config' => fn () => $this->callSilent('config:clear') == 0,
38-
'events' => fn () => $this->callSilent('event:clear') == 0,
39-
'routes' => fn () => $this->callSilent('route:clear') == 0,
40-
'views' => fn () => $this->callSilent('view:clear') == 0,
41-
])->each(fn ($task, $description) => $this->components->task($description, $task));
41+
foreach ($this->getOptimizeClearTasks() as $description => $command) {
42+
$this->components->task($description, fn () => $this->callSilently($command) == 0);
43+
}
4244

4345
$this->newLine();
4446
}
47+
48+
public function getOptimizeClearTasks(): array
49+
{
50+
return [
51+
'cache' => 'cache:clear',
52+
'compiled' => 'clear-compiled',
53+
'config' => 'config:clear',
54+
'events' => 'event:clear',
55+
'routes' => 'route:clear',
56+
'views' => 'view:clear',
57+
...ServiceProvider::$optimizeClearingCommands
58+
];
59+
}
4560
}

src/Illuminate/Foundation/Console/OptimizeCommand.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Illuminate\Foundation\Console;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Contracts\Events\Dispatcher;
7+
use Illuminate\Foundation\Events\Optimizing;
8+
use Illuminate\Support\ServiceProvider;
69
use Symfony\Component\Console\Attribute\AsCommand;
710

811
#[AsCommand(name: 'optimize')]
@@ -22,6 +25,12 @@ class OptimizeCommand extends Command
2225
*/
2326
protected $description = 'Cache framework bootstrap, configuration, and metadata to increase performance';
2427

28+
public function __construct(
29+
protected Dispatcher $events,
30+
) {
31+
parent::__construct();
32+
}
33+
2534
/**
2635
* Execute the console command.
2736
*
@@ -31,13 +40,21 @@ public function handle()
3140
{
3241
$this->components->info('Caching framework bootstrap, configuration, and metadata.');
3342

34-
collect([
35-
'config' => fn () => $this->callSilent('config:cache') == 0,
36-
'events' => fn () => $this->callSilent('event:cache') == 0,
37-
'routes' => fn () => $this->callSilent('route:cache') == 0,
38-
'views' => fn () => $this->callSilent('view:cache') == 0,
39-
])->each(fn ($task, $description) => $this->components->task($description, $task));
43+
foreach ($this->getOptimizeTasks() as $description => $command) {
44+
$this->components->task($description, fn () => $this->callSilently($command) == 0);
45+
}
4046

4147
$this->newLine();
4248
}
49+
50+
protected function getOptimizeTasks(): array
51+
{
52+
return [
53+
'config' => 'config:cache',
54+
'events' => 'event:cache',
55+
'routes' => 'route:cache',
56+
'views' => 'view:cache',
57+
...ServiceProvider::$optimizationCommands
58+
];
59+
}
4360
}

src/Illuminate/Support/ServiceProvider.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ abstract class ServiceProvider
5151
*/
5252
public static $publishGroups = [];
5353

54+
/**
55+
* Commands that should be run during the optimize command.
56+
*
57+
* @var array<string, string>
58+
*/
59+
public static array $optimizationCommands = [];
60+
61+
/**
62+
* Commands that should be run during the optimize:clear command.
63+
*
64+
* @var array<string, string>
65+
*/
66+
public static array $optimizeClearingCommands = [];
67+
5468
/**
5569
* The migration paths available for publishing.
5670
*
@@ -537,4 +551,15 @@ public static function addProviderToBootstrapFile(string $provider, ?string $pat
537551

538552
return true;
539553
}
554+
555+
protected function registerOptimizeCommands(string $key, string $optimize = null, string $optimizeClear = null): void
556+
{
557+
if ($optimize) {
558+
static::$optimizationCommands[$key] = $optimize;
559+
}
560+
561+
if ($optimizeClear) {
562+
static::$optimizeClearingCommands[$key] = $optimizeClear;
563+
}
564+
}
540565
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Foundation\Console;
4+
5+
use Illuminate\Foundation\Console\ClosureCommand;
6+
use Illuminate\Support\ServiceProvider;
7+
use Illuminate\Tests\Integration\Generators\TestCase;
8+
9+
class OptimizeClearCommandTest extends TestCase
10+
{
11+
protected function getPackageProviders($app): array
12+
{
13+
return [TestServiceProvider::class];
14+
}
15+
16+
public function testCanListenToOptimizingEvent(): void
17+
{
18+
$this->artisan('optimize:clear')
19+
->assertSuccessful()
20+
->expectsOutputToContain('my package');
21+
}
22+
}
23+
24+
class TestServiceProvider extends ServiceProvider
25+
{
26+
public function boot(): void
27+
{
28+
$this->commands([
29+
new ClosureCommand('my_package:clear', fn () => 0),
30+
]);
31+
32+
$this->registerOptimizeCommands(
33+
key: 'my package',
34+
optimizeClear: 'my_package:clear',
35+
);
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Foundation\Console;
4+
5+
use Illuminate\Foundation\Console\ClosureCommand;
6+
use Illuminate\Support\ServiceProvider;
7+
use Illuminate\Tests\Integration\Generators\TestCase;
8+
9+
class OptimizeCommandTest extends TestCase
10+
{
11+
protected function getPackageProviders($app): array
12+
{
13+
return [TestServiceProvider::class];
14+
}
15+
16+
public function testCanListenToOptimizingEvent(): void
17+
{
18+
$this->artisan('optimize')
19+
->assertSuccessful()
20+
->expectsOutputToContain('my package');
21+
}
22+
}
23+
24+
class TestServiceProvider extends ServiceProvider
25+
{
26+
public function boot(): void
27+
{
28+
$this->commands([
29+
new ClosureCommand('my_package:cache', fn () => 0),
30+
]);
31+
32+
$this->registerOptimizeCommands(
33+
key: 'my package',
34+
optimize: 'my_package:cache',
35+
);
36+
}
37+
}

0 commit comments

Comments
 (0)