Add new "dumpdie" function that uses an exception to write to the PSR response and thus still works with RoadRunner.
This commit is contained in:
parent
e890106010
commit
0f651a3dc5
|
@ -161,7 +161,10 @@
|
|||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "src"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"./config/functions.php"
|
||||
]
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"prefer-stable": true,
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function env(): App\Environment
|
||||
{
|
||||
return App\Environment::getInstance();
|
||||
}
|
||||
|
||||
function logger(): Monolog\Logger
|
||||
{
|
||||
return App\Utilities\Logger::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws App\Exception\DumpException
|
||||
*/
|
||||
function dumpdie(mixed ...$vars): never
|
||||
{
|
||||
if (env()->isCli()) {
|
||||
$dumper = new Symfony\Component\VarDumper\Dumper\CliDumper();
|
||||
$varCloner = new Symfony\Component\VarDumper\Cloner\VarCloner();
|
||||
|
||||
foreach ($vars as $var) {
|
||||
$dumper->dump($varCloner->cloneVar($var), true);
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$dumper = new Symfony\Component\VarDumper\Dumper\HtmlDumper();
|
||||
$varCloner = new Symfony\Component\VarDumper\Cloner\VarCloner();
|
||||
|
||||
$dumps = [];
|
||||
foreach ($vars as $var) {
|
||||
$dumps[] = $dumper->dump($varCloner->cloneVar($var), true);
|
||||
}
|
||||
|
||||
throw new App\Exception\DumpException(dumps: $dumps);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exception;
|
||||
|
||||
use App\Exception;
|
||||
use Monolog\Level;
|
||||
use Throwable;
|
||||
|
||||
final class DumpException extends Exception
|
||||
{
|
||||
public function __construct(
|
||||
string $message = 'Debug app dump.',
|
||||
int $code = 200,
|
||||
Throwable $previous = null,
|
||||
Level $loggerLevel = Level::Debug,
|
||||
private readonly array $dumps = []
|
||||
) {
|
||||
parent::__construct($message, $code, $previous, $loggerLevel);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getDumps(): array
|
||||
{
|
||||
return $this->dumps;
|
||||
}
|
||||
}
|
|
@ -116,6 +116,19 @@ final class ErrorHandler extends SlimErrorHandler
|
|||
$locale->register($this->environment);
|
||||
}
|
||||
|
||||
if ($this->exception instanceof Exception\DumpException) {
|
||||
$response = $this->responseFactory->createResponse($this->statusCode);
|
||||
$responseBody = $response->getBody();
|
||||
foreach ($this->exception->getDumps() as $dump) {
|
||||
$responseBody->write($dump);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
if (!($this->request instanceof ServerRequest)) {
|
||||
return parent::respond();
|
||||
}
|
||||
|
||||
// Special handling for cURL requests.
|
||||
$ua = $this->request->getHeaderLine('User-Agent');
|
||||
|
||||
|
@ -134,10 +147,6 @@ final class ErrorHandler extends SlimErrorHandler
|
|||
return $response;
|
||||
}
|
||||
|
||||
if (!($this->request instanceof ServerRequest)) {
|
||||
return parent::respond();
|
||||
}
|
||||
|
||||
if ($this->exception instanceof HttpException) {
|
||||
/** @var Response $response */
|
||||
$response = $this->responseFactory->createResponse($this->exception->getCode());
|
||||
|
|
Loading…
Reference in New Issue