| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Protoku\Exceptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ErrorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Protoku\Interfaces\FormatterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	const ERROR_HANDLER = 'handleError'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	const EXCEPTION_HANDLER = 'handleException'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	const SHUTDOWN_HANDLER = 'handleShutdown'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 * @var FormatterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	private $formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * Handler constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * @param FormatterInterface $formatter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	public function __construct(FormatterInterface $formatter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		$this->formatter = $formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		$this->disablePHPDisplayErrors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		$this->registerErrorHandler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		$this->registerExceptionHandler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		$this->registerShutdownHandler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 	protected function disablePHPDisplayErrors() | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 		ini_set( 'display_errors', 0 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * Register the PHP error handler. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	protected function registerErrorHandler() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		set_error_handler([$this, self::ERROR_HANDLER]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * Register the PHP exception handler. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	protected function registerExceptionHandler() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		set_exception_handler([$this, self::EXCEPTION_HANDLER]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * Register a function for execution on shutdown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	protected function registerShutdownHandler() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		register_shutdown_function([$this, self::SHUTDOWN_HANDLER]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * @param int $level | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @param string $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @param int $line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @throws ErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	public function handleError(int $level, string $message, string $file = '', int $line = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		throw new ErrorException($message, 0, $level, $file, $line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * @param \Throwable $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	public function handleException(\Throwable $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$data = json_encode($this->formatter->format($e)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		$this->sendException($data, 501); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * Handle the PHP shutdown event. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	public function handleShutdown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		$error = error_get_last(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		if ($error) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			$this->handleException(new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	public function sendException($data, $statusCode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		header("HTTP/1.0 " . $statusCode . " Internal Server Error"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		header('Content-Type: application/json; charset=utf-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		echo $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		return; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 112 |  |  | } | 
            
                        
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.