24
24
use Symfony \Component \Filesystem \Filesystem ;
25
25
use Symfony \Component \Finder \Finder ;
26
26
use Symfony \Component \HttpKernel \CacheClearer \CacheClearerInterface ;
27
+ use Symfony \Component \HttpKernel \Event \Cache \CacheWarmerAggregateItemFinishedEvent ;
27
28
use Symfony \Component \HttpKernel \RebootableInterface ;
28
29
29
30
/**
37
38
#[AsCommand(name: 'cache:clear ' , description: 'Clear the cache ' )]
38
39
class CacheClearCommand extends Command
39
40
{
40
- private CacheClearerInterface $ cacheClearer ;
41
41
private Filesystem $ filesystem ;
42
+ private EventDispatcher $ eventDispatcher ;
42
43
43
- public function __construct (CacheClearerInterface $ cacheClearer , Filesystem $ filesystem = null )
44
- {
44
+ public function __construct (
45
+ private readonly CacheClearerInterface $ cacheClearer ,
46
+ Filesystem $ filesystem = null
47
+ ) {
45
48
parent ::__construct ();
46
49
47
- $ this ->cacheClearer = $ cacheClearer ;
48
E864
50
$ this ->filesystem = $ filesystem ?? new Filesystem ();
49
51
}
50
52
@@ -107,7 +109,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107
109
$ this ->cacheClearer ->clear ($ realCacheDir );
108
110
109
111
// The current event dispatcher is stale, let's not use it anymore
110
- $ this ->getApplication ()->setDispatcher (new EventDispatcher ());
112
+ $ this ->eventDispatcher = new EventDispatcher ();
113
+ $ this ->getApplication ()->setDispatcher ($ this ->eventDispatcher );
111
114
112
115
$ containerFile = (new \ReflectionObject ($ kernel ->getContainer ()))->getFileName ();
113
116
$ containerDir = basename (\dirname ($ containerFile ));
@@ -121,6 +124,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
121
124
}
122
125
$ fs ->remove ($ warmupDir );
123
126
127
+ if ($ io ->isDebug ()) {
128
+ $ this ->eventDispatcher ->addListener (CacheWarmerAggregateItemFinishedEvent::class, function (CacheWarmerAggregateItemFinishedEvent $ event ) use ($ io ) {
129
+ $ io ->info (sprintf ('"%s" finished warmup in %.2f ms. ' , $ event ->getWarmerClass (), $ event ->getTime ()));
130
+ });
131
+ }
132
+
124
133
if ($ _SERVER ['REQUEST_TIME ' ] <= filemtime ($ containerFile ) && filemtime ($ containerFile ) <= time ()) {
125
134
if ($ output ->isVerbose ()) {
126
135
$ io ->comment ('Cache is fresh. ' );
@@ -132,7 +141,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
132
141
$ warmer = $ kernel ->getContainer ()->get ('cache_warmer ' );
133
142
// non optional warmers already ran during container compilation
134
143
$ warmer ->enableOnlyOptionalWarmers ();
135
- $ preload = (array ) $ warmer ->warmUp ($ realCacheDir );
144
+ $ preload = (array ) $ warmer ->warmUp ($ realCacheDir, $ this -> eventDispatcher );
136
145
137
146
if ($ preload && file_exists ($ preloadFile = $ realCacheDir .'/ ' .$ kernel ->getContainer ()->getParameter ('kernel.container_class ' ).'.preload.php ' )) {
138
147
Preloader::append ($ preloadFile , $ preload );
@@ -145,7 +154,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
145
154
if ($ output ->isVerbose ()) {
146
155
$ io ->comment ('Warming up cache... ' );
147
156
}
148
- $ this ->warmup ($ warmupDir , $ realCacheDir , !$ input ->getOption ('no-optional-warmers ' ));
157
+ $ this ->warmup ($ io , $ warmupDir , $ realCacheDir , !$ input ->getOption ('no-optional-warmers ' ));
149
158
}
150
159
151
160
if (!$ fs ->exists ($ warmupDir .'/ ' .$ containerDir )) {
@@ -219,7 +228,7 @@ private function isNfs(string $dir): bool
219
228
return false ;
220
229
}
221
230
222
- private function warmup (string $ warmupDir , string $ realBuildDir , bool $ enableOptionalWarmers = true ): void
231
+ private function warmup (SymfonyStyle $ io , string $ warmupDir , string $ realBuildDir , bool $ enableOptionalWarmers = true ): void
223
232
{
224
233
// create a temporary kernel
225
234
$ kernel = $ this ->getApplication ()->getKernel ();
@@ -233,7 +242,7 @@ private function warmup(string $warmupDir, string $realBuildDir, bool $enableOpt
233
242
$ warmer = $ kernel ->getContainer ()->get ('cache_warmer ' );
234
243
// non optional warmers already ran during container compilation
235
244
$ warmer ->enableOnlyOptionalWarmers ();
236
- $ preload = (array ) $ warmer ->warmUp ($ warmupDir );
245
+ $ preload = (array ) $ warmer ->warmUp ($ warmupDir, $ this -> eventDispatcher );
237
246
238
247
if ($ preload && file_exists ($ preloadFile = $ warmupDir .'/ ' .$ kernel ->getContainer ()->getParameter ('kernel.container_class ' ).'.preload.php ' )) {
239
248
Preloader::append ($ preloadFile , $ preload );
0 commit comments