1 | <?php |
||
28 | class ExceptionController |
||
29 | { |
||
30 | private $exceptionWrapperHandler; |
||
31 | private $formatNegotiator; |
||
32 | private $viewHandler; |
||
33 | private $exceptionCodes; |
||
34 | private $exceptionMessages; |
||
35 | private $showException; |
||
36 | |||
37 | 4 | public function __construct( |
|
38 | ExceptionWrapperHandlerInterface $exceptionWrapperHandler, |
||
39 | FormatNegotiator $formatNegotiator, |
||
40 | ViewHandlerInterface $viewHandler, |
||
41 | array $exceptionCodes, |
||
42 | array $exceptionMessages, |
||
43 | $showException |
||
44 | ) { |
||
45 | 4 | $this->exceptionWrapperHandler = $exceptionWrapperHandler; |
|
46 | 4 | $this->formatNegotiator = $formatNegotiator; |
|
47 | 4 | $this->viewHandler = $viewHandler; |
|
48 | 4 | $this->exceptionCodes = $exceptionCodes; |
|
49 | 4 | $this->exceptionMessages = $exceptionMessages; |
|
50 | 4 | $this->showException = $showException; |
|
51 | 4 | } |
|
52 | |||
53 | /** |
||
54 | * @return ViewHandlerInterface |
||
55 | */ |
||
56 | protected function getViewHandler() |
||
60 | |||
61 | /** |
||
62 | * Converts an Exception to a Response. |
||
63 | * |
||
64 | * @param Request $request |
||
65 | * @param FlattenException $exception |
||
66 | * @param DebugLoggerInterface $logger |
||
67 | * |
||
68 | * @throws \InvalidArgumentException |
||
69 | * |
||
70 | * @return Response |
||
71 | */ |
||
72 | 4 | public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) |
|
73 | { |
||
74 | try { |
||
75 | 4 | $format = $this->getFormat($request, $request->getRequestFormat()); |
|
76 | 4 | } catch (\Exception $e) { |
|
77 | $format = null; |
||
78 | } |
||
79 | 4 | if (null === $format) { |
|
80 | $message = 'No matching accepted Response format could be determined, while handling: '; |
||
81 | $message .= $this->getExceptionMessage($exception); |
||
82 | |||
83 | return $this->createPlainResponse($message, Response::HTTP_NOT_ACCEPTABLE, $exception->getHeaders()); |
||
84 | } |
||
85 | |||
86 | 4 | $currentContent = $this->getAndCleanOutputBuffering( |
|
87 | 4 | $request->headers->get('X-Php-Ob-Level', -1) |
|
88 | 4 | ); |
|
89 | 4 | $code = $this->getStatusCode($exception); |
|
90 | 4 | $parameters = $this->getParameters($currentContent, $code, $exception, $logger, $format); |
|
91 | 4 | $showException = $request->attributes->get('showException', $this->showException); |
|
92 | |||
93 | try { |
||
94 | 4 | $view = $this->createView($format, $exception, $code, $parameters, $request, $showException); |
|
95 | |||
96 | 4 | $response = $this->viewHandler->handle($view); |
|
97 | 4 | } catch (\Exception $e) { |
|
98 | $message = 'An Exception was thrown while handling: '; |
||
99 | $message .= $this->getExceptionMessage($exception); |
||
100 | $response = $this->createPlainResponse($message, Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getHeaders()); |
||
101 | } |
||
102 | |||
103 | 4 | return $response; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * Returns a Response Object with content type text/plain. |
||
108 | * |
||
109 | * @param string $content |
||
110 | * @param int $status |
||
111 | * @param array $headers |
||
112 | * |
||
113 | * @return Response |
||
114 | */ |
||
115 | private function createPlainResponse($content, $status, $headers) |
||
116 | { |
||
117 | $headers['content-type'] = 'text/plain'; |
||
118 | |||
119 | return new Response($content, $status, $headers); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Creates a new ExceptionWrapper instance that can be overwritten by a custom |
||
124 | * ExceptionController class. |
||
125 | * |
||
126 | * @param array $parameters output data |
||
127 | * |
||
128 | * @return ExceptionWrapper ExceptionWrapper instance |
||
129 | */ |
||
130 | 4 | protected function createExceptionWrapper(array $parameters) |
|
134 | |||
135 | /** |
||
136 | * @param string $format |
||
137 | * @param FlattenException $exception |
||
138 | * @param int $code |
||
139 | * @param array $parameters |
||
140 | * @param Request $request |
||
141 | * @param bool $showException |
||
142 | * |
||
143 | * @return View |
||
144 | */ |
||
145 | 4 | protected function createView($format, FlattenException $exception, $code, $parameters, Request $request, $showException) |
|
153 | |||
154 | /** |
||
155 | * Gets and cleans any content that was already outputted. |
||
156 | * |
||
157 | * This code comes from Symfony and should be synchronized on a regular basis |
||
158 | * see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 4 | private function getAndCleanOutputBuffering($startObLevel) |
|
171 | |||
172 | /** |
||
173 | * Extracts the exception message. |
||
174 | * |
||
175 | * @param FlattenException $exception |
||
176 | * @param array $exceptionMap |
||
177 | * |
||
178 | * @return int|false |
||
179 | */ |
||
180 | 4 | private function isSubclassOf($exception, $exceptionMap) |
|
193 | |||
194 | /** |
||
195 | * Extracts the exception message. |
||
196 | * |
||
197 | * @param FlattenException $exception |
||
198 | * |
||
199 | * @return string Message |
||
200 | */ |
||
201 | 4 | protected function getExceptionMessage($exception) |
|
213 | |||
214 | /** |
||
215 | * Determines the status code to use for the response. |
||
216 | * |
||
217 | * @param FlattenException $exception |
||
218 | * |
||
219 | * @return int |
||
220 | */ |
||
221 | 4 | protected function getStatusCode($exception) |
|
227 | |||
228 | /** |
||
229 | * Determines the format to use for the response. |
||
230 | * |
||
231 | * @param Request $request |
||
232 | * @param string $format |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | 4 | protected function getFormat(Request $request, $format) |
|
250 | |||
251 | /** |
||
252 | * Determines the parameters to pass to the view layer. |
||
253 | * |
||
254 | * Overwrite it in a custom ExceptionController class to add additionally parameters |
||
255 | * that should be passed to the view layer. |
||
256 | * |
||
257 | * @param string $currentContent |
||
258 | * @param int $code |
||
259 | * @param FlattenException $exception |
||
260 | * @param DebugLoggerInterface $logger |
||
261 | * @param string $format |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | 4 | protected function getParameters($currentContent, $code, $exception, DebugLoggerInterface $logger = null, $format = 'html') |
|
276 | } |
||
277 |