| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Class ErrorController | ErrorController.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @package Faulancer\AbstractController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author Florian Knapp <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace Faulancer\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Faulancer\Exception\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Faulancer\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Class ErrorAbstractController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class ErrorController extends AbstractController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * ErrorController constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param Request   $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param \Exception $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function __construct(Request $request, $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         parent::__construct($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->exception = $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return \Faulancer\Http\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function displayError() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ob_end_clean(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if (getenv('APPLICATION_ENV') || defined('APPLICATION_ENV') && APPLICATION_ENV === 'development') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             return $this->renderDebugPage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         return $this->renderNotFoundPage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @return \Faulancer\Http\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private function renderDebugPage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->getView()->addStylesheet('/core/css/main.css'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->getView()->addScript('/core/js/namespace.js'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->getView()->addScript('/core/js/engine.js'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->getView()->setTemplatePath(__DIR__ . '/../../template'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $raiser = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'function'=> !empty($this->exception->getTrace()[0]['function']) ? $this->exception->getTrace()[0]['function'] : 'unknown', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'message' => $this->exception->getMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'type'    => $this->exception->getCode(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'file'    => $this->exception->getFile(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'line'    => $this->exception->getLine() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $trace  = $this->exception->getTrace(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if (isset($trace[0]['line']) && $trace[0]['line'] !== $raiser['line']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             array_unshift($trace, $raiser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             array_shift($trace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             array_unshift($trace, $raiser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return $this->render('/debug.phtml', ['exception' => $this->exception, 'trace' => $trace]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |     private function renderNotFoundPage() | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 88 |  |  | } |