From 35a61b3a52f6b0bd9e25c8e7e44348f5b3c42b31 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 22 Jan 2012 16:53:27 +0100 Subject: [PATCH] [HttpKernel] added arguments to ExceptionHandler (closes #2739) --- .../Templating/Helper/CodeHelper.php | 23 +--------- .../HttpKernel/Debug/ExceptionHandler.php | 43 ++++++++++++++++++- .../HttpKernel/Debug/ExceptionHandlerTest.php | 8 ++++ 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php index b32aa69733..5e6e13c808 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php @@ -51,28 +51,7 @@ class CodeHelper extends Helper */ public function formatArgsAsText(array $args) { - $result = array(); - foreach ($args as $key => $item) { - if ('object' === $item[0]) { - $formattedValue = sprintf("object(%s)", $item[1]); - } elseif ('array' === $item[0]) { - $formattedValue = sprintf("array(%s)", is_array($item[1]) ? $this->formatArgsAsText($item[1]) : $item[1]); - } elseif ('string' === $item[0]) { - $formattedValue = sprintf("'%s'", $item[1]); - } elseif ('null' === $item[0]) { - $formattedValue = 'null'; - } elseif ('boolean' === $item[0]) { - $formattedValue = strtolower(var_export($item[1], true)); - } elseif ('resource' === $item[0]) { - $formattedValue = 'resource'; - } else { - $formattedValue = str_replace("\n", '', var_export((string) $item[1], true)); - } - - $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue); - } - - return implode(', ', $result); + return strip_tags($this->formatArgs($args)); } public function abbrClass($class) diff --git a/src/Symfony/Component/HttpKernel/Debug/ExceptionHandler.php b/src/Symfony/Component/HttpKernel/Debug/ExceptionHandler.php index 8e1fcfecc8..b3c41bd279 100644 --- a/src/Symfony/Component/HttpKernel/Debug/ExceptionHandler.php +++ b/src/Symfony/Component/HttpKernel/Debug/ExceptionHandler.php @@ -15,6 +15,10 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\FlattenException; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; +if (!defined('ENT_SUBSTITUTE')) { + define('ENT_SUBSTITUTE', 8); +} + /** * ExceptionHandler converts an exception to a Response object. * @@ -29,10 +33,12 @@ use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; class ExceptionHandler { private $debug; + private $charset; - public function __construct($debug = true) + public function __construct($debug = true, $charset = 'UTF-8') { $this->debug = $debug; + $this->charset = $charset; } /** @@ -119,7 +125,7 @@ EOF foreach ($e['trace'] as $i => $trace) { $content .= '
  • '; if ($trace['function']) { - $content .= sprintf('at %s%s%s()', $this->abbrClass($trace['class']), $trace['type'], $trace['function']); + $content .= sprintf('at %s%s%s(%s)', $this->abbrClass($trace['class']), $trace['type'], $trace['function'], $this->formatArgs($trace['args'])); } if (isset($trace['file']) && isset($trace['line'])) { if ($linkFormat = ini_get('xdebug.file_link_format')) { @@ -220,4 +226,37 @@ EOF; return sprintf("%s", $class, array_pop($parts)); } + + /** + * Formats an array as a string. + * + * @param array $args The argument array + * + * @return string + */ + public function formatArgs(array $args) + { + $result = array(); + foreach ($args as $key => $item) { + if ('object' === $item[0]) { + $formattedValue = sprintf("object(%s)", $this->abbrClass($item[1])); + } elseif ('array' === $item[0]) { + $formattedValue = sprintf("array(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]); + } elseif ('string' === $item[0]) { + $formattedValue = sprintf("'%s'", htmlspecialchars($item[1], ENT_QUOTES | ENT_SUBSTITUTE, $this->charset)); + } elseif ('null' === $item[0]) { + $formattedValue = 'null'; + } elseif ('boolean' === $item[0]) { + $formattedValue = ''.strtolower(var_export($item[1], true)).''; + } elseif ('resource' === $item[0]) { + $formattedValue = 'resource'; + } else { + $formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), true)); + } + + $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue); + } + + return implode(', ', $result); + } } diff --git a/tests/Symfony/Tests/Component/HttpKernel/Debug/ExceptionHandlerTest.php b/tests/Symfony/Tests/Component/HttpKernel/Debug/ExceptionHandlerTest.php index 26947c5669..1370cca6c0 100644 --- a/tests/Symfony/Tests/Component/HttpKernel/Debug/ExceptionHandlerTest.php +++ b/tests/Symfony/Tests/Component/HttpKernel/Debug/ExceptionHandlerTest.php @@ -43,4 +43,12 @@ class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase $this->assertEquals('404', $response->getStatusCode()); $this->assertContains('Sorry, the page you are looking for could not be found.', $response->getContent()); } + + public function testNestedExceptions() + { + $handler = new ExceptionHandler(true); + $response = $handler->createResponse(new \RuntimeException('Foo', null, new \RuntimeException('Bar'))); + + file_put_contents('/Users/fabien/work/symfony/git/symfony/exception.html', $response->getContent()); + } }