| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Lenevor Framework | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This source file is subject to the new BSD license that is bundled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * with this package in the file license.md. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * It is also available through the world-wide-web at this URL: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * https://lenevor.com/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * If you did not receive a copy of the license and are unable to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * obtain it through the world-wide-web, please send an email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * to [email protected] so we can send you a copy immediately. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @package     Lenevor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @subpackage  Base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @link        https://lenevor.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | namespace Syscodes\Core\Bootstrap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use ErrorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Syscodes\Contracts\Core\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Syscodes\Contracts\Debug\ExceptionHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Syscodes\Debug\FatalExceptions\FatalErrorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use Syscodes\Debug\FatalExceptions\FatalThrowableError; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * It is an integrated exception handler that allows you to report and  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * generate exceptions in a simple and friendly way. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * @author Alexander Campo <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | class BootHandleExceptions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * The application implementation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var \Syscodes\Contracts\Core\Application $app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Bootstrap the given application. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param  \Syscodes\Contracts\Core\Application  $app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function bootstrap(Application $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->app = $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         error_reporting(-1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         set_error_handler([$this, 'handleError']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         set_exception_handler([$this, 'handleException']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         register_shutdown_function([$this, 'handleShutdown']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ( ! $app->isUnitTests()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ini_set('display_errors', 'off'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Handle a PHP error for the application. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param  int  $level | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param  string  $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param  string|null  $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param  int  $line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param  array  $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @throws \ErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function handleError($level, $message, $file = '', $line = 0, $context = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if (error_reporting() & $level) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             throw new ErrorException($message, 0, $level, $file, $line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * Handle an exception for the application. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param  \Throwable  $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function handleException(Throwable $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if ( ! $e instanceof Exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $e = new FatalThrowableError($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $this->getExceptionHandler()->report($e); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         if ($this->app->runningInConsole()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $this->renderForConsole($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $this->renderHttpResponse($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * Render an exception to the console. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param  \Throwable  $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     protected function renderForConsole(Throwable $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->getExceptionHandler()->renderForConsole($e); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * Render an exception as an HTTP response and send it. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param  \Throwable  $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     protected function renderHttpResponse(Throwable $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $this->getExceptionHandler()->render($this->app['request'], $e)->send(true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * Handle the PHP shutdown event. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |     public function handleShutdown() | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |         if ( ! is_null($error = error_get_last()) && $this->isFatal($error['type'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |             $this->handleException($this->fatalExceptionFromError($error, 0)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * Determine if the error type is fatal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @param  int  $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     protected function isFatal($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return in_array($type, array(E_COMPILE_ERROR, E_CORE_ERROR, E_ERROR, E_PARSE)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * Create a new fatal exception instance from an error array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @param  array  $error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @param  int|null  $traceOffset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @return \Syscodes\Debug\FatalExceptions\FatalErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     protected function fatalExceptionFromError(array $error, $traceOffset = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         return new FatalErrorException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             $error['message'], $error['type'], 0, $error['file'], $error['line'], $traceOffset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * Get an instance of the exception handler. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      * @return \Syscodes\Contracts\Debug\Handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     protected function getExceptionHandler() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     {     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         return $this->app->make(ExceptionHandler::class); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 191 |  |  | } |