Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Debug] cleanup interfaces before 2.5-final #10941

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 2 commits into from
May 21, 2014
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
6 changes: 3 additions & 3 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parameter key="debug.stopwatch.class">Symfony\Component\Stopwatch\Stopwatch</parameter>
<parameter key="debug.container.dump">%kernel.cache_dir%/%kernel.container_class%.xml</parameter>
<parameter key="debug.controller_resolver.class">Symfony\Component\HttpKernel\Controller\TraceableControllerResolver</parameter>
<parameter key="debug.fatal_error_exceptions_listener.class">Symfony\Component\HttpKernel\EventListener\FatalErrorExceptionsListener</parameter>
<parameter key="debug.debug_handlers_listener.class">Symfony\Component\HttpKernel\EventListener\DebugHandlersListener</parameter>
</parameters>

<services>
Expand Down Expand Up @@ -41,11 +41,11 @@
<argument type="service" id="logger" on-invalid="null" />
</service>

<service id="debug.fatal_error_exceptions_listener" class="%debug.fatal_error_exceptions_listener.class%">
<service id="debug.debug_handlers_listener" class="%debug.debug_handlers_listener.class%">
<tag name="kernel.event_subscriber" />
<argument type="collection">
<argument type="service" id="http_kernel" on-invalid="null" />
<argument>handleFatalErrorException</argument>
<argument>terminateWithException</argument>
</argument>
</service>
</services>
Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Component/Debug/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ CHANGELOG
2.5.0
-----

* added ErrorHandler::setFatalErrorExceptionHandler()
* added ExceptionHandler::setHandler()
* added UndefinedMethodFatalErrorHandler
* deprecated ExceptionHandlerInterface
* deprecated DummyException

2.4.0
Expand Down
104 changes: 17 additions & 87 deletions src/Symfony/Component/Debug/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Debug\Exception\ContextErrorException;
use Symfony\Component\Debug\Exception\FatalErrorException;
use Symfony\Component\Debug\Exception\OutOfMemoryException;
use Symfony\Component\Debug\FatalErrorHandler\UndefinedFunctionFatalErrorHandler;
use Symfony\Component\Debug\FatalErrorHandler\UndefinedMethodFatalErrorHandler;
use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
Expand Down Expand Up @@ -53,8 +54,6 @@ class ErrorHandler

private $displayErrors;

private $caughtOutput = 0;

/**
* @var LoggerInterface[] Loggers for channels
*/
Expand All @@ -64,8 +63,6 @@ class ErrorHandler

private static $stackedErrorLevels = array();

private static $fatalHandler = false;

/**
* Registers the error handler.
*
Expand Down Expand Up @@ -119,16 +116,6 @@ public static function setLogger(LoggerInterface $logger, $channel = 'deprecatio
self::$loggers[$channel] = $logger;
}

/**
* Sets a fatal error exception handler.
*
* @param callable $handler An handler that will be called on FatalErrorException
*/
public static function setFatalErrorExceptionHandler($handler)
{
self::$fatalHandler = $handler;
}

/**
* @throws ContextErrorException When error_reporting returns error
*/
Expand Down Expand Up @@ -284,7 +271,7 @@ public function handleFatal()
throw $exception;
}

if (!$error || !$this->level || !in_array($error['type'], array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) {
if (!$error || !$this->level || !($error['type'] & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_PARSE))) {
return;
}

Expand All @@ -298,7 +285,7 @@ public function handleFatal()
self::$loggers['emergency']->emergency($error['message'], $fatal);
}

if ($this->displayErrors && ($exceptionHandler || self::$fatalHandler)) {
if ($this->displayErrors && $exceptionHandler) {
$this->handleFatalError($exceptionHandler, $error);
}
}
Expand Down Expand Up @@ -327,82 +314,25 @@ private function handleFatalError($exceptionHandler, array $error)

$level = isset($this->levels[$error['type']]) ? $this->levels[$error['type']] : $error['type'];
$message = sprintf('%s: %s in %s line %d', $level, $error['message'], $error['file'], $error['line']);
$exception = new FatalErrorException($message, 0, $error['type'], $error['file'], $error['line'], 3);

foreach ($this->getFatalErrorHandlers() as $handler) {
if ($e = $handler->handleError($error, $exception)) {
$exception = $e;
break;
}
}

