This commit is contained in:
Bastian Allgeier
2020-07-07 12:40:13 +02:00
parent 5f025ac2c2
commit f79d2e960c
176 changed files with 10532 additions and 5343 deletions

View File

@@ -10,7 +10,7 @@ use Whoops\Handler\PrettyPageHandler;
use Whoops\Run as Whoops;
/**
* AppErrors
* PHP error handling using the Whoops library
*
* @package Kirby Cms
* @author Bastian Allgeier <bastian@getkirby.com>
@@ -20,14 +20,30 @@ use Whoops\Run as Whoops;
*/
trait AppErrors
{
/**
* Whoops instance cache
*
* @var \Whoops\Run
*/
protected $whoops;
/**
* Registers the PHP error handler for CLI usage
*
* @return void
*/
protected function handleCliErrors(): void
{
$whoops = new Whoops();
$whoops->pushHandler(new PlainTextHandler());
$whoops->register();
$this->setWhoopsHandler(new PlainTextHandler());
}
protected function handleErrors()
/**
* Registers the PHP error handler
* based on the environment
*
* @return void
*/
protected function handleErrors(): void
{
if ($this->request()->cli() === true) {
$this->handleCliErrors();
@@ -42,21 +58,25 @@ trait AppErrors
$this->handleHtmlErrors();
}
protected function handleHtmlErrors()
/**
* Registers the PHP error handler for HTML output
*
* @return void
*/
protected function handleHtmlErrors(): void
{
$whoops = new Whoops();
$handler = null;
if ($this->option('debug') === true) {
if ($this->option('whoops', true) === true) {
$handler = new PrettyPageHandler();
$handler->setPageTitle('Kirby CMS Debugger');
$handler->setResourcesPath(dirname(__DIR__, 2) . '/assets');
$handler->addCustomCss('whoops.css');
if ($editor = $this->option('editor')) {
$handler->setEditor($editor);
}
$whoops->pushHandler($handler);
$whoops->register();
}
} else {
$handler = new CallbackHandler(function ($exception, $inspector, $run) {
@@ -65,20 +85,27 @@ trait AppErrors
if (is_a($fatal, 'Closure') === true) {
echo $fatal($this);
} else {
include static::$root . '/views/fatal.php';
include $this->root('kirby') . '/views/fatal.php';
}
return Handler::QUIT;
});
}
$whoops->pushHandler($handler);
$whoops->register();
if ($handler !== null) {
$this->setWhoopsHandler($handler);
} else {
$this->unsetWhoopsHandler();
}
}
protected function handleJsonErrors()
/**
* Registers the PHP error handler for JSON output
*
* @return void
*/
protected function handleJsonErrors(): void
{
$whoops = new Whoops();
$handler = new CallbackHandler(function ($exception, $inspector, $run) {
if (is_a($exception, 'Kirby\Exception\Exception') === true) {
$httpCode = $exception->getHttpCode();
@@ -112,7 +139,46 @@ trait AppErrors
return Handler::QUIT;
});
$this->setWhoopsHandler($handler);
}
/**
* Enables Whoops with the specified handler
*
* @param Callable|\Whoops\Handler\HandlerInterface $handler
* @return void
*/
protected function setWhoopsHandler($handler): void
{
$whoops = $this->whoops();
$whoops->clearHandlers();
$whoops->pushHandler($handler);
$whoops->register();
$whoops->register(); // will only do something if not already registered
}
/**
* Clears the Whoops handlers and disables Whoops
*
* @return void
*/
protected function unsetWhoopsHandler(): void
{
$whoops = $this->whoops();
$whoops->clearHandlers();
$whoops->unregister(); // will only do something if currently registered
}
/**
* Returns the Whoops error handler instance
*
* @return \Whoops\Run
*/
protected function whoops()
{
if ($this->whoops !== null) {
return $this->whoops;
}
return $this->whoops = new Whoops();
}
}