-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add new code base for Whoops Error handling with updated …
…settings
- Loading branch information
Showing
5 changed files
with
220 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Flextype (https://flextype.org) | ||
* Founded by Sergey Romanenko and maintained by Flextype Community. | ||
*/ | ||
|
||
namespace Flextype\Middlewares; | ||
|
||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Flextype\WhoopsGuard; | ||
|
||
class WhoopsMiddleware implements MiddlewareInterface { | ||
|
||
protected $settings = []; | ||
protected $handlers = []; | ||
|
||
/** | ||
* Instance the whoops middleware object | ||
* | ||
* @param array $settings | ||
* @param array $handlers | ||
*/ | ||
public function __construct(array $settings = [], array $handlers = []) { | ||
$this->settings = $settings; | ||
$this->handlers = $handlers; | ||
} | ||
|
||
/** | ||
* Handle the requests | ||
* | ||
* @param ServerRequestInterface $request | ||
* @param RequestHandlerInterface $handler | ||
* @return ResponseInterface | ||
*/ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { | ||
$whoopsGuard = new WhoopsGuard($this->settings); | ||
$whoopsGuard->setRequest($request); | ||
$whoopsGuard->setHandlers($this->handlers); | ||
$whoopsGuard->install(); | ||
|
||
return $handler->handle($request); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Flextype (https://flextype.org) | ||
* Founded by Sergey Romanenko and maintained by Flextype Community. | ||
*/ | ||
|
||
namespace Flextype; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Whoops\Run as WhoopsRun; | ||
use Whoops\Util\Misc; | ||
use Whoops\Handler\PrettyPageHandler; | ||
use Whoops\Handler\PlainTextHandler; | ||
use Whoops\Handler\JsonResponseHandler; | ||
use Whoops\Handler\XmlResponseHandler; | ||
|
||
class WhoopsGuard { | ||
|
||
protected $settings = []; | ||
protected $request = null; | ||
protected $handlers = []; | ||
|
||
/** | ||
* Instance the whoops guard object | ||
* | ||
* @param array $settings | ||
*/ | ||
public function __construct(array $settings = []) { | ||
$this->settings = array_merge([ | ||
'enable' => true, | ||
'editor' => '', | ||
'title' => '', | ||
], $settings); | ||
} | ||
|
||
/** | ||
* Set the server request object | ||
* | ||
* @param ServerRequestInterface $request | ||
* @return void | ||
*/ | ||
public function setRequest(ServerRequestInterface $request): void { | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* Set the custom handlers for whoops | ||
* | ||
* @param array $handlers | ||
* @return void | ||
*/ | ||
public function setHandlers(array $handlers): void { | ||
$this->handlers = $handlers; | ||
} | ||
|
||
/** | ||
* Install the whoops guard object | ||
* | ||
* @return WhoopsRun|null | ||
*/ | ||
public function install(): ?WhoopsRun { | ||
if ($this->settings['enable'] === false) { | ||
return null; | ||
} | ||
|
||
// Enable PrettyPageHandler with editor options | ||
$prettyPageHandler = new PrettyPageHandler(); | ||
|
||
if (empty($this->settings['editor']) === false) { | ||
$prettyPageHandler->setEditor($this->settings['editor']); | ||
} | ||
|
||
if (empty($this->settings['title']) === false) { | ||
$prettyPageHandler->setPageTitle($this->settings['title']); | ||
} | ||
|
||
// Add more information to the PrettyPageHandler | ||
$contentCharset = '<none>'; | ||
if ( | ||
method_exists($this->request, 'getContentCharset') === true && | ||
$this->request->getContentCharset() !== null | ||
) { | ||
$contentCharset = $this->request->getContentCharset(); | ||
} | ||
|
||
$prettyPageHandler->addDataTable('Flextype', [ | ||
'Version' => Flextype::VERSION, | ||
'Accept Charset' => $this->request->getHeader('ACCEPT_CHARSET') ?: '<none>', | ||
'Content Charset' => $contentCharset, | ||
'HTTP Method' => $this->request->getMethod(), | ||
'Path' => $this->request->getUri()->getPath(), | ||
'Query String' => $this->request->getUri()->getQuery() ?: '<none>', | ||
'Base URL' => (string) $this->request->getUri(), | ||
'Scheme' => $this->request->getUri()->getScheme(), | ||
'Port' => $this->request->getUri()->getPort(), | ||
'Host' => $this->request->getUri()->getHost(), | ||
]); | ||
|
||
// Set Whoops to default exception handler | ||
$whoops = new \Whoops\Run; | ||
|
||
switch ($this->settings['handler']) { | ||
case 'json': | ||
$whoops->pushHandler(new JsonResponseHandler()); | ||
break; | ||
|
||
case 'xml': | ||
$whoops->pushHandler(new XmlResponseHandler()); | ||
break; | ||
|
||
case 'plain': | ||
$whoops->pushHandler(new PlainTextHandler()); | ||
break; | ||
|
||
case 'pretty': | ||
default: | ||
$whoops->pushHandler($prettyPageHandler); | ||
break; | ||
} | ||
|
||
// Enable JsonResponseHandler when request is AJAX | ||
if (Misc::isAjaxRequest() === true){ | ||
$whoops->pushHandler(new JsonResponseHandler()); | ||
} | ||
|
||
// Add each custom handler to whoops handler stack | ||
if (empty($this->handlers) === false) { | ||
foreach($this->handlers as $handler) { | ||
$whoops->pushHandler($handler); | ||
} | ||
} | ||
|
||
$whoops->register(); | ||
|
||
return $whoops; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters