jonathanmaron /
ctw-middleware-httpexception
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace Ctw\Middleware\HttpExceptionMiddleware; |
||
| 5 | |||
| 6 | use Ctw\Http\HttpException; |
||
| 7 | use Ctw\Http\HttpStatus; |
||
| 8 | use Laminas\DevelopmentMode; |
||
|
0 ignored issues
–
show
|
|||
| 9 | use Laminas\Diactoros\Response\HtmlResponse; |
||
| 10 | use Laminas\Diactoros\Response\JsonResponse; |
||
| 11 | use Mezzio\Template\TemplateRendererInterface as Template; |
||
| 12 | use Psr\Http\Message\ServerRequestInterface; |
||
| 13 | use Psr\Http\Server\MiddlewareInterface; |
||
| 14 | |||
| 15 | abstract class AbstractHttpExceptionMiddleware implements MiddlewareInterface |
||
| 16 | { |
||
| 17 | private Template $template; |
||
| 18 | |||
| 19 | 2 | public function getTemplate(): Template |
|
| 20 | { |
||
| 21 | 2 | return $this->template; |
|
| 22 | } |
||
| 23 | |||
| 24 | 4 | public function setTemplate(Template $template): self |
|
| 25 | { |
||
| 26 | 4 | $this->template = $template; |
|
| 27 | |||
| 28 | 4 | return $this; |
|
| 29 | } |
||
| 30 | |||
| 31 | 4 | protected function asJson(ServerRequestInterface $request): bool |
|
| 32 | { |
||
| 33 | 4 | $header = $request->getHeader('Accept'); |
|
| 34 | 4 | $header = array_filter($header, static function (string $string): bool { |
|
| 35 | 2 | $pos = strpos($string, 'application/json'); |
|
| 36 | 2 | return is_int($pos); |
|
| 37 | 4 | }); |
|
| 38 | |||
| 39 | 4 | return [] !== $header; |
|
| 40 | } |
||
| 41 | |||
| 42 | 2 | protected function getJsonResponse(HttpException\HttpExceptionInterface $exception): JsonResponse |
|
| 43 | { |
||
| 44 | 2 | $statusCode = $exception->getStatusCode(); |
|
| 45 | |||
| 46 | 2 | $entity = (new HttpStatus($statusCode))->get(); |
|
| 47 | |||
| 48 | 2 | $data = [ |
|
| 49 | 2 | 'type' => $entity->url, |
|
| 50 | 2 | 'title' => $entity->name, |
|
| 51 | 2 | 'status' => $entity->statusCode, |
|
| 52 | 2 | 'detail' => $exception->getMessage(), |
|
| 53 | 2 | ]; |
|
| 54 | |||
| 55 | 2 | return new JsonResponse($data, $exception->getStatusCode(), [ |
|
| 56 | 2 | 'Content-Type' => 'application/problem+json', |
|
| 57 | 2 | ]); |
|
| 58 | } |
||
| 59 | |||
| 60 | 2 | protected function getHtmlResponse(HttpException\HttpExceptionInterface $exception): HtmlResponse |
|
| 61 | { |
||
| 62 | 2 | $template = $this->getTemplate(); |
|
| 63 | 2 | $statusCode = $exception->getStatusCode(); |
|
| 64 | |||
| 65 | 2 | $entity = (new HttpStatus($statusCode))->get(); |
|
| 66 | |||
| 67 | 2 | $data = [ |
|
| 68 | 2 | 'entity' => $entity, |
|
| 69 | 2 | 'exception' => $exception, |
|
| 70 | 2 | ]; |
|
| 71 | |||
| 72 | 2 | $html = $template->render('error::http-exception', $data); |
|
| 73 | |||
| 74 | 2 | return new HtmlResponse($html, $exception->getStatusCode()); |
|
| 75 | } |
||
| 76 | |||
| 77 | 4 | protected function isDevelopmentMode(): bool |
|
| 78 | { |
||
| 79 | // @todo there must be a better way of doing this :-) |
||
| 80 | // @todo get development mode status from container in factory? |
||
| 81 | |||
| 82 | // "composer install --no-dev" removes this class |
||
| 83 | 4 | $class = '\Laminas\DevelopmentMode\Status'; |
|
| 84 | 4 | if (!class_exists($class)) { |
|
| 85 | 4 | return false; |
|
| 86 | } |
||
| 87 | |||
| 88 | ob_start(); |
||
| 89 | (new DevelopmentMode\Status())->__invoke(); |
||
|
0 ignored issues
–
show
The type
Laminas\DevelopmentMode\Status was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 90 | $ob = (string) ob_get_clean(); |
||
| 91 | $pos = strpos($ob, 'ENABLED'); |
||
| 92 | |||
| 93 | return is_int($pos); |
||
| 94 | } |
||
| 95 | } |
||
| 96 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths