From ffbbd2c06c64b08fb47974eed5dbce4ca2bb0eec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Guti=C3=A9rrez?= Date: Thu, 3 Aug 2017 20:23:40 +0200 Subject: [PATCH] Make PrettyPageHandler more easily extendable (#517) separate code and frames extraction --- src/Whoops/Handler/PrettyPageHandler.php | 64 ++++++++++++++++-------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/src/Whoops/Handler/PrettyPageHandler.php b/src/Whoops/Handler/PrettyPageHandler.php index 26c5ae1..0f215a8 100644 --- a/src/Whoops/Handler/PrettyPageHandler.php +++ b/src/Whoops/Handler/PrettyPageHandler.php @@ -176,27 +176,8 @@ class PrettyPageHandler extends Handler } $inspector = $this->getInspector(); - $frames = $inspector->getFrames(); - - $code = $inspector->getException()->getCode(); - - if ($inspector->getException() instanceof \ErrorException) { - // ErrorExceptions wrap the php-error types within the "severity" property - $code = Misc::translateErrorCode($inspector->getException()->getSeverity()); - } - - // Detect frames that belong to the application. - if ($this->applicationPaths) { - /* @var \Whoops\Exception\Frame $frame */ - foreach ($frames as $frame) { - foreach ($this->applicationPaths as $path) { - if (substr($frame->getFile(), 0, strlen($path)) === $path) { - $frame->setApplication(true); - break; - } - } - } - } + $frames = $this->getExceptionFrames(); + $code = $this->getExceptionCode(); // List of variables that will be passed to the layout template. $vars = [ @@ -268,6 +249,47 @@ class PrettyPageHandler extends Handler return Handler::QUIT; } + /** + * Get the stack trace frames of the exception that is currently being handled. + * + * @return \Whoops\Exception\FrameCollection; + */ + protected function getExceptionFrames() + { + $frames = $this->getInspector()->getFrames(); + + if ($this->getApplicationPaths()) { + foreach ($frames as $frame) { + foreach ($this->getApplicationPaths() as $path) { + if (strpos($frame->getFile(), $path) === 0) { + $frame->setApplication(true); + break; + } + } + } + } + + return $frames; + } + + /** + * Get the code of the exception that is currently being handled. + * + * @return string + */ + protected function getExceptionCode() + { + $exception = $this->getException(); + + $code = $exception->getCode(); + if ($exception instanceof \ErrorException) { + // ErrorExceptions wrap the php-error types within the 'severity' property + $code = Misc::translateErrorCode($exception->getSeverity()); + } + + return (string) $code; + } + /** * @return string */