// To be as fail-safe as possible, the FatalErrorException is first handled
// by the exception handler, then by the fatal error handler. The latter takes
// precedence and any output from the former is cancelled, if and only if
// nothing bad happens in this handling path.

$caughtOutput = 0;

if ($exceptionHandler) {
$this->caughtOutput = false;
ob_start(array($this, 'catchOutput'));
try {
call_user_func($exceptionHandler, $exception);
} catch (\Exception $e) {
// Ignore this exception, we have to deal with the fatal error
}
if (false === $this->caughtOutput) {
ob_end_clean();
}
if (isset($this->caughtOutput[0])) {
ob_start(array($this, 'cleanOutput'));
echo $this->caughtOutput;
$caughtOutput = ob_get_length();
}
$this->caughtOutput = 0;
}

if (self::$fatalHandler) {
try {
call_user_func(self::$fatalHandler, $exception);

if ($caughtOutput) {
$this->caughtOutput = $caughtOutput;
}
} catch (\Exception $e) {
if (!$caughtOutput) {
// Neither the exception nor the fatal handler succeeded.
// Let PHP handle that now.
throw $exception;
if (0 === strpos($error['message'], 'Allowed memory') || 0 === strpos($error['message'], 'Out of memory')) {
$exception = new OutOfMemoryException($message, 0, $error['type'], $error['file'], $error['line'], 3, false);
} else {
$exception = new FatalErrorException($message, 0, $error['type'], $error['file'], $error['line'], 3, true);

foreach ($this->getFatalErrorHandlers() as $handler) {
if ($e = $handler->handleError($error, $exception)) {
$exception = $e;
break;
}
}
}
}

/**
* @internal
*/
public function catchOutput($buffer)
{
$this->caughtOutput = $buffer;

return '';
}

/**
* @internal
*/
public function cleanOutput($buffer)
{
if ($this->caughtOutput) {
// use substr_replace() instead of substr() for mbstring overloading resistance
$cleanBuffer = substr_replace($buffer, '', 0, $this->caughtOutput);
if (isset($cleanBuffer[0])) {
$buffer = $cleanBuffer;
}
try {
call_user_func($exceptionHandler, $exception);
} catch (\Exception $e) {
// The handler failed. Let PHP handle that now.
throw $exception;
}

return $buffer;
}
}

Expand Down
24 changes: 14 additions & 10 deletions src/Symfony/Component/Debug/Exception/FatalErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,40 @@
*/
class FatalErrorException extends \ErrorException
{
public function __construct($message, $code, $severity, $filename, $lineno, $traceOffset = null)
public function __construct($message, $code, $severity, $filename, $lineno, $traceOffset = null, $traceArgs = true)
{
parent::__construct($message, $code, $severity, $filename, $lineno);

if (null !== $traceOffset) {
if (function_exists('xdebug_get_function_stack')) {
$trace = xdebug_get_function_stack();
if (0 < $traceOffset) {
$trace = array_slice($trace, 0, -$traceOffset);
array_splice($trace, -$traceOffset);
}
$trace = array_reverse($trace);

foreach ($trace as $i => $frame) {
foreach ($trace as &$frame) {
if (!isset($frame['type'])) {
// XDebug pre 2.1.1 doesn't currently set the call type key http://bugs.xdebug.org/view.php?id=695
if (isset($frame['class'])) {
$trace[$i]['type'] = '::';
$frame['type'] = '::';
}
} elseif ('dynamic' === $frame['type']) {
$trace[$i]['type'] = '->';
$frame['type'] = '->';
} elseif ('static' === $frame['type']) {
$trace[$i]['type'] = '::';
$frame['type'] = '::';
}

// XDebug also has a different name for the parameters array
if (isset($frame['params']) && !isset($frame['args'])) {
$trace[$i]['args'] = $frame['params'];
unset($trace[$i]['params']);
if (!$traceArgs) {
unset($frame['params'], $frame['args']);
} elseif (isset($frame['params']) && !isset($frame['args'])) {
$frame['args'] = $frame['params'];
unset($frame['params']);
}
}

unset($frame);
$trace = array_reverse($trace);
} else {
$trace = array();
}
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Debug/Exception/OutOfMemoryException.php
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;

/**
* Out of memory exception.
*
* @author Nicolas Grekas <[email protected]>
*/
class OutOfMemoryException extends FatalErrorException
{
}
Loading