Navigation Menu

Skip to content

Commit

Permalink
[Debug] added the component (closes #6828, closes #6834, closes #7330)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Apr 7, 2013
1 parent 22adfdf commit 2ff0927
Show file tree
Hide file tree
Showing 20 changed files with 872 additions and 648 deletions.
14 changes: 12 additions & 2 deletions UPGRADE-3.0.md
Expand Up @@ -3,8 +3,8 @@ UPGRADE FROM 2.x to 3.0

### ClassLoader

* The `UniversalClassLoader` class has been removed in favor of `ClassLoader`. The only difference is that some method
names are different:
* The `UniversalClassLoader` class has been removed in favor of
`ClassLoader`. The only difference is that some method names are different:

* `registerNamespaces()` -> `addPrefixes()`
* `registerPrefixes()` -> `addPrefixes()`
Expand Down Expand Up @@ -34,6 +34,16 @@ UPGRADE FROM 2.x to 3.0
* `Symfony\Bridge\Monolog\Logger`
* `Symfony\Component\HttpKernel\Log\NullLogger`

* The `Symfony\Component\HttpKernel\Kernel::init()` method has been removed.

* The following classes have been renamed as they have been moved to the
Debug component:

* `Symfony\Component\HttpKernel\Debug\ErrorHandler` -> `Symfony\Component\Debug\ErrorHandler`
* `Symfony\Component\HttpKernel\Debug\ExceptionHandler` -> `Symfony\Component\Debug\ExceptionHandler`
* `Symfony\Component\HttpKernel\Exception\FatalErrorException` -> `Symfony\Component\Debug\Exception\FatalErrorException`
* `Symfony\Component\HttpKernel\Exception\FlattenException` -> `Symfony\Component\Debug\Exception\FlattenException`

### Routing

* Some route settings have been renamed:
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Debug/.gitignore
@@ -0,0 +1,5 @@
vendor/
composer.lock
phpunit.xml
Tests/ProjectContainer.php
Tests/classes.map
7 changes: 7 additions & 0 deletions src/Symfony/Component/Debug/CHANGELOG.md
@@ -0,0 +1,7 @@
CHANGELOG
=========

2.3.0
-----

* added the component
128 changes: 128 additions & 0 deletions src/Symfony/Component/Debug/ErrorHandler.php
@@ -0,0 +1,128 @@
<?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;

use Symfony\Component\Debug\Exception\FatalErrorException;
use Psr\Log\LoggerInterface;

/**
* ErrorHandler.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
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;

/**
* Register 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 src/Symfony/Component/Debug/Exception/FatalErrorException.php
@@ -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;

/**
* Fatal Error Exception.
*
* @author Konstanton Myakshin <koc-dp@yandex.ru>
*/
class FatalErrorException extends \ErrorException
{
}

0 comments on commit 2ff0927

Please sign in to comment.