-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Debug] added the component (closes #6828, closes #6834, closes #7330) #7441
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2ff0927
[Debug] added the component (closes #6828, closes #6834, closes #7330)
fabpot 2b305c2
added a main Debug class to ease integration
fabpot 946bfb2
[Debug] made the exception handler independant of HttpFoundation
fabpot e455269
[Debug] ensured that the Debug tools can only be registered once
fabpot daa3a3c
[Debug] changed composer to accept more versions
fabpot 1ab1146
[Debug] fixed minor bugs
fabpot f693128
fixed typos
fabpot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
2.3.0 | ||
----- | ||
|
||
* added the component |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Debug; | ||
|
||
use Symfony\Component\ClassLoader\DebugClassLoader; | ||
|
||
/** | ||
* Registers all the debug tools. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
class Debug | ||
{ | ||
private static $enabled = false; | ||
|
||
/** | ||
* Enables the debug tools. | ||
* | ||
* This method registers an error handler and an exception handler. | ||
* | ||
* If the Symfony ClassLoader component is available, a special | ||
* class loader is also registered. | ||
* | ||
* @param integer $errorReportingLevel The level of error reporting you want | ||
*/ | ||
public static function enable($errorReportingLevel = null) | ||
{ | ||
if (static::$enabled) { | ||
return; | ||
} | ||
|
||
static::$enabled = true; | ||
|
||
error_reporting(-1); | ||
|
||
ErrorHandler::register($errorReportingLevel); | ||
if ('cli' !== php_sapi_name()) { | ||
ExceptionHandler::register(); | ||
} elseif (!ini_get('log_errors') || ini_get('error_log')) { | ||
ini_set('display_errors', 1); | ||
} | ||
|
||
if (class_exists('Symfony\Component\ClassLoader\DebugClassLoader')) { | ||
DebugClassLoader::enable(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Debug; | ||
|
||
use Symfony\Component\Debug\Exception\FatalErrorException; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* ErrorHandler. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
class ErrorHandler | ||
{ | ||
const TYPE_DEPRECATION = -100; | ||
|
||
private $levels = array( | ||
E_WARNING => 'Warning', | ||
E_NOTICE => 'Notice', | ||
E_USER_ERROR => 'User Error', | ||
E_USER_WARNING => 'User Warning', | ||
E_USER_NOTICE => 'User Notice', | ||
E_STRICT => 'Runtime Notice', | ||
E_RECOVERABLE_ERROR => 'Catchable Fatal Error', | ||
E_DEPRECATED => 'Deprecated', | ||
E_USER_DEPRECATED => 'User Deprecated', | ||
E_ERROR => 'Error', | ||
E_CORE_ERROR => 'Core Error', | ||
E_COMPILE_ERROR => 'Compile Error', | ||
E_PARSE => 'Parse', | ||
); | ||
|
||
private $level; | ||
|
||
private $reservedMemory; | ||
|
||
/** @var LoggerInterface */ | ||
private static $logger; | ||
|
||
/** | ||
* 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) | ||
* | ||
* @return The registered error handler | ||
*/ | ||
public static function register($level = null) | ||
{ | ||
$handler = new static(); | ||
$handler->setLevel($level); | ||
|
||
ini_set('display_errors', 0); | ||
set_error_handler(array($handler, 'handle')); | ||
register_shutdown_function(array($handler, 'handleFatal')); | ||
$handler->reservedMemory = str_repeat('x', 10240); | ||
|
||
return $handler; | ||
} | ||
|
||
public function setLevel($level) | ||
{ | ||
$this->level = null === $level ? error_reporting() : $level; | ||
} | ||
|
||
public static function setLogger(LoggerInterface $logger) | ||
{ | ||
self::$logger = $logger; | ||
} | ||
|
||
/** | ||
* @throws \ErrorException When error_reporting returns error | ||
*/ | ||
public function handle($level, $message, $file, $line, $context) | ||
{ | ||
if (0 === $this->level) { | ||
return false; | ||
} | ||
|
||
if ($level & (E_USER_DEPRECATED | E_DEPRECATED)) { | ||
if (null !== self::$logger) { | ||
$stack = version_compare(PHP_VERSION, '5.4', '<') ? array_slice(debug_backtrace(false), 0, 10) : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10); | ||
|
||
self::$logger->warning($message, array('type' => self::TYPE_DEPRECATION, 'stack' => $stack)); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
if (error_reporting() & $level && $this->level & $level) { | ||
throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line), 0, $level, $file, $line); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function handleFatal() | ||
{ | ||
if (null === $error = error_get_last()) { | ||
return; | ||
} | ||
|
||
unset($this->reservedMemory); | ||
$type = $error['type']; | ||
if (0 === $this->level || !in_array($type, array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) { | ||
return; | ||
} | ||
|
||
// get current exception handler | ||
$exceptionHandler = set_exception_handler(function() {}); | ||
restore_exception_handler(); | ||
|
||
if (is_array($exceptionHandler) && $exceptionHandler[0] instanceof ExceptionHandler) { | ||
$level = isset($this->levels[$type]) ? $this->levels[$type] : $type; | ||
$message = sprintf('%s: %s in %s line %d', $level, $error['message'], $error['file'], $error['line']); | ||
$exception = new FatalErrorException($message, 0, $type, $error['file'], $error['line']); | ||
$exceptionHandler[0]->handle($exception); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Symfony/Component/Debug/Exception/FatalErrorException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Debug\Exception; | ||
|
||
/** | ||
* Fatal Error Exception. | ||
* | ||
* @author Konstanton Myakshin <[email protected]> | ||
*/ | ||
class FatalErrorException extends \ErrorException | ||
{ | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could this be reconsidered (or at least rediscussed)? maybe in a separate PR (as this is not a blocking step for creating this standalone component)?