[WebProfilerBundle] Some eye candy for deprecated calls

This commit is contained in:
Victor Berchet 2012-12-07 12:47:14 +01:00 committed by Fabien Potencier
parent 0c6e145c0d
commit 7428bf9aa4
7 changed files with 102 additions and 30 deletions

View File

@ -1,16 +1,18 @@
{% extends '@WebProfiler/Profiler/layout.html.twig' %}
{% import _self as logger %}
{% block toolbar %}
{% if collector.counterrors or collector.countdeprecations %}
{% set icon %}
<img width="15" height="28" alt="Logs" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAcCAYAAABoMT8aAAAA4klEQVQ4y2P4//8/AyWYYXgYwOPp6Xnc3t7+P7EYpB6k7+zZs2ADNEjRjIwDAgKWgAywIUfz8+fPVzg7O/8AGeCATQEQnAfi/SAah/wcV1dXvAYUgORANA75ehcXl+/4DHAABRIe+ZrhbgAhTHsDiEgHBA0glA6GfSDiw5mZma+A+sphBlhVVFQ88vHx+Xfu3Ll7QP5haOjjwtuAuGHv3r3NIMNABqh8+/atsaur666vr+9XUlwSHx//AGQANxCbAnEWyGQicRMQ9wBxIQM0qjiBWAFqkB00/glhayBWHwb1AgB38EJsUtxtWwAAAABJRU5ErkJggg=="/>
{% if collector.counterrors %}
{% set statusColor = "red" %}
{% set status_color = "red" %}
{% else %}
{% set statusColor = "yellow" %}
{% set status_color = "yellow" %}
{% endif %}
{% set errorCount = collector.counterrors + collector.countdeprecations %}
<span class="sf-toolbar-status sf-toolbar-status-{{ statusColor }}">{{ errorCount }}</span>
{% set error_count = collector.counterrors + collector.countdeprecations %}
<span class="sf-toolbar-status sf-toolbar-status-{{ status_color }}">{{ error_count }}</span>
{% endset %}
{% set text %}
{% if collector.counterrors %}
@ -35,9 +37,9 @@
<span class="icon"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAgCAYAAAAMq2gFAAABjElEQVRIx2MIDw+vd3R0/GFvb/+fGtjFxeVJSUmJ1f///5nv37/PAMMMzs7OVLMEhoODgy/k5+cHJCYmagAtZAJbRG1L0DEwxCYALeOgiUXbt2+/X1NT8xTEdnd3/wi0SI4mFgHBDCBeCLXoF5BtwkCEpvNAvB8JnydCTwgQR0It+g1kWxNjUQEQOyDhAiL0gNUiWWRDjEUOyMkUZsCoRaMWjVpEvEVkFkGjFmEUqgc+fvx4hVYWIReqzi9evKileaoDslnu3LkTNLQtGk3edLPIycnpL9Bge5pb1NXVdQNosDmGRcAm7F+QgKur6783b95cBQoeRGv1kII3QPOdAoZF8+fPP4PUqnx55syZVKCEI1rLh1hsAbWEZ8aMGaUoFoFcMG3atKdIjfSPISEhawICAlaQgwMDA1f6+/sfB5rzE2Sej4/PD3C7DkjoAHHVoUOHLpSVlX3w8vL6Sa34Alr6Z8WKFaCoMARZxAHEoFZ/HBD3A/FyIF4BxMvIxCC964F4G6hZDMTxQCwJAGWE8pur5kFDAAAAAElFTkSuQmCC" alt="Logger" /></span>
<strong>Logs</strong>
{% if collector.counterrors or collector.countdeprecations %}
{% set errorCount = collector.counterrors + collector.countdeprecations %}
{% set error_count = collector.counterrors + collector.countdeprecations %}
<span class="count">
<span>{{ errorCount }}</span>
<span>{{ error_count }}</span>
</span>
{% endif %}
</span>
@ -56,7 +58,19 @@
<input type="hidden" name="panel" value="logger" />
<label for="priority">Priority</label>
<select id="priority" name="priority" onchange="document.getElementById('priority-form').submit(); ">
{% for value, text in { 100: 'DEBUG', 200: 'INFO', 250: 'NOTICE', 300: 'WARNING', 400: 'ERROR', 500: 'CRITICAL', 550: 'ALERT', 600: 'EMERGENCY' } %}
{# values < 0 are custom levels #}
{% for value, text in {
100: 'DEBUG',
200: 'INFO',
250: 'NOTICE',
300: 'WARNING',
400: 'ERROR',
500: 'CRITICAL',
550: 'ALERT',
600: 'EMERGENCY',
'-100': 'DEPRECATION only'
}
%}
<option value="{{ value }}"{{ value == priority ? ' selected' : '' }}>{{ text }}</option>
{% endfor %}
</select>
@ -70,15 +84,9 @@
{% if collector.logs %}
<ul class="alt">
{% for log in collector.logs if log.priority >= priority %}
{% for log in collector.logs if priority >= 0 and log.priority >= priority or priority < 0 and log.context.type|default(0) == priority %}
<li class="{{ cycle(['odd', 'even'], loop.index) }}{% if log.priority >= 400 %} error{% elseif log.priority >= 300 %} warning{% endif %}">
{{ log.priorityName }} - {{ log.message }}
{% if log.context is defined and log.context is not empty %}
<br />
<small>
<strong>Context</strong>: {{ log.context|yaml_encode }}
</small>
{% endif %}
{{ logger.display_message(loop.index, log) }}
</li>
{% else %}
<li><em>No logs available for this priority.</em></li>
@ -90,3 +98,39 @@
</p>
{% endif %}
{% endblock %}
{% macro display_message(log_index, log) %}
{% if constant('Symfony\\Component\\HttpKernel\\Debug\\ErrorHandler::TYPE_DEPRECATION') == log.context.type|default(0) %}
DEPRECATION - Deprecated call in {{ log.context.file|format_file(log.context.line) }}.
{% set id = 'sf-call-stack-' ~ log_index %}
<a href="#" onclick="Sfjs.toggle('{{ id }}', document.getElementById('{{ id }}-on'), document.getElementById('{{ id }}-off')); return false;">
<img class="toggle" id="{{ id }}-off" alt="-" src="data:image/gif;base64,R0lGODlhEgASAMQSANft94TG57Hb8GS44ez1+mC24IvK6ePx+Wa44dXs92+942e54o3L6W2844/M6dnu+P/+/l614P///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABIALAAAAAASABIAQAVCoCQBTBOd6Kk4gJhGBCTPxysJb44K0qD/ER/wlxjmisZkMqBEBW5NHrMZmVKvv9hMVsO+hE0EoNAstEYGxG9heIhCADs=" style="display:none" />
<img class="toggle" id="{{ id }}-on" alt="+" src="data:image/gif;base64,R0lGODlhEgASAMQTANft99/v+Ga44bHb8ITG52S44dXs9+z1+uPx+YvK6WC24G+944/M6W28443L6dnu+Ge54v/+/l614P///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABMALAAAAAASABIAQAVS4DQBTiOd6LkwgJgeUSzHSDoNaZ4PU6FLgYBA5/vFID/DbylRGiNIZu74I0h1hNsVxbNuUV4d9SsZM2EzWe1qThVzwWFOAFCQFa1RQq6DJB4iIQA7" style="display:inline" />
</a>
{% for index, call in log.context.stack if index > 0 %}
{% if index == 1 %}
<ul class="sf-call-stack" id="{{ id }}" style="display: none">
{% endif %}
{% if call.class is defined %}
{% set from = call.class|abbr_class ~ '::' ~ call.function|abbr_method() %}
{% elseif call.function is defined %}
{% set from = call.function|abbr_method %}
{% elseif call.file is defined %}
{% set from = call.file %}
{% else %}
{% set from = '-' %}
{% endif %}
<li>Called from {{ call.file is defined and call.line is defined
? call.file|format_file(call.line, from)
: from|raw
}}
</li>
{{ index == log.context.stack|length - 1 ? '</ul>' : '' }}
{% endfor %}
{% else %}
{{ log.priorityName }} - {{ log.message }}
{% endif %}
{% endmacro %}

View File

@ -273,6 +273,10 @@ ul.alt li.warning {
background-color: #ffcc00;
margin-bottom: 1px;
}
ul.sf-call-stack li {
text-size: small;
padding: 0 0 0 20px;
}
td.main, td.menu {
text-align: left;
margin: 0;

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Debug\ErrorHandler;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
@ -114,7 +115,7 @@ class LoggerDataCollector extends DataCollector
{
$count = 0;
foreach ($this->logger->getLogs() as $log) {
if (isset($log['context']['type']) && 'deprecation' === $log['context']['type']) {
if (isset($log['context']['type']) && ErrorHandler::TYPE_DEPRECATION === $log['context']['type']) {
$count++;
}
}

View File

@ -21,6 +21,8 @@ use Symfony\Component\HttpKernel\Log\LoggerInterface;
*/
class ErrorHandler
{
const TYPE_DEPRECATION = -100;
private $levels = array(
E_WARNING => 'Warning',
E_NOTICE => 'Notice',
@ -82,9 +84,18 @@ class ErrorHandler
return false;
}
if ($level & E_USER_DEPRECATED || $level & E_DEPRECATED) {
if ($level & (E_USER_DEPRECATED | E_DEPRECATED)) {
if (null !== self::$logger) {
self::$logger->warn($message, array('type' => 'deprecation', 'file' => $file, 'line' => $line));
$deprecation = array(
'type' => self::TYPE_DEPRECATION,
'file' => $file,
'line' => $line,
'stack' => version_compare(PHP_VERSION, '5.4', '<')
? array_slice(debug_backtrace(false), 0, 10)
: debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10)
);
self::$logger->warn($message, $deprecation);
}
return true;

View File

@ -91,7 +91,6 @@ class ExceptionHandler
*/
public function getContent(FlattenException $exception)
{
$title = '';
switch ($exception->getStatusCode()) {
case 404:
$title = 'Sorry, the page you are looking for could not be found.';
@ -103,13 +102,10 @@ class ExceptionHandler
$content = '';
if ($this->debug) {
try {
$message = nl2br($exception->getMessage());
$class = $this->abbrClass($exception->getClass());
$count = count($exception->getAllPrevious());
$content = '';
$total = $count + 1;
foreach ($exception->toArray() as $position => $e) {
$ind = $count - $position + 1;
$total = $count + 1;
$class = $this->abbrClass($e['class']);
$message = nl2br($e['message']);
$content .= sprintf(<<<EOF
@ -121,7 +117,7 @@ class ExceptionHandler
EOF
, $ind, $total, $class, $message);
foreach ($e['trace'] as $i => $trace) {
foreach ($e['trace'] as $trace) {
$content .= ' <li>';
if ($trace['function']) {
$content .= sprintf('at %s%s%s(%s)', $this->abbrClass($trace['class']), $trace['type'], $trace['function'], $this->formatArgs($trace['args']));

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector;
use Symfony\Component\HttpKernel\Debug\ErrorHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -66,8 +67,8 @@ class LoggerDataCollectorTest extends \PHPUnit_Framework_TestCase
array(
1,
array(
array('message' => 'foo', 'context' => array('type' => 'deprecation')),
array('message' => 'foo2', 'context' => array('type' => 'deprecation'))
array('message' => 'foo', 'context' => array('type' => ErrorHandler::TYPE_DEPRECATION)),
array('message' => 'foo2', 'context' => array('type' => ErrorHandler::TYPE_DEPRECATION))
),
null,
2

View File

@ -68,10 +68,25 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
restore_error_handler();
$logger = $this->getMock('Symfony\Component\HttpKernel\Log\LoggerInterface');
$logger->expects($this->once())->method('warn')->with(
$this->equalTo('foo'),
$this->equalTo(array('type' => 'deprecation', 'file' => 'foo.php', 'line' => '12'))
);
$that = $this;
$warnArgCheck = function($message, $context) use ($that) {
$that->assertEquals('foo', $message);
$that->assertArrayHasKey('file', $context);
$that->assertEquals($context['file'], 'foo.php');
$that->assertArrayHasKey('line', $context);
$that->assertEquals($context['line'], 12);
$that->assertArrayHasKey('type', $context);
$that->assertEquals($context['type'], ErrorHandler::TYPE_DEPRECATION);
$that->assertArrayHasKey('stack', $context);
$that->assertInternalType('array', $context['stack']);
};
$logger
->expects($this->once())
->method('warn')
->will($this->returnCallback($warnArgCheck))
;
$handler = ErrorHandler::register(E_USER_DEPRECATED);
$handler->setLogger($logger);