You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.9 KiB

3 years ago
  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. public function __construct(iterable $warmers = [], bool $debug = false, string $deprecationLogsFilepath = null)
  26. {
  27. $this->warmers = $warmers;
  28. $this->debug = $debug;
  29. $this->deprecationLogsFilepath = $deprecationLogsFilepath;
  30. }
  31. public function enableOptionalWarmers()
  32. {
  33. $this->optionalsEnabled = true;
  34. }
  35. public function enableOnlyOptionalWarmers()
  36. {
  37. $this->onlyOptionalsEnabled = $this->optionalsEnabled = true;
  38. }
  39. /**
  40. * Warms up the cache.
  41. *
  42. * @return string[] A list of classes or files to preload on PHP 7.4+
  43. */
  44. public function warmUp(string $cacheDir)
  45. {
  46. if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
  47. $collectedLogs = [];
  48. $previousHandler = set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) {
  49. if (\E_USER_DEPRECATED !== $type && \E_DEPRECATED !== $type) {
  50. return $previousHandler ? $previousHandler($type, $message, $file, $line) : false;
  51. }
  52. if (isset($collectedLogs[$message])) {
  53. ++$collectedLogs[$message]['count'];
  54. return null;
  55. }
  56. $backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 3);
  57. // Clean the trace by removing first frames added by the error handler itself.
  58. for ($i = 0; isset($backtrace[$i]); ++$i) {
  59. if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
  60. $backtrace = \array_slice($backtrace, 1 + $i);
  61. break;
  62. }
  63. }
  64. $collectedLogs[$message] = [
  65. 'type' => $type,
  66. 'message' => $message,
  67. 'file' => $file,
  68. 'line' => $line,
  69. 'trace' => $backtrace,
  70. 'count' => 1,
  71. ];
  72. return null;
  73. });
  74. }
  75. $preload = [];
  76. try {
  77. foreach ($this->warmers as $warmer) {
  78. if (!$this->optionalsEnabled && $warmer->isOptional()) {
  79. continue;
  80. }
  81. if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) {
  82. continue;
  83. }
  84. $preload[] = array_values((array) $warmer->warmUp($cacheDir));
  85. }
  86. } finally {
  87. if ($collectDeprecations) {
  88. restore_error_handler();
  89. if (is_file($this->deprecationLogsFilepath)) {
  90. $previousLogs = unserialize(file_get_contents($this->deprecationLogsFilepath));
  91. $collectedLogs = array_merge($previousLogs, $collectedLogs);
  92. }
  93. file_put_contents($this->deprecationLogsFilepath, serialize(array_values($collectedLogs)));
  94. }
  95. }
  96. return array_values(array_unique(array_merge([], ...$preload)));
  97. }
  98. /**
  99. * Checks whether this warmer is optional or not.
  100. *
  101. * @return bool always false
  102. */
  103. public function isOptional(): bool
  104. {
  105. return false;
  106. }
  107. }