CacheWarmerAggregate.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 ($this->debug) {
  47. $collectedLogs = [];
  48. $previousHandler = \defined('PHPUNIT_COMPOSER_INSTALL');
  49. $previousHandler = $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;
  56. }
  57. $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
  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, 1 + $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. });
  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 ($this->debug && true !== $previousHandler) {
  87. restore_error_handler();
  88. if (file_exists($this->deprecationLogsFilepath)) {
  89. $previousLogs = unserialize(file_get_contents($this->deprecationLogsFilepath));
  90. $collectedLogs = array_merge($previousLogs, $collectedLogs);
  91. }
  92. file_put_contents($this->deprecationLogsFilepath, serialize(array_values($collectedLogs)));
  93. }
  94. }
  95. }
  96. /**
  97. * Checks whether this warmer is optional or not.
  98. *
  99. * @return bool always false
  100. */
  101. public function isOptional()
  102. {
  103. return false;
  104. }
  105. }