Нет описания

CacheWarmerAggregate.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * @param string $cacheDir The cache directory
  43. */
  44. public function warmUp($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. try {
  76. foreach ($this->warmers as $warmer) {
  77. if (!$this->optionalsEnabled && $warmer->isOptional()) {
  78. continue;
  79. }
  80. if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) {
  81. continue;
  82. }
  83. $warmer->warmUp($cacheDir);
  84. }
  85. } finally {
  86. if ($collectDeprecations) {
  87. restore_error_handler();
  88. if (file_exists($this->deprecationLogsFilepath)) {
  89. $previousLogs = unserialize(file_get_contents($this->deprecationLogsFilepath));
  90. if (\is_array($previousLogs)) {
  91. $collectedLogs = array_merge($previousLogs, $collectedLogs);
  92. }
  93. }
  94. file_put_contents($this->deprecationLogsFilepath, serialize(array_values($collectedLogs)));
  95. }
  96. }
  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. }