-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
CacheWarmerAggregate handle deprecations logs #27421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,16 @@ | |
class CacheWarmerAggregate implements CacheWarmerInterface | ||
{ | ||
private $warmers; | ||
private $debug; | ||
private $deprecationLogsFilepath; | ||
private $optionalsEnabled = false; | ||
private $onlyOptionalsEnabled = false; | ||
|
||
public function __construct(iterable $warmers = array()) | ||
public function __construct(iterable $warmers = array(), bool $debug = false, string $deprecationLogsFilepath = null) | ||
{ | ||
$this->warmers = $warmers; | ||
$this->debug = $debug; | ||
$this->deprecationLogsFilepath = $deprecationLogsFilepath; | ||
} | ||
|
||
public function enableOptionalWarmers() | ||
|
@@ -46,15 +50,62 @@ public function enableOnlyOptionalWarmers() | |
*/ | ||
public function warmUp($cacheDir) | ||
{ | ||
foreach ($this->warmers as $warmer) { | ||
if (!$this->optionalsEnabled && $warmer->isOptional()) { | ||
continue; | ||
} | ||
if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) { | ||
continue; | ||
if ($this->debug) { | ||
$collectedLogs = array(); | ||
$previousHandler = defined('PHPUNIT_COMPOSER_INSTALL'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're talking about the definition of |
||
$previousHandler = $previousHandler ?: set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) { | ||
if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) { | ||
return $previousHandler ? $previousHandler($type, $message, $file, $line) : false; | ||
} | ||
|
||
if (isset($collectedLogs[$message])) { | ||
++$collectedLogs[$message]['count']; | ||
|
||
return; | ||
} | ||
|
||
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); | ||
// Clean the trace by removing first frames added by the error handler itself. | ||
for ($i = 0; isset($backtrace[$i]); ++$i) { | ||
if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) { | ||
$backtrace = array_slice($backtrace, 1 + $i); | ||
break; | ||
} | ||
} | ||
|
||
$collectedLogs[$message] = array( | ||
'type' => $type, | ||
'message' => $message, | ||
'file' => $file, | ||
'line' => $line, | ||
'trace' => $backtrace, | ||
'count' => 1, | ||
); | ||
}); | ||
} | ||
|
||
try { | ||
foreach ($this->warmers as $warmer) { | ||
if (!$this->optionalsEnabled && $warmer->isOptional()) { | ||
continue; | ||
} | ||
if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) { | ||
continue; | ||
} | ||
|
||
$warmer->warmUp($cacheDir); | ||
} | ||
} finally { | ||
if ($this->debug && true !== $previousHandler) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've also prepare a test scenario with a cacheWarmer who throw deprecations but here I'm directly in conflict with #25413 (fixing #24767). It can't pass in this condition during the test and it's right. Here was my test code (CacheWarmerAggregateTest): public function testWarmupWithDeprecationLogs()
{
$cacheDir = self::$cacheDir;
$warmer = $this->getCacheWarmerMock();
$warmer
->expects($this->once())
->method('warmUp')
->with($cacheDir)
->will($this->returnValue($this->throwDeprecation()));
;
$deprecationLogsFilepath = self::$cacheDir.'/Deprecations.log';
$aggregate = new CacheWarmerAggregate(array($warmer), true, $deprecationLogsFilepath);
$aggregate->warmUp($cacheDir);
$this->assertTrue(file_exists($deprecationLogsFilepath));
$this->assertCount(1, unserialize(file_get_contents($deprecationLogsFilepath)));
}
protected function throwDeprecation()
{
@trigger_error('Deprecation Message', E_USER_DEPRECATED);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsetting the env var temporarily should be doable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've try many solution, and even temporarily removing the defined constant (runkit_constant_remove), deprecations have a different behavior during phpunit test. Still looking to find a solution and test it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, let's forget about it :): |
||
restore_error_handler(); | ||
|
||
$warmer->warmUp($cacheDir); | ||
if (file_exists($this->deprecationLogsFilepath)) { | ||
$previousLogs = unserialize(file_get_contents($this->deprecationLogsFilepath)); | ||
$collectedLogs = array_merge($previousLogs, $collectedLogs); | ||
} | ||
|
||
file_put_contents($this->deprecationLogsFilepath, serialize(array_values($collectedLogs))); | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CacheWarmerAggregate
is annoted as final, so it shouldn't be BCBreak