CacheWarmerAggregate.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. public function warmUp(string $cacheDir)
  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_ARGS, 3);
  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, 1 + $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. try {
  74. foreach ($this->warmers as $warmer) {
  75. if (!$this->optionalsEnabled && $warmer->isOptional()) {
  76. continue;
  77. }
  78. if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) {
  79. continue;
  80. }
  81. $warmer->warmUp($cacheDir);
  82. }
  83. } finally {
  84. if ($collectDeprecations) {
  85. restore_error_handler();
  86. if (file_exists($this->deprecationLogsFilepath)) {
  87. $previousLogs = unserialize(file_get_contents($this->deprecationLogsFilepath));
  88. $collectedLogs = array_merge($previousLogs, $collectedLogs);
  89. }
  90. file_put_contents($this->deprecationLogsFilepath, serialize(array_values($collectedLogs)));
  91. }
  92. }
  93. }
  94. /**
  95. * Checks whether this warmer is optional or not.
  96. *
  97. * @return bool always false
  98. */
  99. public function isOptional(): bool
  100. {
  101. return false;
  102. }
  103. }