1 | <?php |
||
30 | class ExceptionController |
||
31 | { |
||
32 | private $exceptionWrapperHandler; |
||
33 | private $formatNegotiator; |
||
34 | private $viewHandler; |
||
35 | private $templating; |
||
36 | private $exceptionCodes; |
||
37 | private $exceptionMessages; |
||
38 | private $showException; |
||
39 | |||
40 | 4 | public function __construct( |
|
57 | |||
58 | /** |
||
59 | * Creates a new ExceptionWrapper instance that can be overwritten by a custom |
||
60 | * ExceptionController class. |
||
61 | * |
||
62 | * @param array $parameters Template parameters |
||
63 | * |
||
64 | * @return ExceptionWrapper ExceptionWrapper instance |
||
65 | */ |
||
66 | 4 | protected function createExceptionWrapper(array $parameters) |
|
70 | |||
71 | /** |
||
72 | * Converts an Exception to a Response. |
||
73 | * |
||
74 | * @param Request $request |
||
75 | * @param FlattenException $exception |
||
76 | * @param DebugLoggerInterface $logger |
||
77 | * |
||
78 | * @throws \InvalidArgumentException |
||
79 | * |
||
80 | * @return Response |
||
81 | */ |
||
82 | 4 | public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) |
|
124 | |||
125 | /** |
||
126 | * Returns a Response Object with content type text/plain. |
||
127 | * |
||
128 | * @param string $content |
||
129 | * @param int $status |
||
130 | * @param array $headers |
||
131 | * |
||
132 | * @return Response |
||
133 | */ |
||
134 | private function createPlainResponse($content, $status, $headers) |
||
135 | { |
||
136 | $headers['content-type'] = 'text/plain'; |
||
137 | |||
138 | return new Response($content, $status, $headers); |
||
139 | } |
||
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 | 4 | private function getAndCleanOutputBuffering($startObLevel) |
|
150 | { |
||
151 | 4 | if (ob_get_level() <= $startObLevel) { |
|
152 | 4 | return ''; |
|
153 | } |
||
154 | Response::closeOutputBuffers($startObLevel + 1, true); |
||
155 | |||
156 | return ob_get_clean(); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Extracts the exception message. |
||
161 | * |
||
162 | * @param FlattenException $exception |
||
163 | * @param array $exceptionMap |
||
164 | * |
||
165 | * @return int|false |
||
166 | */ |
||
167 | 4 | private function isSubclassOf($exception, $exceptionMap) |
|
186 | |||
187 | /** |
||
188 | * Extracts the exception message. |
||
189 | * |
||
190 | * @param FlattenException $exception |
||
191 | * |
||
192 | * @return string Message |
||
193 | */ |
||
194 | 4 | protected function getExceptionMessage($exception) |
|
206 | |||
207 | /** |
||
208 | * Determines the status code to use for the response. |
||
209 | * |
||
210 | * @param FlattenException $exception |
||
211 | * |
||
212 | * @return int |
||
213 | */ |
||
214 | 4 | protected function getStatusCode($exception) |
|
220 | |||
221 | /** |
||
222 | * Determines the format to use for the response. |
||
223 | * |
||
224 | * @param Request $request |
||
225 | * @param string $format |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | 4 | protected function getFormat(Request $request, $format) |
|
230 | { |
||
231 | try { |
||
232 | 4 | $accept = $this->formatNegotiator->getBest('', []); |
|
233 | 4 | if ($accept) { |
|
234 | $format = $request->getFormat($accept->getType()); |
||
235 | } |
||
236 | 4 | $request->attributes->set('_format', $format); |
|
237 | 4 | } catch (StopFormatListenerException $e) { |
|
238 | $format = $request->getRequestFormat(); |
||
239 | } |
||
240 | |||
241 | 4 | return $format; |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * Determines the parameters to pass to the view layer. |
||
246 | * |
||
247 | * Overwrite it in a custom ExceptionController class to add additionally parameters |
||
248 | * that should be passed to the view layer. |
||
249 | * |
||
250 | * @param ViewHandlerInterface $viewHandler |
||
251 | * @param string $currentContent |
||
252 | * @param int $code |
||
253 | * @param FlattenException $exception |
||
254 | * @param DebugLoggerInterface $logger |
||
255 | * @param string $format |
||
256 | * |
||
257 | * @return array |
||
258 | */ |
||
259 | 4 | protected function getParameters(ViewHandlerInterface $viewHandler, $currentContent, $code, $exception, DebugLoggerInterface $logger = null, $format = 'html') |
|
260 | { |
||
261 | $parameters = [ |
||
262 | 4 | 'status' => 'error', |
|
263 | 4 | 'status_code' => $code, |
|
264 | 4 | 'status_text' => array_key_exists($code, Response::$statusTexts) ? Response::$statusTexts[$code] : 'error', |
|
265 | 4 | 'currentContent' => $currentContent, |
|
266 | 4 | 'message' => $this->getExceptionMessage($exception), |
|
267 | 4 | 'exception' => $exception, |
|
268 | 4 | ]; |
|
269 | |||
270 | 4 | if ($viewHandler->isFormatTemplating($format)) { |
|
271 | $parameters['logger'] = $logger; |
||
272 | } |
||
273 | |||
274 | 4 | return $parameters; |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * Finds the template for the given format and status code. |
||
279 | * |
||
280 | * Note this method needs to be overridden in case another |
||
281 | * engine than Twig should be supported; |
||
282 | * |
||
283 | * This code is inspired by TwigBundle and should be synchronized on a regular basis |
||
284 | * see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php |
||
285 | * |
||
286 | * @param Request $request |
||
287 | * @param string $format |
||
288 | * @param int $statusCode |
||
289 | * @param bool $showException |
||
290 | * |
||
291 | * @return TemplateReference |
||
292 | */ |
||
293 | private function findTemplate(Request $request, $format, $statusCode, $showException) |
||
319 | } |
||
320 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.