21
21
class CacheWarmerAggregate implements CacheWarmerInterface
22
22
{
23
23
private $ warmers ;
24
+ private $ debug ;
25
+ private $ deprecationLogsFilepath ;
24
26
private $ optionalsEnabled = false ;
25
27
private $ onlyOptionalsEnabled = false ;
26
28
27
- public function __construct (iterable $ warmers = array ())
29
+ public function __construct (iterable $ warmers = array (), bool $ debug = false , string $ deprecationLogsFilepath = null )
28
30
{
29
31
$ this ->warmers = $ warmers ;
32
+ $ this ->debug = $ debug ;
33
+ $ this ->deprecationLogsFilepath = $ deprecationLogsFilepath ;
30
34
}
31
35
32
36
public function enableOptionalWarmers ()
@@ -46,15 +50,62 @@ public function enableOnlyOptionalWarmers()
46
50
*/
47
51
public function warmUp ($ cacheDir )
48
52
{
49
- foreach ($ this ->warmers as $ warmer ) {
50
- if (!$ this ->optionalsEnabled && $ warmer ->isOptional ()) {
51
- continue ;
52
- }
53
- if ($ this ->onlyOptionalsEnabled && !$ warmer ->isOptional ()) {
54
- continue ;
53
+ if ($ this ->debug ) {
54
+ $ collectedLogs = array ();
55
+ $ previousHandler = defined ('PHPUNIT_COMPOSER_INSTALL ' );
56
+ $ previousHandler = $ previousHandler ?: set_error_handler (function ($ type , $ message , $ file , $ line ) use (&$ collectedLogs , &$ previousHandler ) {
57
+ if (E_USER_DEPRECATED !== $ type && E_DEPRECATED !== $ type ) {
58
+ return $ previousHandler ? $ previousHandler ($ type , $ message , $ file , $ line ) : false ;
59
+ }
60
+
61
+ if (isset ($ collectedLogs [$ message ])) {
62
+ ++$ collectedLogs [$ message ]['count ' ];
63
+
64
+ return ;
65
+ }
66
+
67
+ $ backtrace = debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS , 3 );
68
+ // Clean the trace by removing first frames added by the error handler itself.
69
+ for ($ i = 0 ; isset ($ backtrace [$ i ]); ++$ i ) {
70
+ if (isset ($ backtrace [$ i ]['file ' ], $ backtrace [$ i ]['line ' ]) && $ backtrace [$ i ]['line ' ] === $ line && $ backtrace [$ i ]['file ' ] === $ file ) {
71
+ $ backtrace = array_slice ($ backtrace , 1 + $ i );
72
+ break ;
73
+ }
74
+ }
75
+
76
+ $ collectedLogs [$ message ] = array (
77
+ 'type ' => $ type ,
78
+ 'message ' => $ message ,
79
+ 'file ' => $ file ,
80
+ 'line ' => $ line ,
81
+ 'trace ' => $ backtrace ,
82
+ 'count ' => 1 ,
83
+ );
84
+ });
85
+ }
86
+
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
+ $ warmer ->warmUp ($ cacheDir );
55
97
}
98
+ } finally {
99
+ if ($ this ->debug && true !== $ previousHandler ) {
100
+ restore_error_handler ();
56
101
57
- $ warmer ->warmUp ($ cacheDir );
102
+ if (file_exists ($ this ->deprecationLogsFilepath )) {
103
+ $ previousLogs = unserialize (file_get_contents ($ this ->deprecationLogsFilepath ));
104
+ $ collectedLogs = array_merge ($ previousLogs , $ collectedLogs );
105
+ }
106
+
107
+ file_put_contents ($ this ->deprecationLogsFilepath , serialize (array_values ($ collectedLogs )));
108
+ }
58
109
}
59
110
}
60
111
0 commit comments