| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpKernel\Controller;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
|
|
|
15 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
16 |
use Symfony\Component\HttpFoundation\Response;
|
|
|
17 |
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
18 |
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Renders error or exception pages from a given FlattenException.
|
|
|
22 |
*
|
|
|
23 |
* @author Yonel Ceruto <yonelceruto@gmail.com>
|
|
|
24 |
* @author Matthias Pigulla <mp@webfactory.de>
|
|
|
25 |
*/
|
|
|
26 |
class ErrorController
|
|
|
27 |
{
|
|
|
28 |
private HttpKernelInterface $kernel;
|
|
|
29 |
private string|object|array|null $controller;
|
|
|
30 |
private ErrorRendererInterface $errorRenderer;
|
|
|
31 |
|
|
|
32 |
public function __construct(HttpKernelInterface $kernel, string|object|array|null $controller, ErrorRendererInterface $errorRenderer)
|
|
|
33 |
{
|
|
|
34 |
$this->kernel = $kernel;
|
|
|
35 |
$this->controller = $controller;
|
|
|
36 |
$this->errorRenderer = $errorRenderer;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
public function __invoke(\Throwable $exception): Response
|
|
|
40 |
{
|
|
|
41 |
$exception = $this->errorRenderer->render($exception);
|
|
|
42 |
|
|
|
43 |
return new Response($exception->getAsString(), $exception->getStatusCode(), $exception->getHeaders());
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function preview(Request $request, int $code): Response
|
|
|
47 |
{
|
|
|
48 |
/*
|
|
|
49 |
* This Request mimics the parameters set by
|
|
|
50 |
* \Symfony\Component\HttpKernel\EventListener\ErrorListener::duplicateRequest, with
|
|
|
51 |
* the additional "showException" flag.
|
|
|
52 |
*/
|
|
|
53 |
$subRequest = $request->duplicate(null, null, [
|
|
|
54 |
'_controller' => $this->controller,
|
|
|
55 |
'exception' => new HttpException($code, 'This is a sample exception.'),
|
|
|
56 |
'logger' => null,
|
|
|
57 |
'showException' => false,
|
|
|
58 |
]);
|
|
|
59 |
|
|
|
60 |
return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
|
|
|
61 |
}
|
|
|
62 |
}
|