Skip to content

Commit 62b5698

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

File tree

5 files changed

+131
-14
lines changed

5 files changed

+131
-14
lines changed

src/Illuminate/Foundation/Console/OptimizeClearCommand.php

Lines changed: 17 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\Support\ServiceProvider;
67
use Symfony\Component\Console\Attribute\AsCommand;
78

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

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));
35+
foreach ($this->getOptimizeClearTasks() as $description => $command) {
36+
$this->components->task($description, fn () => $this->callSilently($command) == 0);
37+
}
4238

4339
$this->newLine();
4440
}
41+
42+
public function getOptimizeClearTasks(): array
43+
{
44+
return [
45+
'cache' => 'cache:clear',
46+
'compiled' => 'clear-compiled',
47+
'config' => 'config:clear',
48+
'events' => 'event:clear',
49+
'routes' => 'route:clear',
50+
'views' => 'view:clear',
51+
...ServiceProvider::$optimizeClearingCommands,
52+
];
53+
}
4554
}

src/Illuminate/Foundation/Console/OptimizeCommand.php

Lines changed: 15 additions & 6 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\Support\ServiceProvider;
67
use Symfony\Component\Console\Attribute\AsCommand;
78

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

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));
35+
foreach ($this->getOptimizeTasks() as $description => $command) {
36+
$this->components->task($description, fn () => $this->callSilently($command) == 0);
37+
}
4038

4139
$this->newLine();
4240
}
41+
42+
protected function getOptimizeTasks(): array
43+
{
44+
return [
45+
'config' => 'config:cache',
46+
'events' => 'event:cache',
47+
'routes' => 'route:cache',
48+
'views' => 'view:cache',
49+
...ServiceProvider::$optimizingCommands,
50+
];
51+
}
4352
}

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 $optimizingCommands = [];
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::$optimizingCommands[$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)