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( |
|
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) |
|
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) |
||
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) |
|
199 | |||
200 | /** |
||
201 | * Extracts the exception message. |
||
202 | * |
||
203 | * @param FlattenException $exception |
||
204 | * |
||
205 | * @return string Message |
||
206 | */ |
||
207 | 4 | protected function getExceptionMessage($exception) |
|
219 | |||
220 | /** |
||
221 | * Determines the status code to use for the response. |
||
222 | * |
||
223 | * @param FlattenException $exception |
||
224 | * |
||
225 | * @return int |
||
226 | */ |
||
227 | 4 | protected function getStatusCode($exception) |
|
233 | |||
234 | /** |
||
235 | * Determines the format to use for the response. |
||
236 | * |
||
237 | * @param Request $request |
||
238 | * @param string $format |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | 4 | protected function getFormat(Request $request, $format) |
|
256 | |||
257 | /** |
||
258 | * Determines the parameters to pass to the view layer. |
||
259 | * |
||
260 | * Overwrite it in a custom ExceptionController class to add additionally parameters |
||
261 | * that should be passed to the view layer. |
||
262 | * |
||
263 | * @param string $currentContent |
||
264 | * @param int $code |
||
265 | * @param FlattenException $exception |
||
266 | * @param DebugLoggerInterface $logger |
||
267 | * @param string $format |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | 4 | protected function getParameters($currentContent, $code, $exception, DebugLoggerInterface $logger = null, $format = 'html') |
|
282 | } |
||
283 |
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.