<?php
namespace Symfony\Component\Debug;
if (!defined('ENT_SUBSTITUTE')) {
define('ENT_SUBSTITUTE', 8);
}
class ExceptionHandler
{
private $debug;
private $charset;
private $handler;
private $caughtOutput = 0;
public function __construct($debug = true, $charset = 'UTF-8')
{
$this->debug = $debug;
$this->charset = $charset;
}
public static function register($debug = true)
{
$handler = new static($debug);
set_exception_handler(array($handler, 'handle'));
return $handler;
}
public function setHandler($handler)
{
if (isset($handler) && !is_callable($handler)) {
throw new \LogicException('The exception handler must be a valid PHP callable.');
}
$old = $this->handler;
$this->handler = $handler;
return $old;
}
public function handle(\Exception $exception)
{
$caughtOutput = 0;
$this->caughtOutput = false;
ob_start(array($this, 'catchOutput'));
echo $exception->getMessage();
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 (!empty($this->handler)) {
try {
call_user_func($this->handler, $exception);
if ($caughtOutput) {
$this->caughtOutput = $caughtOutput;
}
ob_end_flush();
} catch (\Exception $e) {
if (!$caughtOutput) {
// All handlers failed. Let PHP handle that now.
throw $exception;
}
}
}
}
public function catchOutput($buffer)
{
$this->caughtOutput = $buffer;
return '';
}
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;
}
}
return $buffer;
}
}
ExceptionHandler::register();
throw new \Exception('foo msg');
- Output for 5.3.0 - 5.3.29, 5.4.10 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- foo msg
- Output for 5.4.0 - 5.4.9
- Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
- Parse error: syntax error, unexpected T_STRING in /in/joqjK on line 3
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- Parse error: parse error, unexpected T_STRING in /in/joqjK on line 3
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/joqjK on line 3
Process exited with code 255.
preferences:
198.78 ms | 401 KiB | 313 Q