Final Class Yiisoft\ErrorHandler\Factory\ThrowableResponseFactory
| Inheritance | Yiisoft\ErrorHandler\Factory\ThrowableResponseFactory |
|---|---|
| Implements | Yiisoft\ErrorHandler\ThrowableResponseFactoryInterface |
| Deprecated since version | Use {@see \Yiisoft\ErrorHandler\ThrowableResponseFactory} instead. |
ThrowableResponseFactory renders Throwable object
and produces a response according to the content type provided by the client.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ErrorHandler\Factory\ThrowableResponseFactory | |
| create() | Yiisoft\ErrorHandler\Factory\ThrowableResponseFactory | |
| forceContentType() | Force content type to respond with regardless of request. | Yiisoft\ErrorHandler\Factory\ThrowableResponseFactory |
| withRenderer() | Returns a new instance with the specified content type and renderer class. | Yiisoft\ErrorHandler\Factory\ThrowableResponseFactory |
| withoutRenderers() | Returns a new instance without renderers by the specified content types. | Yiisoft\ErrorHandler\Factory\ThrowableResponseFactory |
Method Details
| public mixed __construct ( \Psr\Http\Message\ResponseFactoryInterface $responseFactory, Yiisoft\ErrorHandler\ErrorHandler $errorHandler, \Psr\Container\ContainerInterface $container, Yiisoft\ErrorHandler\HeadersProvider|null $headersProvider = null ) | ||
| $responseFactory | \Psr\Http\Message\ResponseFactoryInterface | |
| $errorHandler | Yiisoft\ErrorHandler\ErrorHandler | |
| $container | \Psr\Container\ContainerInterface | |
| $headersProvider | Yiisoft\ErrorHandler\HeadersProvider|null | |
public function __construct(
private readonly ResponseFactoryInterface $responseFactory,
private readonly ErrorHandler $errorHandler,
private readonly ContainerInterface $container,
?HeadersProvider $headersProvider = null,
) {
$this->headersProvider = $headersProvider ?? new HeadersProvider();
}
| public \Psr\Http\Message\ResponseInterface create ( Throwable $throwable, \Psr\Http\Message\ServerRequestInterface $request ) | ||
| $throwable | Throwable | |
| $request | \Psr\Http\Message\ServerRequestInterface | |
public function create(Throwable $throwable, ServerRequestInterface $request): ResponseInterface
{
$contentType = $this->contentType ?? $this->getContentType($request);
$renderer = $request->getMethod() === Method::HEAD ? new HeaderRenderer() : $this->getRenderer($contentType);
$data = $this->errorHandler->handle($throwable, $renderer, $request);
$response = $this->responseFactory->createResponse(Status::INTERNAL_SERVER_ERROR);
foreach ($this->headersProvider->getAll() as $name => $value) {
$response = $response->withHeader($name, $value);
}
return $data->addToResponse($response->withHeader(Header::CONTENT_TYPE, $contentType));
}
Force content type to respond with regardless of request.
| public self forceContentType ( string $contentType ) | ||
| $contentType | string |
The content type to respond with regardless of request. |
public function forceContentType(string $contentType): self
{
$contentType = $this->normalizeContentType($contentType);
if (!isset($this->renderers[$contentType])) {
throw new InvalidArgumentException(sprintf('The renderer for %s is not set.', $contentType));
}
$new = clone $this;
$new->contentType = $contentType;
return $new;
}
Returns a new instance with the specified content type and renderer class.
| public self withRenderer ( string $contentType, string $rendererClass ) | ||
| $contentType | string |
The content type to add associated renderers for. |
| $rendererClass | string |
The classname implementing the {@see \Yiisoft\ErrorHandler\ThrowableRendererInterface}. |
public function withRenderer(string $contentType, string $rendererClass): self
{
if (!is_subclass_of($rendererClass, ThrowableRendererInterface::class)) {
throw new InvalidArgumentException(sprintf(
'Class "%s" does not implement "%s".',
$rendererClass,
ThrowableRendererInterface::class,
));
}
$new = clone $this;
$new->renderers[$this->normalizeContentType($contentType)] = $rendererClass;
return $new;
}
Returns a new instance without renderers by the specified content types.
| public self withoutRenderers ( string[] $contentTypes ) | ||
| $contentTypes | string[] |
The content types to remove associated renderers for. If not specified, all renderers will be removed. |
public function withoutRenderers(string ...$contentTypes): self
{
$new = clone $this;
if (count($contentTypes) === 0) {
$new->renderers = [];
return $new;
}
foreach ($contentTypes as $contentType) {
unset($new->renderers[$this->normalizeContentType($contentType)]);
}
return $new;
}
Signup or Login in order to comment.