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

Open in your IDE?
  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 $warmers;
  21.     private $debug;
  22.     private $deprecationLogsFilepath;
  23.     private $optionalsEnabled false;
  24.     private $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.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function warmUp(string $cacheDir): array
  46.     {
  47.         if ($collectDeprecations $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
  48.             $collectedLogs = [];
  49.             $previousHandler set_error_handler(function ($type$message$file$line) use (&$collectedLogs, &$previousHandler) {
  50.                 if (\E_USER_DEPRECATED !== $type && \E_DEPRECATED !== $type) {
  51.                     return $previousHandler $previousHandler($type$message$file$line) : false;
  52.                 }
  53.                 if (isset($collectedLogs[$message])) {
  54.                     ++$collectedLogs[$message]['count'];
  55.                     return null;
  56.                 }
  57.                 $backtrace debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS3);
  58.                 // Clean the trace by removing first frames added by the error handler itself.
  59.                 for ($i 0; isset($backtrace[$i]); ++$i) {
  60.                     if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
  61.                         $backtrace \array_slice($backtrace$i);
  62.                         break;
  63.                     }
  64.                 }
  65.                 $collectedLogs[$message] = [
  66.                     'type' => $type,
  67.                     'message' => $message,
  68.                     'file' => $file,
  69.                     'line' => $line,
  70.                     'trace' => $backtrace,
  71.                     'count' => 1,
  72.                 ];
  73.                 return null;
  74.             });
  75.         }
  76.         $preload = [];
  77.         try {
  78.             foreach ($this->warmers as $warmer) {
  79.                 if (!$this->optionalsEnabled && $warmer->isOptional()) {
  80.                     continue;
  81.                 }
  82.                 if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) {
  83.                     continue;
  84.                 }
  85.                 $preload[] = array_values((array) $warmer->warmUp($cacheDir));
  86.             }
  87.         } finally {
  88.             if ($collectDeprecations) {
  89.                 restore_error_handler();
  90.                 if (is_file($this->deprecationLogsFilepath)) {
  91.                     $previousLogs unserialize(file_get_contents($this->deprecationLogsFilepath));
  92.                     if (\is_array($previousLogs)) {
  93.                         $collectedLogs array_merge($previousLogs$collectedLogs);
  94.                     }
  95.                 }
  96.                 file_put_contents($this->deprecationLogsFilepathserialize(array_values($collectedLogs)));
  97.             }
  98.         }
  99.         return array_values(array_unique(array_merge([], ...$preload)));
  100.     }
  101.     /**
  102.      * {@inheritdoc}
  103.      */
  104.     public function isOptional(): bool
  105.     {
  106.         return false;
  107.     }
  108. }