| 148 |
lars |
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 |
|
|
|
12 |
namespace Symfony\Component\HttpKernel\CacheWarmer;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* Aggregates several cache warmers into a single one.
|
|
|
16 |
*
|
|
|
17 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
18 |
*
|
|
|
19 |
* @final
|
|
|
20 |
*/
|
|
|
21 |
class CacheWarmerAggregate implements CacheWarmerInterface
|
|
|
22 |
{
|
|
|
23 |
private iterable $warmers;
|
|
|
24 |
private bool $debug;
|
|
|
25 |
private ?string $deprecationLogsFilepath;
|
|
|
26 |
private bool $optionalsEnabled = false;
|
|
|
27 |
private bool $onlyOptionalsEnabled = false;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* @param iterable<mixed, CacheWarmerInterface> $warmers
|
|
|
31 |
*/
|
|
|
32 |
public function __construct(iterable $warmers = [], bool $debug = false, string $deprecationLogsFilepath = null)
|
|
|
33 |
{
|
|
|
34 |
$this->warmers = $warmers;
|
|
|
35 |
$this->debug = $debug;
|
|
|
36 |
$this->deprecationLogsFilepath = $deprecationLogsFilepath;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
public function enableOptionalWarmers()
|
|
|
40 |
{
|
|
|
41 |
$this->optionalsEnabled = true;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
public function enableOnlyOptionalWarmers()
|
|
|
45 |
{
|
|
|
46 |
$this->onlyOptionalsEnabled = $this->optionalsEnabled = true;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public function warmUp(string $cacheDir): array
|
|
|
50 |
{
|
|
|
51 |
if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
|
|
|
52 |
$collectedLogs = [];
|
|
|
53 |
$previousHandler = set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) {
|
|
|
54 |
if (\E_USER_DEPRECATED !== $type && \E_DEPRECATED !== $type) {
|
|
|
55 |
return $previousHandler ? $previousHandler($type, $message, $file, $line) : false;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
if (isset($collectedLogs[$message])) {
|
|
|
59 |
++$collectedLogs[$message]['count'];
|
|
|
60 |
|
|
|
61 |
return null;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 3);
|
|
|
65 |
// Clean the trace by removing first frames added by the error handler itself.
|
|
|
66 |
for ($i = 0; isset($backtrace[$i]); ++$i) {
|
|
|
67 |
if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
|
|
|
68 |
$backtrace = \array_slice($backtrace, 1 + $i);
|
|
|
69 |
break;
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
$collectedLogs[$message] = [
|
|
|
74 |
'type' => $type,
|
|
|
75 |
'message' => $message,
|
|
|
76 |
'file' => $file,
|
|
|
77 |
'line' => $line,
|
|
|
78 |
'trace' => $backtrace,
|
|
|
79 |
'count' => 1,
|
|
|
80 |
];
|
|
|
81 |
|
|
|
82 |
return null;
|
|
|
83 |
});
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$preload = [];
|
|
|
87 |
try {
|
|
|
88 |
foreach ($this->warmers as $warmer) {
|
|
|
89 |
if (!$this->optionalsEnabled && $warmer->isOptional()) {
|
|
|
90 |
continue;
|
|
|
91 |
}
|
|
|
92 |
if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) {
|
|
|
93 |
continue;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$preload[] = array_values((array) $warmer->warmUp($cacheDir));
|
|
|
97 |
}
|
|
|
98 |
} finally {
|
|
|
99 |
if ($collectDeprecations) {
|
|
|
100 |
restore_error_handler();
|
|
|
101 |
|
|
|
102 |
if (is_file($this->deprecationLogsFilepath)) {
|
|
|
103 |
$previousLogs = unserialize(file_get_contents($this->deprecationLogsFilepath));
|
|
|
104 |
if (\is_array($previousLogs)) {
|
|
|
105 |
$collectedLogs = array_merge($previousLogs, $collectedLogs);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
file_put_contents($this->deprecationLogsFilepath, serialize(array_values($collectedLogs)));
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
return array_values(array_unique(array_merge([], ...$preload)));
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
public function isOptional(): bool
|
|
|
117 |
{
|
|
|
118 |
return false;
|
|
|
119 |
}
|
|
|
120 |
}
|