Total Complexity | 87 |
Total Lines | 604 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Output often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Output, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Output extends Message\Response |
||
29 | { |
||
30 | use FilePathCollectorTrait; |
||
31 | |||
32 | /** |
||
33 | * Output::$mimeType |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $mimeType = 'text/html'; |
||
38 | |||
39 | /** |
||
40 | * Output::$charset |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $charset = 'utf8'; |
||
45 | |||
46 | // ------------------------------------------------------------------------ |
||
47 | |||
48 | /** |
||
49 | * Output::__construct |
||
50 | */ |
||
51 | public function __construct() |
||
52 | { |
||
53 | parent::__construct(); |
||
54 | |||
55 | // Set Browser Views Directory |
||
56 | $this->setFileDirName('Views'); |
||
57 | $this->addFilePath(PATH_KERNEL); |
||
58 | |||
59 | // Autoload exception and error language file |
||
60 | language()->loadFile(['exception', 'error']); |
||
61 | |||
62 | // Register Kernel defined handler |
||
63 | $this->register(); |
||
64 | } |
||
65 | |||
66 | // ------------------------------------------------------------------------ |
||
67 | |||
68 | /** |
||
69 | * Output::register |
||
70 | * |
||
71 | * Register Kernel defined error, exception and shutdown handler. |
||
72 | * |
||
73 | * @return void |
||
74 | */ |
||
75 | final private function register() |
||
76 | { |
||
77 | set_error_handler([&$this, 'errorHandler']); |
||
78 | set_exception_handler([&$this, 'exceptionHandler']); |
||
79 | register_shutdown_function([&$this, 'shutdownHandler']); |
||
80 | } |
||
81 | |||
82 | // ------------------------------------------------------------------------ |
||
83 | |||
84 | /** |
||
85 | * Output::shutdownHandler |
||
86 | * |
||
87 | * Kernel defined shutdown handler function. |
||
88 | * |
||
89 | * @return void |
||
90 | * @throws \O2System\Spl\Exceptions\ErrorException |
||
91 | */ |
||
92 | public function shutdownHandler() |
||
93 | { |
||
94 | $lastError = error_get_last(); |
||
95 | |||
96 | if (is_array($lastError)) { |
||
|
|||
97 | $this->errorHandler( |
||
98 | $lastError[ 'type' ], |
||
99 | $lastError[ 'message' ], |
||
100 | $lastError[ 'file' ], |
||
101 | $lastError[ 'line' ] |
||
102 | ); |
||
103 | } |
||
104 | } |
||
105 | // -------------------------------------------------------------------- |
||
106 | |||
107 | /** |
||
108 | * Output::errorHandler |
||
109 | * |
||
110 | * Kernel defined error handler function. |
||
111 | * |
||
112 | * @param int $errorSeverity The first parameter, errno, contains the level of the error raised, as an integer. |
||
113 | * @param string $errorMessage The second parameter, errstr, contains the error message, as a string. |
||
114 | * @param string $errorFile The third parameter is optional, errfile, which contains the filename that the error |
||
115 | * was raised in, as a string. |
||
116 | * @param string $errorLine The fourth parameter is optional, errline, which contains the line number the error |
||
117 | * was raised at, as an integer. |
||
118 | * @param array $errorContext The fifth parameter is optional, errcontext, which is an array that points to the |
||
119 | * active symbol table at the point the error occurred. In other words, errcontext will |
||
120 | * contain an array of every variable that existed in the scope the error was triggered |
||
121 | * in. User error handler must not modify error context. |
||
122 | * |
||
123 | * @return bool If the function returns FALSE then the normal error handler continues. |
||
124 | * @throws ErrorException |
||
125 | */ |
||
126 | public function errorHandler($errorSeverity, $errorMessage, $errorFile, $errorLine, $errorContext = []) |
||
127 | { |
||
128 | $isFatalError = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $errorSeverity) === $errorSeverity); |
||
129 | |||
130 | // When the error is fatal the Kernel will throw it as an exception. |
||
131 | if ($isFatalError) { |
||
132 | throw new ErrorException($errorMessage, $errorSeverity, $errorLine, $errorLine, $errorContext); |
||
133 | } |
||
134 | |||
135 | // Should we ignore the error? We'll get the current error_reporting |
||
136 | // level and add its bits with the severity bits to find out. |
||
137 | if (($errorSeverity & error_reporting()) !== $errorSeverity) { |
||
138 | return false; |
||
139 | } |
||
140 | |||
141 | $error = new ErrorException($errorMessage, $errorSeverity, $errorFile, $errorLine, $errorContext); |
||
142 | |||
143 | // Logged the error |
||
144 | if(services()->has('logger')) { |
||
145 | logger()->error( |
||
146 | implode( |
||
147 | ' ', |
||
148 | [ |
||
149 | '[ ' . $error->getStringSeverity() . ' ] ', |
||
150 | $error->getMessage(), |
||
151 | $error->getFile() . ':' . $error->getLine(), |
||
152 | ] |
||
153 | ) |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | // Should we display the error? |
||
158 | if (str_ireplace(['off', 'none', 'no', 'false', 'null'], 0, ini_get('display_errors')) == 1) { |
||
159 | if (is_ajax()) { |
||
160 | $this->setContentType('application/json'); |
||
161 | $this->statusCode = 500; |
||
162 | $this->reasonPhrase = 'Internal Server Error'; |
||
163 | |||
164 | $this->send(implode( |
||
165 | ' ', |
||
166 | [ |
||
167 | '[ ' . $error->getStringSeverity() . ' ] ', |
||
168 | $error->getMessage(), |
||
169 | $error->getFile() . ':' . $error->getLine(), |
||
170 | ] |
||
171 | )); |
||
172 | exit(EXIT_ERROR); |
||
173 | } |
||
174 | |||
175 | $filePath = $this->getFilePath('error'); |
||
176 | |||
177 | ob_start(); |
||
178 | include $filePath; |
||
179 | $htmlOutput = ob_get_contents(); |
||
180 | ob_end_clean(); |
||
181 | |||
182 | echo $htmlOutput; |
||
183 | exit(EXIT_ERROR); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | // ------------------------------------------------------------------------ |
||
188 | |||
189 | /** |
||
190 | * Output::getFilePath |
||
191 | * |
||
192 | * @param string $filename |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getFilePath($filename) |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | |||
211 | // ------------------------------------------------------------------------ |
||
212 | |||
213 | /** |
||
214 | * Output::setContentType |
||
215 | * |
||
216 | * @param string $mimeType |
||
217 | * @param string $charset |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function setContentType($mimeType, $charset = null) |
||
222 | { |
||
223 | static $mimes = []; |
||
224 | |||
225 | if (empty($mimes)) { |
||
226 | $mimes = require(str_replace('Http', 'Config', __DIR__) . DIRECTORY_SEPARATOR . 'Mimes.php'); |
||
227 | } |
||
228 | |||
229 | if (strpos($mimeType, '/') === false) { |
||
230 | $extension = ltrim($mimeType, '.'); |
||
231 | // Is this extension supported? |
||
232 | if (isset($mimes[ $extension ])) { |
||
233 | $mimeType =& $mimes[ $extension ]; |
||
234 | if (is_array($mimeType)) { |
||
235 | $mimeType = current($mimeType); |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | |||
240 | $this->mimeType = $mimeType; |
||
241 | |||
242 | $this->addHeader( |
||
243 | 'Content-Type', |
||
244 | $mimeType |
||
245 | . (empty($charset) ? '' : '; charset=' . $charset) |
||
246 | ); |
||
247 | |||
248 | return $this; |
||
249 | } |
||
250 | |||
251 | // ------------------------------------------------------------------------ |
||
252 | |||
253 | /** |
||
254 | * Output::addHeader |
||
255 | * |
||
256 | * @param string $name |
||
257 | * @param string $value |
||
258 | * |
||
259 | * @return static |
||
260 | */ |
||
261 | public function addHeader($name, $value) |
||
266 | } |
||
267 | |||
268 | // ------------------------------------------------------------------------ |
||
269 | |||
270 | /** |
||
271 | * Output::send |
||
272 | * |
||
273 | * @param $data |
||
274 | * @param array $headers |
||
275 | */ |
||
276 | public function send($data = null, array $headers = []) |
||
277 | { |
||
278 | $statusCode = $this->statusCode; |
||
279 | $reasonPhrase = $this->reasonPhrase; |
||
280 | |||
281 | if (is_ajax()) { |
||
282 | $contentType = isset($_SERVER[ 'HTTP_X_REQUESTED_CONTENT_TYPE' ]) ? $_SERVER[ 'HTTP_X_REQUESTED_CONTENT_TYPE' ] : 'application/json'; |
||
283 | $this->setContentType($contentType); |
||
284 | } |
||
285 | |||
286 | $this->sendHeaders($headers); |
||
287 | |||
288 | $response = [ |
||
289 | 'status' => (int)$statusCode, |
||
290 | 'reason' => $reasonPhrase, |
||
291 | 'success' => $statusCode >= 200 && $statusCode < 300 ? true : false, |
||
292 | 'message' => isset($data[ 'message' ]) ? $data[ 'message' ] : '', |
||
293 | 'result' => [], |
||
294 | ]; |
||
295 | |||
296 | if ($data instanceof \ArrayIterator) { |
||
297 | $data = $data->getArrayCopy(); |
||
298 | } |
||
299 | |||
300 | if (is_array($data) and count($data)) { |
||
301 | if (is_numeric(key($data))) { |
||
302 | $response[ 'result' ] = $data; |
||
303 | } elseif (is_string(key($data))) { |
||
304 | if (array_key_exists('success', $data)) { |
||
305 | $response[ 'success' ] = $data[ 'success' ]; |
||
306 | unset($data[ 'success' ]); |
||
307 | } |
||
308 | |||
309 | if (array_key_exists('message', $data)) { |
||
310 | $response[ 'message' ] = $data[ 'message' ]; |
||
311 | unset($data[ 'message' ]); |
||
312 | } |
||
313 | |||
314 | if (array_key_exists('timestamp', $data)) { |
||
315 | $response[ 'timestamp' ] = $data[ 'timestamp' ]; |
||
316 | unset($data[ 'timestamp' ]); |
||
317 | } |
||
318 | |||
319 | if (array_key_exists('metadata', $data)) { |
||
320 | $response[ 'metadata' ] = $data[ 'metadata' ]; |
||
321 | unset($data[ 'metadata' ]); |
||
322 | } |
||
323 | |||
324 | if (array_key_exists('errors', $data)) { |
||
325 | $response[ 'errors' ] = $data[ 'errors' ]; |
||
326 | } |
||
327 | |||
328 | if (array_key_exists('error', $data)) { |
||
329 | $response[ 'error' ] = $data[ 'error' ]; |
||
330 | } |
||
331 | |||
332 | if (array_key_exists('data', $data)) { |
||
333 | if ($data[ 'data' ] instanceof \ArrayIterator) { |
||
334 | $data[ 'data' ] = $data[ 'data' ]->getArrayCopy(); |
||
335 | } |
||
336 | |||
337 | if (is_array($data[ 'data' ])) { |
||
338 | if (is_string(key($data[ 'data' ]))) { |
||
339 | $response[ 'result' ] = [$data[ 'data' ]]; |
||
340 | } elseif (is_numeric(key($data[ 'data' ]))) { |
||
341 | $response[ 'result' ] = $data[ 'data' ]; |
||
342 | } |
||
343 | } else { |
||
344 | $response[ 'result' ] = [$data[ 'data' ]]; |
||
345 | } |
||
346 | } |
||
347 | } |
||
348 | } |
||
349 | |||
350 | if (is_object($data)) { |
||
351 | if (isset($data->success)) { |
||
352 | $response[ 'success' ] = $data->success; |
||
353 | unset($data->success); |
||
354 | } |
||
355 | |||
356 | if (isset($data->message)) { |
||
357 | $response[ 'message' ] = $data->message; |
||
358 | unset($data->message); |
||
359 | } |
||
360 | |||
361 | if (isset($data->timestamp)) { |
||
362 | $response[ 'timestamp' ] = $data->timestamp; |
||
363 | unset($data->timestamp); |
||
364 | } |
||
365 | |||
366 | if (isset($data->metadata)) { |
||
367 | $response[ 'metadata' ] = $data->metadata; |
||
368 | unset($data->metadata); |
||
369 | } |
||
370 | |||
371 | if (isset($data->errors)) { |
||
372 | $response[ 'errors' ] = $data->errors; |
||
373 | unset($data->errors); |
||
374 | } |
||
375 | |||
376 | if (isset($data->error)) { |
||
377 | $response[ 'error' ] = $data->error; |
||
378 | unset($data->error); |
||
379 | } |
||
380 | |||
381 | if (isset($data->data)) { |
||
382 | if ($data->data instanceof \ArrayIterator) { |
||
383 | $data->data = $data->data->getArrayCopy(); |
||
384 | } |
||
385 | |||
386 | if (is_array($data->data)) { |
||
387 | if (is_string(key($data->data))) { |
||
388 | $response[ 'result' ] = [$data->data]; |
||
389 | } elseif (is_numeric(key($data->data))) { |
||
390 | $response[ 'result' ] = $data->data; |
||
391 | } |
||
392 | } else { |
||
393 | $response[ 'result' ] = [$data->data]; |
||
394 | } |
||
395 | } |
||
396 | } |
||
397 | |||
398 | if ($this->mimeType === 'application/json') { |
||
399 | if ( ! empty($data)) { |
||
400 | array_push($response[ 'result' ], $data); |
||
401 | } |
||
402 | |||
403 | echo json_encode($response, JSON_PRETTY_PRINT); |
||
404 | } elseif ($this->mimeType === 'application/xml') { |
||
405 | $xml = new \SimpleXMLElement('<?xml version="1.0"?><response></response>'); |
||
406 | $xml->addAttribute('status', $statusCode); |
||
407 | $xml->addAttribute('reason', $reasonPhrase); |
||
408 | |||
409 | if ( ! empty($data)) { |
||
410 | $this->arrayToXml(['message' => $data], $xml); |
||
411 | } |
||
412 | echo $xml->asXML(); |
||
413 | } else { |
||
414 | echo $data; |
||
415 | } |
||
416 | } |
||
417 | |||
418 | protected function sendHeaders(array $headers = []) |
||
419 | { |
||
420 | ini_set('expose_php', 0); |
||
421 | |||
422 | // collect headers that already sent |
||
423 | foreach (headers_list() as $header) { |
||
424 | $headerParts = explode(':', $header); |
||
425 | $headerParts = array_map('trim', $headerParts); |
||
426 | $headers[ $headerParts[ 0 ] ] = $headerParts[ 1 ]; |
||
427 | header_remove($header[ 0 ]); |
||
428 | } |
||
429 | |||
430 | if (count($headers)) { |
||
431 | $this->headers = array_merge($this->headers, $headers); |
||
432 | } |
||
433 | |||
434 | if ($this->statusCode === 204) { |
||
435 | $this->statusCode = 200; |
||
436 | $this->reasonPhrase = 'OK'; |
||
437 | } |
||
438 | |||
439 | $this->sendHeaderStatus($this->statusCode, $this->reasonPhrase, $this->protocol); |
||
440 | |||
441 | foreach ($this->headers as $name => $value) { |
||
442 | $this->sendHeader($name, $value); |
||
443 | } |
||
444 | } |
||
445 | |||
446 | // ------------------------------------------------------------------------ |
||
447 | |||
448 | /** |
||
449 | * Output::sendHeaderStatus |
||
450 | * |
||
451 | * @param int $statusCode |
||
452 | * @param string $reasonPhrase |
||
453 | * @param string $protocol |
||
454 | * |
||
455 | * @return $this |
||
456 | */ |
||
457 | public function sendHeaderStatus($statusCode, $reasonPhrase, $protocol = '1.1') |
||
462 | } |
||
463 | |||
464 | // ------------------------------------------------------------------------ |
||
465 | |||
466 | /** |
||
467 | * Output::sendHeader |
||
468 | * |
||
469 | * @param string $name |
||
470 | * @param string $value |
||
471 | * @param bool $replace |
||
472 | * |
||
473 | * @return static |
||
474 | */ |
||
475 | public function sendHeader($name, $value, $replace = true) |
||
476 | { |
||
477 | @header($name . ': ' . trim($value), $replace); |
||
478 | |||
479 | return $this; |
||
480 | } |
||
481 | |||
482 | // ------------------------------------------------------------------------ |
||
483 | |||
484 | /** |
||
485 | * Output::arrayToXml |
||
486 | * |
||
487 | * @param array $data |
||
488 | * @param \SimpleXMLElement $xml |
||
489 | */ |
||
490 | protected function arrayToXml(array $data, \SimpleXMLElement &$xml) |
||
491 | { |
||
492 | foreach ($data as $key => $value) { |
||
493 | if (is_numeric($key)) { |
||
494 | $key = 'item' . $key; //dealing with <0/>..<n/> issues |
||
495 | } |
||
496 | if (is_array($value)) { |
||
497 | $subnode = $xml->addChild($key); |
||
498 | $this->arrayToXml($value, $subnode); |
||
499 | } else { |
||
500 | $xml->addChild("$key", htmlspecialchars("$value")); |
||
501 | } |
||
502 | } |
||
503 | } |
||
504 | |||
505 | // ------------------------------------------------------------------------ |
||
506 | |||
507 | /** |
||
508 | * Output::sendPayload |
||
509 | * |
||
510 | * @param array $data |
||
511 | * @param string|null $mimeType |
||
512 | */ |
||
513 | public function sendPayload(array $data, $mimeType = null) |
||
528 | } |
||
529 | |||
530 | // ------------------------------------------------------------------------ |
||
531 | |||
532 | /** |
||
533 | * Output::exceptionHandler |
||
534 | * |
||
535 | * Kernel defined exception handler function. |
||
536 | * |
||
537 | * @param \Exception|\Error|\O2System\Spl\Exceptions\Abstracts\AbstractException $exception Throwable exception. |
||
538 | * |
||
539 | * @return void |
||
540 | */ |
||
541 | public function exceptionHandler($exception) |
||
542 | { |
||
543 | if (is_ajax()) { |
||
544 | $this->statusCode = 500; |
||
545 | $this->reasonPhrase = 'Internal Server Error'; |
||
546 | |||
547 | $this->send(implode( |
||
548 | ' ', |
||
549 | [ |
||
550 | ($exception->getCode() != 0 ? '[ ' . $exception->getCode() . ']' : ''), |
||
551 | $exception->getMessage(), |
||
552 | $exception->getFile() . ':' . $exception->getLine(), |
||
553 | ] |
||
554 | )); |
||
555 | } elseif ($exception instanceof AbstractException) { |
||
556 | |||
557 | ob_start(); |
||
558 | include $this->getFilePath('exception'); |
||
559 | $htmlOutput = ob_get_contents(); |
||
560 | ob_end_clean(); |
||
561 | |||
562 | echo $htmlOutput; |
||
563 | exit(EXIT_ERROR); |
||
564 | } elseif ($exception instanceof \Exception || $exception instanceof \Error) { |
||
565 | |||
566 | $exceptionClassName = get_class_name($exception); |
||
567 | $header = language()->getLine('E_HEADER_' . $exceptionClassName); |
||
568 | $description = language()->getLine('E_DESCRIPTION_' . $exceptionClassName); |
||
569 | $trace = new Trace($exception->getTrace()); |
||
570 | |||
571 | ob_start(); |
||
572 | include $this->getFilePath('exception-spl'); |
||
573 | $htmlOutput = ob_get_contents(); |
||
574 | ob_end_clean(); |
||
575 | |||
576 | echo $htmlOutput; |
||
577 | exit(EXIT_ERROR); |
||
578 | } |
||
579 | } |
||
580 | |||
581 | // ------------------------------------------------------------------------ |
||
582 | |||
583 | /** |
||
584 | * Output::sendError |
||
585 | * |
||
586 | * @param int $code |
||
587 | * @param null|array|string $vars |
||
588 | * @param array $headers |
||
589 | */ |
||
590 | public function sendError($code = 204, $vars = null, $headers = []) |
||
632 | } |
||
633 | } |
||
634 |