|
| 1 | +<?php |
| 2 | +namespace Codeception\Lib\Connector\Laravel7; |
| 3 | + |
| 4 | +use Throwable; |
| 5 | +use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class ExceptionHandlerDecorator |
| 9 | + * |
| 10 | + * @package Codeception\Lib\Connector\Laravel7 |
| 11 | + */ |
| 12 | +class ExceptionHandlerDecorator implements ExceptionHandlerContract |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var ExceptionHandlerContract |
| 16 | + */ |
| 17 | + private $laravelExceptionHandler; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var boolean |
| 21 | + */ |
| 22 | + private $exceptionHandlingDisabled = true; |
| 23 | + |
| 24 | + /** |
| 25 | + * ExceptionHandlerDecorator constructor. |
| 26 | + * |
| 27 | + * @param object $laravelExceptionHandler |
| 28 | + */ |
| 29 | + public function __construct($laravelExceptionHandler) |
| 30 | + { |
| 31 | + $this->laravelExceptionHandler = $laravelExceptionHandler; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param boolean $exceptionHandlingDisabled |
| 36 | + */ |
| 37 | + public function exceptionHandlingDisabled($exceptionHandlingDisabled) |
| 38 | + { |
| 39 | + $this->exceptionHandlingDisabled = $exceptionHandlingDisabled; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Report or log an exception. |
| 44 | + * |
| 45 | + * @param \Throwable $e |
| 46 | + * @return void |
| 47 | + */ |
| 48 | + public function report(Throwable $e) |
| 49 | + { |
| 50 | + $this->laravelExceptionHandler->report($e); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Determine if the exception should be reported. |
| 55 | + * |
| 56 | + * @param \Throwable $e |
| 57 | + * @return bool |
| 58 | + */ |
| 59 | + public function shouldReport(Throwable $e) |
| 60 | + { |
| 61 | + return $this->exceptionHandlingDisabled; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param $request |
| 66 | + * @param Throwable $e |
| 67 | + * @return \Symfony\Component\HttpFoundation\Response |
| 68 | + * @throws Throwable |
| 69 | + */ |
| 70 | + public function render($request, Throwable $e) |
| 71 | + { |
| 72 | + $response = $this->laravelExceptionHandler->render($request, $e); |
| 73 | + |
| 74 | + if ($this->exceptionHandlingDisabled && $this->isSymfonyExceptionHandlerOutput($response->getContent())) { |
| 75 | + // If content was generated by the \Symfony\Component\Debug\ExceptionHandler class |
| 76 | + // the Laravel application could not handle the exception, |
| 77 | + // so re-throw this exception if the Codeception user disabled Laravel's exception handling. |
| 78 | + throw $e; |
| 79 | + } |
| 80 | + |
| 81 | + return $response; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Check if the response content is HTML output of the Symfony exception handler class. |
| 86 | + * |
| 87 | + * @param string $content |
| 88 | + * @return bool |
| 89 | + */ |
| 90 | + private function isSymfonyExceptionHandlerOutput($content) |
| 91 | + { |
| 92 | + return strpos($content, '<div id="sf-resetcontent" class="sf-reset">') !== false || |
| 93 | + strpos($content, '<div class="exception-summary">') !== false; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Render an exception to the console. |
| 98 | + * |
| 99 | + * @param \Symfony\Component\Console\Output\OutputInterface $output |
| 100 | + * @param \Throwable $e |
| 101 | + * @return void |
| 102 | + */ |
| 103 | + public function renderForConsole($output, Throwable $e) |
| 104 | + { |
| 105 | + $this->laravelExceptionHandler->renderForConsole($output, $e); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param string $method |
| 110 | + * @param array $args |
| 111 | + * @return mixed |
| 112 | + */ |
| 113 | + public function __call($method, $args) |
| 114 | + { |
| 115 | + return call_user_func_array([$this->laravelExceptionHandler, $method], $args); |
| 116 | + } |
| 117 | +} |
0 commit comments