mirror of
https://github.com/symfony/debug.git
synced 2026-03-24 17:22:13 +01:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca764f8af9 | ||
|
|
9fb191fa62 | ||
|
|
b2e922174d | ||
|
|
bdfb718a6c | ||
|
|
3c1b5f218d | ||
|
|
78dc94dce6 | ||
|
|
4885c636af | ||
|
|
6a55635898 | ||
|
|
7f875d2aae | ||
|
|
13d5fe432f | ||
|
|
89151cc3fe | ||
|
|
93f26330c7 | ||
|
|
b4ea949b41 | ||
|
|
264eb40f8d | ||
|
|
ff1f1fc483 | ||
|
|
5da7e8c937 | ||
|
|
085d4fd990 | ||
|
|
7f671456b9 | ||
|
|
234f31cd20 | ||
|
|
729f6d19cf | ||
|
|
947e8d434b |
@@ -30,8 +30,8 @@ class Debug
|
||||
* If the Symfony ClassLoader component is available, a special
|
||||
* class loader is also registered.
|
||||
*
|
||||
* @param integer $errorReportingLevel The level of error reporting you want
|
||||
* @param Boolean $displayErrors Whether to display errors (for development) or just log them (for production)
|
||||
* @param int $errorReportingLevel The level of error reporting you want
|
||||
* @param bool $displayErrors Whether to display errors (for development) or just log them (for production)
|
||||
*/
|
||||
public static function enable($errorReportingLevel = null, $displayErrors = true)
|
||||
{
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Symfony\Component\Debug;
|
||||
|
||||
use Symfony\Component\Debug\Exception\FatalErrorException;
|
||||
use Symfony\Component\Debug\Exception\ContextErrorException;
|
||||
use Symfony\Component\Debug\Exception\DummyException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
@@ -55,10 +56,10 @@ class ErrorHandler
|
||||
/**
|
||||
* Registers the error handler.
|
||||
*
|
||||
* @param integer $level The level at which the conversion to Exception is done (null to use the error_reporting() value and 0 to disable)
|
||||
* @param Boolean $displayErrors Display errors (for dev environment) or just log they (production usage)
|
||||
* @param int $level The level at which the conversion to Exception is done (null to use the error_reporting() value and 0 to disable)
|
||||
* @param bool $displayErrors Display errors (for dev environment) or just log they (production usage)
|
||||
*
|
||||
* @return The registered error handler
|
||||
* @return ErrorHandler The registered error handler
|
||||
*/
|
||||
public static function register($level = null, $displayErrors = true)
|
||||
{
|
||||
@@ -104,6 +105,7 @@ class ErrorHandler
|
||||
$stack = array_map(
|
||||
function ($row) {
|
||||
unset($row['args']);
|
||||
|
||||
return $row;
|
||||
},
|
||||
array_slice(debug_backtrace(false), 0, 10)
|
||||
@@ -119,7 +121,46 @@ class ErrorHandler
|
||||
}
|
||||
|
||||
if ($this->displayErrors && error_reporting() & $level && $this->level & $level) {
|
||||
throw new ContextErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line), 0, $level, $file, $line, $context);
|
||||
// make sure the ContextErrorException class is loaded (https://bugs.php.net/bug.php?id=65322)
|
||||
if (!class_exists('Symfony\Component\Debug\Exception\ContextErrorException')) {
|
||||
require __DIR__.'/Exception/ContextErrorException.php';
|
||||
}
|
||||
if (!class_exists('Symfony\Component\Debug\Exception\FlattenException')) {
|
||||
require __DIR__.'/Exception/FlattenException.php';
|
||||
}
|
||||
|
||||
if (PHP_VERSION_ID < 50400 && isset($context['GLOBALS']) && is_array($context)) {
|
||||
unset($context['GLOBALS']);
|
||||
}
|
||||
|
||||
$exception = new ContextErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line), 0, $level, $file, $line, $context);
|
||||
|
||||
// Exceptions thrown from error handlers are sometimes not caught by the exception
|
||||
// handler, so we invoke it directly (https://bugs.php.net/bug.php?id=54275)
|
||||
$exceptionHandler = set_exception_handler(function () {});
|
||||
restore_exception_handler();
|
||||
|
||||
if (is_array($exceptionHandler) && $exceptionHandler[0] instanceof ExceptionHandler) {
|
||||
$exceptionHandler[0]->handle($exception);
|
||||
|
||||
if (!class_exists('Symfony\Component\Debug\Exception\DummyException')) {
|
||||
require __DIR__.'/Exception/DummyException.php';
|
||||
}
|
||||
|
||||
// we must stop the PHP script execution, as the exception has
|
||||
// already been dealt with, so, let's throw an exception that
|
||||
// will be caught by a dummy exception handler
|
||||
set_exception_handler(function (\Exception $e) use ($exceptionHandler) {
|
||||
if (!$e instanceof DummyException) {
|
||||
// happens if our dummy exception is caught by a
|
||||
// catch-all from user code, in which case, let's the
|
||||
// current handler handle this "new" exception
|
||||
call_user_func($exceptionHandler, $e);
|
||||
}
|
||||
});
|
||||
|
||||
throw new DummyException();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -131,7 +172,7 @@ class ErrorHandler
|
||||
return;
|
||||
}
|
||||
|
||||
unset($this->reservedMemory);
|
||||
$this->reservedMemory = '';
|
||||
$type = $error['type'];
|
||||
if (0 === $this->level || !in_array($type, array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) {
|
||||
return;
|
||||
@@ -152,7 +193,7 @@ class ErrorHandler
|
||||
}
|
||||
|
||||
// get current exception handler
|
||||
$exceptionHandler = set_exception_handler(function() {});
|
||||
$exceptionHandler = set_exception_handler(function () {});
|
||||
restore_exception_handler();
|
||||
|
||||
if (is_array($exceptionHandler) && $exceptionHandler[0] instanceof ExceptionHandler) {
|
||||
|
||||
@@ -19,18 +19,18 @@ namespace Symfony\Component\Debug\Exception;
|
||||
class ContextErrorException extends \ErrorException
|
||||
{
|
||||
private $context = array();
|
||||
|
||||
|
||||
public function __construct($message, $code, $severity, $filename, $lineno, $context = array())
|
||||
{
|
||||
parent::__construct($message, $code, $severity, $filename, $lineno);
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array Array of variables that existed when the exception occured
|
||||
* @return array Array of variables that existed when the exception occurred
|
||||
*/
|
||||
public function getContext()
|
||||
{
|
||||
return $this->context;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
Exception/DummyException.php
Normal file
21
Exception/DummyException.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Debug\Exception;
|
||||
|
||||
/**
|
||||
* Used to stop execution of a PHP script after handling a fatal error.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class DummyException extends \ErrorException
|
||||
{
|
||||
}
|
||||
@@ -249,7 +249,7 @@ class FlattenException
|
||||
if ($level > 10) {
|
||||
$result[$key] = array('array', '*DEEP NESTED ARRAY*');
|
||||
} else {
|
||||
$result[$key] = array('array', $this->flattenArgs($value, ++$level));
|
||||
$result[$key] = array('array', $this->flattenArgs($value, $level + 1));
|
||||
}
|
||||
} elseif (null === $value) {
|
||||
$result[$key] = array('null', null);
|
||||
|
||||
@@ -43,7 +43,7 @@ class ExceptionHandler
|
||||
/**
|
||||
* Registers the exception handler.
|
||||
*
|
||||
* @param Boolean $debug
|
||||
* @param bool $debug
|
||||
*
|
||||
* @return ExceptionHandler The registered exception handler
|
||||
*/
|
||||
@@ -91,9 +91,11 @@ class ExceptionHandler
|
||||
$exception = FlattenException::create($exception);
|
||||
}
|
||||
|
||||
header(sprintf('HTTP/1.0 %s', $exception->getStatusCode()));
|
||||
foreach ($exception->getHeaders() as $name => $value) {
|
||||
header($name.': '.$value, false);
|
||||
if (!headers_sent()) {
|
||||
header(sprintf('HTTP/1.0 %s', $exception->getStatusCode()));
|
||||
foreach ($exception->getHeaders() as $name => $value) {
|
||||
header($name.': '.$value, false);
|
||||
}
|
||||
}
|
||||
|
||||
echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception));
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2004-2013 Fabien Potencier
|
||||
Copyright (c) 2004-2014 Fabien Potencier
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\Debug\Tests;
|
||||
|
||||
use Symfony\Component\Debug\ErrorHandler;
|
||||
use Symfony\Component\Debug\Exception\DummyException;
|
||||
|
||||
/**
|
||||
* ErrorHandlerTest
|
||||
@@ -20,6 +21,134 @@ use Symfony\Component\Debug\ErrorHandler;
|
||||
*/
|
||||
class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var int Error reporting level before running tests.
|
||||
*/
|
||||
protected $errorReporting;
|
||||
|
||||
/**
|
||||
* @var string Display errors setting before running tests.
|
||||
*/
|
||||
protected $displayErrors;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->errorReporting = error_reporting(E_ALL | E_STRICT);
|
||||
$this->displayErrors = ini_get('display_errors');
|
||||
ini_set('display_errors', '1');
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
ini_set('display_errors', $this->displayErrors);
|
||||
error_reporting($this->errorReporting);
|
||||
}
|
||||
|
||||
public function testCompileTimeError()
|
||||
{
|
||||
// the ContextErrorException must not be loaded to test the workaround
|
||||
// for https://bugs.php.net/bug.php?id=65322.
|
||||
if (class_exists('Symfony\Component\Debug\Exception\ContextErrorException', false)) {
|
||||
$this->markTestSkipped('The ContextErrorException class is already loaded.');
|
||||
}
|
||||
|
||||
$exceptionHandler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('handle'));
|
||||
|
||||
// the following code forces some PHPUnit classes to be loaded
|
||||
// so that they will be available in the exception handler
|
||||
// as they won't be autoloaded by PHP
|
||||
class_exists('PHPUnit_Framework_MockObject_Invocation_Object');
|
||||
$this->assertInstanceOf('stdClass', new \stdClass());
|
||||
$this->assertEquals(1, 1);
|
||||
$this->assertStringStartsWith('foo', 'foobar');
|
||||
$this->assertArrayHasKey('bar', array('bar' => 'foo'));
|
||||
|
||||
$that = $this;
|
||||
$exceptionCheck = function ($exception) use ($that) {
|
||||
$that->assertInstanceOf('Symfony\Component\Debug\Exception\ContextErrorException', $exception);
|
||||
$that->assertEquals(E_STRICT, $exception->getSeverity());
|
||||
$that->assertEquals(2, $exception->getLine());
|
||||
$that->assertStringStartsWith('Runtime Notice: Declaration of _CompileTimeError::foo() should be compatible with', $exception->getMessage());
|
||||
$that->assertArrayHasKey('bar', $exception->getContext());
|
||||
};
|
||||
|
||||
$exceptionHandler->expects($this->once())
|
||||
->method('handle')
|
||||
->will($this->returnCallback($exceptionCheck))
|
||||
;
|
||||
|
||||
ErrorHandler::register();
|
||||
set_exception_handler(array($exceptionHandler, 'handle'));
|
||||
|
||||
// dummy variable to check for in error handler.
|
||||
$bar = 123;
|
||||
|
||||
// trigger compile time error
|
||||
try {
|
||||
eval(<<<'PHP'
|
||||
class _BaseCompileTimeError { function foo() {} }
|
||||
class _CompileTimeError extends _BaseCompileTimeError { function foo($invalid) {} }
|
||||
PHP
|
||||
);
|
||||
} catch (DummyException $e) {
|
||||
// if an exception is thrown, the test passed
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
restore_exception_handler();
|
||||
}
|
||||
|
||||
public function testNotice()
|
||||
{
|
||||
$exceptionHandler = $this->getMock('Symfony\Component\Debug\ExceptionHandler', array('handle'));
|
||||
set_exception_handler(array($exceptionHandler, 'handle'));
|
||||
|
||||
$that = $this;
|
||||
$exceptionCheck = function ($exception) use ($that) {
|
||||
$that->assertInstanceOf('Symfony\Component\Debug\Exception\ContextErrorException', $exception);
|
||||
$that->assertEquals(E_NOTICE, $exception->getSeverity());
|
||||
$that->assertEquals(__LINE__ + 40, $exception->getLine());
|
||||
$that->assertEquals(__FILE__, $exception->getFile());
|
||||
$that->assertRegexp('/^Notice: Undefined variable: (foo|bar)/', $exception->getMessage());
|
||||
$that->assertArrayHasKey('foobar', $exception->getContext());
|
||||
|
||||
$trace = $exception->getTrace();
|
||||
$that->assertEquals(__FILE__, $trace[0]['file']);
|
||||
$that->assertEquals('Symfony\Component\Debug\ErrorHandler', $trace[0]['class']);
|
||||
$that->assertEquals('handle', $trace[0]['function']);
|
||||
$that->assertEquals('->', $trace[0]['type']);
|
||||
|
||||
$that->assertEquals(__FILE__, $trace[1]['file']);
|
||||
$that->assertEquals(__CLASS__, $trace[1]['class']);
|
||||
$that->assertEquals('triggerNotice', $trace[1]['function']);
|
||||
$that->assertEquals('::', $trace[1]['type']);
|
||||
|
||||
$that->assertEquals(__CLASS__, $trace[2]['class']);
|
||||
$that->assertEquals('testNotice', $trace[2]['function']);
|
||||
$that->assertEquals('->', $trace[2]['type']);
|
||||
};
|
||||
|
||||
$exceptionHandler->expects($this->once())
|
||||
->method('handle')
|
||||
->will($this->returnCallback($exceptionCheck));
|
||||
ErrorHandler::register();
|
||||
|
||||
try {
|
||||
self::triggerNotice($this);
|
||||
} catch (DummyException $e) {
|
||||
// if an exception is thrown, the test passed
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
// dummy function to test trace in error handler.
|
||||
private static function triggerNotice($that)
|
||||
{
|
||||
// dummy variable to check for in error handler.
|
||||
$foobar = 123;
|
||||
$that->assertSame('', $foo.$foo.$bar);
|
||||
}
|
||||
|
||||
public function testConstruct()
|
||||
{
|
||||
@@ -36,18 +165,18 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
public function testHandle()
|
||||
{
|
||||
$handler = ErrorHandler::register(0);
|
||||
$this->assertFalse($handler->handle(0, 'foo', 'foo.php', 12, 'foo'));
|
||||
$this->assertFalse($handler->handle(0, 'foo', 'foo.php', 12, array()));
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
$handler = ErrorHandler::register(3);
|
||||
$this->assertFalse($handler->handle(4, 'foo', 'foo.php', 12, 'foo'));
|
||||
$this->assertFalse($handler->handle(4, 'foo', 'foo.php', 12, array()));
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
$handler = ErrorHandler::register(3);
|
||||
try {
|
||||
$handler->handle(111, 'foo', 'foo.php', 12, 'foo');
|
||||
$handler->handle(111, 'foo', 'foo.php', 12, array());
|
||||
} catch (\ErrorException $e) {
|
||||
$this->assertSame('111: foo in foo.php line 12', $e->getMessage());
|
||||
$this->assertSame(111, $e->getSeverity());
|
||||
@@ -58,19 +187,19 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
restore_error_handler();
|
||||
|
||||
$handler = ErrorHandler::register(E_USER_DEPRECATED);
|
||||
$this->assertTrue($handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, 'foo'));
|
||||
$this->assertTrue($handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array()));
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
$handler = ErrorHandler::register(E_DEPRECATED);
|
||||
$this->assertTrue($handler->handle(E_DEPRECATED, 'foo', 'foo.php', 12, 'foo'));
|
||||
$this->assertTrue($handler->handle(E_DEPRECATED, 'foo', 'foo.php', 12, array()));
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
$logger = $this->getMock('Psr\Log\LoggerInterface');
|
||||
|
||||
$that = $this;
|
||||
$warnArgCheck = function($message, $context) use ($that) {
|
||||
$warnArgCheck = function ($message, $context) use ($that) {
|
||||
$that->assertEquals('foo', $message);
|
||||
$that->assertArrayHasKey('type', $context);
|
||||
$that->assertEquals($context['type'], ErrorHandler::TYPE_DEPRECATION);
|
||||
@@ -86,7 +215,7 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$handler = ErrorHandler::register(E_USER_DEPRECATED);
|
||||
$handler->setLogger($logger);
|
||||
$handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, 'foo');
|
||||
$handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array());
|
||||
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ class FlattenExceptionTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals($exception->getMessage(), $flattened->getMessage(), 'The message is copied from the original exception.');
|
||||
$this->assertEquals($exception->getCode(), $flattened->getCode(), 'The code is copied from the original exception.');
|
||||
$this->assertEquals(get_class($exception), $flattened->getClass(), 'The class is set to the class of the original exception');
|
||||
$this->assertInstanceOf($flattened->getClass(), $exception, 'The class is set to the class of the original exception');
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -64,6 +64,6 @@ class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
public function testNestedExceptions()
|
||||
{
|
||||
$handler = new ExceptionHandler(true);
|
||||
$response = $handler->createResponse(new \RuntimeException('Foo', null, new \RuntimeException('Bar')));
|
||||
$response = $handler->createResponse(new \RuntimeException('Foo', 0, new \RuntimeException('Bar')));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user