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