Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

<service id="cache_warmer" class="Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate" public="true">
<argument type="tagged" tag="kernel.cache_warmer" />
<argument>%kernel.debug%</argument>
Copy link
Contributor Author

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

<argument>%kernel.cache_dir%/%kernel.container_class%Deprecations.log</argument>
</service>

<service id="cache_clearer" class="Symfony\Component\HttpKernel\CacheClearer\ChainCacheClearer" public="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're talking about the definition of PHPUNIT_COMPOSER_INSTALL it's related to #25413

$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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
    }

Copy link
Member

@nicolas-grekas nicolas-grekas May 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsetting the env var temporarily should be doable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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)));
}
}
}

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.