1 | <?php |
||
26 | class ExceptionController |
||
27 | { |
||
28 | /** |
||
29 | * @var ViewHandlerInterface |
||
30 | */ |
||
31 | private $viewHandler; |
||
32 | |||
33 | /** |
||
34 | * @var ExceptionValueMap |
||
35 | */ |
||
36 | private $exceptionCodes; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | private $showException; |
||
42 | |||
43 | 9 | public function __construct( |
|
44 | ViewHandlerInterface $viewHandler, |
||
45 | ExceptionValueMap $exceptionCodes, |
||
46 | $showException |
||
47 | ) { |
||
48 | 9 | $this->viewHandler = $viewHandler; |
|
49 | 9 | $this->exceptionCodes = $exceptionCodes; |
|
50 | 9 | $this->showException = $showException; |
|
51 | 9 | } |
|
52 | |||
53 | /** |
||
54 | * Converts an Exception to a Response. |
||
55 | * |
||
56 | * @param Request $request |
||
57 | * @param \Exception|\Throwable $exception |
||
58 | * @param DebugLoggerInterface|null $logger |
||
59 | * |
||
60 | * @throws \InvalidArgumentException |
||
61 | * |
||
62 | * @return Response |
||
63 | */ |
||
64 | 9 | public function showAction(Request $request, $exception, DebugLoggerInterface $logger = null) |
|
65 | { |
||
66 | 9 | $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); |
|
67 | 9 | $code = $this->getStatusCode($exception); |
|
68 | 9 | $templateData = $this->getTemplateData($currentContent, $code, $exception, $logger); |
|
69 | |||
70 | 9 | $view = $this->createView($exception, $code, $templateData, $request, $this->showException); |
|
71 | 9 | $response = $this->viewHandler->handle($view); |
|
72 | |||
73 | 9 | return $response; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param \Exception $exception |
||
78 | * @param int $code |
||
79 | * @param array $templateData |
||
80 | * @param Request $request |
||
81 | * @param bool $showException |
||
82 | * |
||
83 | * @return View |
||
84 | */ |
||
85 | 9 | protected function createView(\Exception $exception, $code, array $templateData, Request $request, $showException) |
|
86 | { |
||
87 | 9 | $view = new View($exception, $code, $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : []); |
|
88 | 9 | $view->setTemplateVar('raw_exception'); |
|
89 | 9 | $view->setTemplateData($templateData); |
|
90 | |||
91 | 9 | return $view; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Determines the status code to use for the response. |
||
96 | * |
||
97 | * @param \Exception $exception |
||
98 | * |
||
99 | * @return int |
||
100 | */ |
||
101 | 9 | protected function getStatusCode(\Exception $exception) |
|
102 | { |
||
103 | // If matched |
||
104 | 9 | if ($statusCode = $this->exceptionCodes->resolveException($exception)) { |
|
105 | return $statusCode; |
||
106 | } |
||
107 | |||
108 | // Otherwise, default |
||
109 | 9 | if ($exception instanceof HttpExceptionInterface) { |
|
110 | 1 | return $exception->getStatusCode(); |
|
111 | } |
||
112 | |||
113 | 8 | return 500; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Determines the parameters to pass to the view layer. |
||
118 | * |
||
119 | * Overwrite it in a custom ExceptionController class to add additionally parameters |
||
120 | * that should be passed to the view layer. |
||
121 | * |
||
122 | * @param string $currentContent |
||
123 | * @param int $code |
||
124 | * @param \Exception $exception |
||
125 | * @param DebugLoggerInterface $logger |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | 9 | private function getTemplateData($currentContent, $code, \Exception $exception, DebugLoggerInterface $logger = null) |
|
140 | |||
141 | /** |
||
142 | * Gets and cleans any content that was already outputted. |
||
143 | * |
||
144 | * This code comes from Symfony and should be synchronized on a regular basis |
||
145 | * see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 9 | private function getAndCleanOutputBuffering($startObLevel) |
|
158 | } |
||
159 |