Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
9 / 10 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| WhoopsHandler | |
90.00% |
9 / 10 |
|
50.00% |
1 / 2 |
4.02 | |
0.00% |
0 / 1 |
| available | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| handle | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
2.01 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\View\Boiler\Error; |
| 6 | |
| 7 | use Celemas\Core\Error\DebugHandler; |
| 8 | use Override; |
| 9 | use Psr\Http\Message\ResponseFactoryInterface as ResponseFactory; |
| 10 | use Psr\Http\Message\ResponseInterface as Response; |
| 11 | use RuntimeException; |
| 12 | use Throwable; |
| 13 | |
| 14 | final class WhoopsHandler implements DebugHandler |
| 15 | { |
| 16 | public static function available(): bool |
| 17 | { |
| 18 | return class_exists(\Whoops\Run::class) && class_exists(\Whoops\Handler\PrettyPageHandler::class); |
| 19 | } |
| 20 | |
| 21 | #[Override] |
| 22 | public function handle(Throwable $exception, ResponseFactory $factory): Response |
| 23 | { |
| 24 | if (!self::available()) { |
| 25 | throw new RuntimeException('Install filp/whoops to use the CMS Whoops debug handler.'); |
| 26 | } |
| 27 | |
| 28 | $whoops = new \Whoops\Run(); |
| 29 | $whoops->allowQuit(false); |
| 30 | $whoops->writeToOutput(false); |
| 31 | $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler()); |
| 32 | |
| 33 | $response = $factory->createResponse()->withStatus(500)->withHeader('Content-type', 'text/html'); |
| 34 | $response->getBody()->write($whoops->handleException($exception)); |
| 35 | |
| 36 | return $response; |
| 37 | } |
| 38 | } |