@@ -501,25 +501,72 @@ protected function initializeContainer()
501
501
$ class = $ this ->getContainerClass ();
502
502
$ cacheDir = $ this ->warmupDir ?: $ this ->getCacheDir ();
503
503
$ cache = new ConfigCache ($ cacheDir .'/ ' .$ class .'.php ' , $ this ->debug );
504
- $ oldContainer = null ;
505
- if ($ fresh = $ cache ->isFresh ()) {
506
- // Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
507
- $ errorLevel = error_reporting (\E_ALL ^ \E_WARNING );
508
- $ fresh = $ oldContainer = false ;
509
- try {
510
- if (file_exists ($ cache ->getPath ()) && \is_object ($ this ->container = include $ cache ->getPath ())) {
511
- $ this ->container ->set ('kernel ' , $ this );
512
- $ oldContainer = $ this ->container ;
513
- $ fresh = true ;
514
- }
515
- } catch (\Throwable $ e ) {
516
- } finally {
504
+
505
+ // Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
506
+ $ errorLevel = error_reporting (\E_ALL ^ \E_WARNING );
507
+
508
+ try {
509
+ if (file_exists ($ cache ->getPath ()) && \is_object ($ this ->container = include $ cache ->getPath ()) && (!$ this ->debug || $ cache ->isFresh ())) {
510
+ $ this ->container ->set ('kernel ' , $ this );
517
511
error_reporting ($ errorLevel );
512
+
513
+ return ;
518
514
}
515
+ } catch (\Throwable $ e ) {
519
516
}
520
517
521
- if ($ fresh ) {
522
- return ;
518
+ $ oldContainer = \is_object ($ this ->container ) ? new \ReflectionClass ($ this ->container ) : null ;
519
+
520
+ try {
521
+ is_dir ($ cacheDir ) ?: mkdir ($ cacheDir , 0777 , true );
522
+
523
+ if ($ lock = fopen ($ cache ->getPath (), 'w ' )) {
524
+ chmod ($ cache ->getPath (), 0666 & ~umask ());
525
+ flock ($ lock , LOCK_EX | LOCK_NB , $ wouldBlock );
526
+
527
+ if (!flock ($ lock , $ wouldBlock ? LOCK_SH : LOCK_EX )) {
528
+ fclose ($ lock );
529
+ } else {
530
+ $ cache = new class ($ cache ->getPath (), $ this ->debug ) extends ConfigCache {
531
+ public $ lock ;
532
+
533
+ public function write ($ content , array $ metadata = null )
534
+ {
535
+ fwrite ($ this ->lock , $ content );
536
+
537
+ if (null !== $ metadata ) {
538
+ file_put_contents ($ this ->getPath ().'.meta ' , serialize ($ metadata ));
539
+ @chmod ($ this ->getPath ().'.meta ' , 0666 & ~umask ());
540
+ }
541
+
542
+ if (\function_exists ('opcache_invalidate ' ) && filter_var (ini_get ('opcache.enable ' ), FILTER_VALIDATE_BOOLEAN )) {
543
+ opcache_invalidate ($ this ->getPath (), true );
544
+ }
545
+
546
+ $ this ->__destruct ();
547
+ }
548
+
549
+ public function __destruct ()
550
+ {
551
+ if ($ this ->lock ) {
552
+ flock ($ this ->lock , LOCK_UN );
553
+ fclose ($ this ->lock );
554
+ $ this ->lock = null ;
555
+ }
556
+ }
557
+ };
558
+ $ cache ->lock = $ lock ;
559
+
560
+ if (\is_object ($ this ->container = include $ cache ->getPath ()) && (!$ oldContainer || \get_class ($ this ->container ) !== $ oldContainer ->name )) {
561
+ $ this ->container ->set ('kernel ' , $ this );
562
+
563
+ return ;
564
+ }
565
+ }
566
+ }
567
+ } catch (\Throwable $ e ) {
568
+ } finally {
569
+ error_reporting ($ errorLevel );
523
570
}
524
571
525
572
if ($ collectDeprecations = $ this ->debug && !\defined ('PHPUNIT_COMPOSER_INSTALL ' )) {
@@ -565,7 +612,7 @@ protected function initializeContainer()
565
612
}
566
613
567
614
try {
568
- $ container = null ;
615
+ $ container = $ this -> container = null ;
569
616
$ container = $ this ->buildContainer ();
570
617
$ container ->compile ();
571
618
} finally {
@@ -577,17 +624,6 @@ protected function initializeContainer()
577
624
}
578
625
}
579
626
580
- if (null === $ oldContainer && file_exists ($ cache ->getPath ())) {
581
- $ errorLevel = error_reporting (\E_ALL ^ \E_WARNING );
582
- try {
583
- $ oldContainer = include $ cache ->getPath ();
584
- } catch (\Throwable $ e ) {
585
- } finally {
586
- error_reporting ($ errorLevel );
587
- }
588
- }
589
- $ oldContainer = \is_object ($ oldContainer ) ? new \ReflectionClass ($ oldContainer ) : false ;
590
-
591
627
$ this ->dumpContainer ($ cache , $ container , $ class , $ this ->getContainerBaseClass ());
592
628
$ this ->container = require $ cache ->getPath ();
593
629
$ this ->container ->set ('kernel ' , $ this );
0 commit comments