vendor/symfony/http-kernel/CacheWarmer/CacheWarmerAggregate.php line 64

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpKernel\CacheWarmer;
  11. /**
  12.  * Aggregates several cache warmers into a single one.
  13.  *
  14.  * @author Fabien Potencier <fabien@symfony.com>
  15.  *
  16.  * @final
  17.  */
  18. class CacheWarmerAggregate implements CacheWarmerInterface
  19. {
  20.     private iterable $warmers;
  21.     private bool $debug;
  22.     private ?string $deprecationLogsFilepath;
  23.     private bool $optionalsEnabled false;
  24.     private bool $onlyOptionalsEnabled false;
  25.     /**
  26.      * @param iterable<mixed, CacheWarmerInterface> $warmers
  27.      */
  28.     public function __construct(iterable $warmers = [], bool $debug falsestring $deprecationLogsFilepath null)
  29.     {
  30.         $this->warmers $warmers;
  31.         $this->debug $debug;
  32.         $this->deprecationLogsFilepath $deprecationLogsFilepath;
  33.     }
  34.     public function enableOptionalWarmers()
  35.     {
  36.         $this->optionalsEnabled true;
  37.     }
  38.     public function enableOnlyOptionalWarmers()
  39.     {
  40.         $this->onlyOptionalsEnabled $this->optionalsEnabled true;
  41.     }
  42.     public function warmUp(string $cacheDir): array
  43.     {
  44.         if ($collectDeprecations $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
  45.             $collectedLogs = [];
  46.             $previousHandler set_error_handler(function ($type$message$file$line) use (&$collectedLogs, &$previousHandler) {
  47.                 if (\E_USER_DEPRECATED !== $type && \E_DEPRECATED !== $type) {
  48.                     return $previousHandler $previousHandler($type$message$file$line) : false;
  49.                 }
  50.                 if (isset($collectedLogs[$message])) {
  51.                     ++$collectedLogs[$message]['count'];
  52.                     return null;
  53.                 }
  54.                 $backtrace debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS3);
  55.                 // Clean the trace by removing first frames added by the error handler itself.
  56.                 for ($i 0; isset($backtrace[$i]); ++$i) {
  57.                     if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
  58.                         $backtrace \array_slice($backtrace$i);
  59.                         break;
  60.                     }
  61.                 }
  62.                 $collectedLogs[$message] = [
  63.                     'type' => $type,
  64.                     'message' => $message,
  65.                     'file' => $file,
  66.                     'line' => $line,
  67.                     'trace' => $backtrace,
  68.                     'count' => 1,
  69.                 ];
  70.                 return null;
  71.             });
  72.         }
  73.         $preload = [];
  74.         try {
  75.             foreach ($this->warmers as $warmer) {
  76.                 if (!$this->optionalsEnabled && $warmer->isOptional()) {
  77.                     continue;
  78.                 }
  79.                 if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) {
  80.                     continue;
  81.                 }
  82.                 $preload[] = array_values((array) $warmer->warmUp($cacheDir));
  83.             }
  84.         } finally {
  85.             if ($collectDeprecations) {
  86.                 restore_error_handler();
  87.                 if (is_file($this->deprecationLogsFilepath)) {
  88.                     $previousLogs unserialize(file_get_contents($this->deprecationLogsFilepath));
  89.                     if (\is_array($previousLogs)) {
  90.                         $collectedLogs array_merge($previousLogs$collectedLogs);
  91.                     }
  92.                 }
  93.                 file_put_contents($this->deprecationLogsFilepathserialize(array_values($collectedLogs)));
  94.             }
  95.         }
  96.         return array_values(array_unique(array_merge([], ...$preload)));
  97.     }
  98.     public function isOptional(): bool
  99.     {
  100.         return false;
  101.     }
  102. }