|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\ErrorHandler\Factory; |
| 6 | + |
| 7 | +use Throwable; |
| 8 | +use InvalidArgumentException; |
| 9 | +use Psr\Container\ContainerInterface; |
| 10 | +use Psr\Http\Message\ResponseFactoryInterface; |
| 11 | +use Psr\Http\Message\ResponseInterface; |
| 12 | +use Psr\Http\Message\ServerRequestInterface; |
| 13 | +use Yiisoft\ErrorHandler\ErrorHandler; |
| 14 | +use Yiisoft\ErrorHandler\HeadersProvider; |
| 15 | +use Yiisoft\ErrorHandler\Renderer\HeaderRenderer; |
| 16 | +use Yiisoft\ErrorHandler\Renderer\HtmlRenderer; |
| 17 | +use Yiisoft\ErrorHandler\Renderer\JsonRenderer; |
| 18 | +use Yiisoft\ErrorHandler\Renderer\PlainTextRenderer; |
| 19 | +use Yiisoft\ErrorHandler\Renderer\XmlRenderer; |
| 20 | +use Yiisoft\ErrorHandler\ThrowableRendererInterface; |
| 21 | +use Yiisoft\ErrorHandler\ThrowableResponseFactoryInterface; |
| 22 | +use Yiisoft\Http\Header; |
| 23 | +use Yiisoft\Http\HeaderValueHelper; |
| 24 | +use Yiisoft\Http\Method; |
| 25 | +use Yiisoft\Http\Status; |
| 26 | + |
| 27 | +use function array_key_exists; |
| 28 | +use function count; |
| 29 | +use function is_subclass_of; |
| 30 | +use function sprintf; |
| 31 | +use function strtolower; |
| 32 | +use function trim; |
| 33 | + |
| 34 | +/** |
| 35 | + * `ThrowableResponseFactory` renders `Throwable` object |
| 36 | + * and produces a response according to the content type provided by the client. |
| 37 | + */ |
| 38 | +final class ThrowableResponseFactory implements ThrowableResponseFactoryInterface |
| 39 | +{ |
| 40 | + private HeadersProvider $headersProvider; |
| 41 | + |
| 42 | + /** |
| 43 | + * @psalm-var array<string,class-string<ThrowableRendererInterface>> |
| 44 | + */ |
| 45 | + private array $renderers = [ |
| 46 | + 'application/json' => JsonRenderer::class, |
| 47 | + 'application/xml' => XmlRenderer::class, |
| 48 | + 'text/xml' => XmlRenderer::class, |
| 49 | + 'text/plain' => PlainTextRenderer::class, |
| 50 | + 'text/html' => HtmlRenderer::class, |
| 51 | + '*/*' => HtmlRenderer::class, |
| 52 | + ]; |
| 53 | + private ?string $contentType = null; |
| 54 | + |
| 55 | + public function __construct( |
| 56 | + private ResponseFactoryInterface $responseFactory, |
| 57 | + private ErrorHandler $errorHandler, |
| 58 | + private ContainerInterface $container, |
| 59 | + HeadersProvider $headersProvider = null, |
| 60 | + ) { |
| 61 | + $this->headersProvider = $headersProvider ?? new HeadersProvider(); |
| 62 | + } |
| 63 | + |
| 64 | + public function create(Throwable $throwable, ServerRequestInterface $request): ResponseInterface |
| 65 | + { |
| 66 | + $contentType = $this->contentType ?? $this->getContentType($request); |
| 67 | + $renderer = $request->getMethod() === Method::HEAD ? new HeaderRenderer() : $this->getRenderer($contentType); |
| 68 | + |
| 69 | + $data = $this->errorHandler->handle($throwable, $renderer, $request); |
| 70 | + $response = $this->responseFactory->createResponse(Status::INTERNAL_SERVER_ERROR); |
| 71 | + foreach ($this->headersProvider->getAll() as $name => $value) { |
| 72 | + $response = $response->withHeader($name, $value); |
| 73 | + } |
| 74 | + return $data->addToResponse($response->withHeader(Header::CONTENT_TYPE, $contentType)); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns a new instance with the specified content type and renderer class. |
| 79 | + * |
| 80 | + * @param string $contentType The content type to add associated renderers for. |
| 81 | + * @param string $rendererClass The classname implementing the {@see ThrowableRendererInterface}. |
| 82 | + */ |
| 83 | + public function withRenderer(string $contentType, string $rendererClass): self |
| 84 | + { |
| 85 | + if (!is_subclass_of($rendererClass, ThrowableRendererInterface::class)) { |
| 86 | + throw new InvalidArgumentException(sprintf( |
| 87 | + 'Class "%s" does not implement "%s".', |
| 88 | + $rendererClass, |
| 89 | + ThrowableRendererInterface::class, |
| 90 | + )); |
| 91 | + } |
| 92 | + |
| 93 | + $new = clone $this; |
| 94 | + $new->renderers[$this->normalizeContentType($contentType)] = $rendererClass; |
| 95 | + return $new; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Returns a new instance without renderers by the specified content types. |
| 100 | + * |
| 101 | + * @param string[] $contentTypes The content types to remove associated renderers for. |
| 102 | + * If not specified, all renderers will be removed. |
| 103 | + */ |
| 104 | + public function withoutRenderers(string ...$contentTypes): self |
| 105 | + { |
| 106 | + $new = clone $this; |
| 107 | + |
| 108 | + if (count($contentTypes) === 0) { |
| 109 | + $new->renderers = []; |
| 110 | + return $new; |
| 111 | + } |
| 112 | + |
| 113 | + foreach ($contentTypes as $contentType) { |
| 114 | + unset($new->renderers[$this->normalizeContentType($contentType)]); |
| 115 | + } |
| 116 | + |
| 117 | + return $new; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Force content type to respond with regardless of request. |
| 122 | + * |
| 123 | + * @param string $contentType The content type to respond with regardless of request. |
| 124 | + */ |
| 125 | + public function forceContentType(string $contentType): self |
| 126 | + { |
| 127 | + $contentType = $this->normalizeContentType($contentType); |
| 128 | + |
| 129 | + if (!isset($this->renderers[$contentType])) { |
| 130 | + throw new InvalidArgumentException(sprintf('The renderer for %s is not set.', $contentType)); |
| 131 | + } |
| 132 | + |
| 133 | + $new = clone $this; |
| 134 | + $new->contentType = $contentType; |
| 135 | + return $new; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Returns the renderer by the specified content type, or null if the renderer was not set. |
| 140 | + * |
| 141 | + * @param string $contentType The content type associated with the renderer. |
| 142 | + */ |
| 143 | + private function getRenderer(string $contentType): ?ThrowableRendererInterface |
| 144 | + { |
| 145 | + if (isset($this->renderers[$contentType])) { |
| 146 | + /** @var ThrowableRendererInterface */ |
| 147 | + return $this->container->get($this->renderers[$contentType]); |
| 148 | + } |
| 149 | + |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Returns the priority content type from the accept request header. |
| 155 | + * |
| 156 | + * @return string The priority content type. |
| 157 | + */ |
| 158 | + private function getContentType(ServerRequestInterface $request): string |
| 159 | + { |
| 160 | + try { |
| 161 | + foreach (HeaderValueHelper::getSortedAcceptTypes($request->getHeader(Header::ACCEPT)) as $header) { |
| 162 | + if (array_key_exists($header, $this->renderers)) { |
| 163 | + return $header; |
| 164 | + } |
| 165 | + } |
| 166 | + } catch (InvalidArgumentException) { |
| 167 | + // The Accept header contains an invalid q factor. |
| 168 | + } |
| 169 | + |
| 170 | + return '*/*'; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Normalizes the content type. |
| 175 | + * |
| 176 | + * @param string $contentType The raw content type. |
| 177 | + * |
| 178 | + * @return string Normalized content type. |
| 179 | + */ |
| 180 | + private function normalizeContentType(string $contentType): string |
| 181 | + { |
| 182 | + if (!str_contains($contentType, '/')) { |
| 183 | + throw new InvalidArgumentException('Invalid content type.'); |
| 184 | + } |
| 185 | + |
| 186 | + return strtolower(trim($contentType)); |
| 187 | + } |
| 188 | +} |
0 commit comments