Skip to content

Commit 1f12e50

Browse files
[11.x] Optimize commands registry (#52928)
* [11.x] Optimize commands registration * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent b5a17c7 commit 1f12e50

File tree

5 files changed

+160
-14
lines changed

5 files changed

+160
-14
lines changed

src/Illuminate/Foundation/Console/OptimizeClearCommand.php

Lines changed: 22 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,28 @@ 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+
/**
43+
* Get the commands that should be run to clear the "optimization" files.
44+
*
45+
* @return array
46+
*/
47+
public function getOptimizeClearTasks()
48+
{
49+
return [
50+
'cache' => 'cache:clear',
51+
'compiled' => 'clear-compiled',
52+
'config' => 'config:clear',
53+
'events' => 'event:clear',
54+
'routes' => 'route:clear',
55+
'views' => 'view:clear',
56+
...ServiceProvider::$optimizeClearCommands,
57+
];
58+
}
4559
}

src/Illuminate/Foundation/Console/OptimizeCommand.php

Lines changed: 20 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,26 @@ 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+
/**
43+
* Get the commands that should be run to optimize the framework.
44+
*
45+
* @return array
46+
*/
47+
protected function getOptimizeTasks()
48+
{
49+
return [
50+
'config' => 'config:cache',
51+
'events' => 'event:cache',
52+
'routes' => 'route:cache',
53+
'views' => 'view:cache',
54+
...ServiceProvider::$optimizeCommands,
55+
];
56+
}
4357
}

src/Illuminate/Support/ServiceProvider.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Foundation\CachesRoutes;
99
use Illuminate\Contracts\Support\DeferrableProvider;
1010
use Illuminate\Database\Eloquent\Factory as ModelFactory;
11+
use Illuminate\Support\Str;
1112
use Illuminate\View\Compilers\BladeCompiler;
1213

1314
/**
@@ -58,6 +59,20 @@ abstract class ServiceProvider
5859
*/
5960
protected static $publishableMigrationPaths = [];
6061

62+
/**
63+
* Commands that should be run during the "optimize" command.
64+
*
65+
* @var array<string, string>
66+
*/
67+
public static array $optimizeCommands = [];
68+
69+
/**
70+
* Commands that should be run during the "optimize:clear" command.
71+
*
72+
* @var array<string, string>
73+
*/
74+
public static array $optimizeClearCommands = [];
75+
6176
/**
6277
* Create a new service provider instance.
6378
*
@@ -460,6 +475,36 @@ public function commands($commands)
460475
});
461476
}
462477

478+
/**
479+
* Register commands that should run on "optimize" or "optimize:clear".
480+
*
481+
* @param string $optimize
482+
* @param string $clear
483+
* @param string|null $key
484+
* @return void
485+
*/
486+
protected function optimizes(string $optimize = null, string $clear = null, ?string $key = null)
487+
{
488+
$key ??= (string) Str::of(get_class($this))
489+
->classBasename()
490+
->before('ServiceProvider')
491+
->kebab()
492+
->lower()
493+
->trim();
494+
495+
if (empty($key)) {
496+
$key = class_basename(get_class($this));
497+
}
498+
499+
if ($optimize) {
500+
static::$optimizeCommands[$key] = $optimize;
501+
}
502+
503+
if ($clear) {
504+
static::$optimizeClearCommands[$key] = $clear;
505+
}
506+
}
507+
463508
/**
464509
* Get the services provided by the provider.
465510
*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 [ServiceProviderWithOptimizeClear::class];
14+
}
15+
16+
public function testCanListenToOptimizingEvent(): void
17+
{
18+
$this->artisan('optimize:clear')
19+
->assertSuccessful()
20+
->expectsOutputToContain('ServiceProviderWithOptimizeClear');
21+
}
22+
}
23+
24+
class ServiceProviderWithOptimizeClear extends ServiceProvider
25+
{
26+
public function boot(): void
27+
{
28+
$this->commands([
29+
new ClosureCommand('my_package:clear', fn () => 0),
30+
]);
31+
32+
$this->optimizes(
33+
clear: 'my_package:clear',
34+
);
35+
}
36+
}
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 [ServiceProviderWithOptimize::class];
14+
}
15+
16+
public function testCanListenToOptimizingEvent(): void
17+
{
18+
$this->artisan('optimize')
19+
->assertSuccessful()
20+
->expectsOutputToContain('my package');
21+
}
22+
}
23+
24+
class ServiceProviderWithOptimize extends ServiceProvider
25+
{
26+
public function boot(): void
27+
{
28+
$this->commands([
29+
new ClosureCommand('my_package:cache', fn () => 0),
30+
]);
31+
32+
$this->optimizes(
33+
optimize: 'my_package:cache',
34+
key: 'my package',
35+
);
36+
}
37+
}

0 commit comments

Comments
 (0)