AloFramework documentation
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Namespaces

  • Alo
    • Cache
    • CLI
    • Controller
    • Db
    • Exception
    • FileSystem
    • Session
    • Validators
    • Windows
  • Controller
  • None
  • PHP

Classes

  • Cookie
  • Cron
  • Curl
  • Email
  • Format
  • Handler
  • Locale
  • Profiler
  • Security
  • SFTP
  1 <?php
  2 
  3     namespace Alo;
  4 
  5     if (!defined('GEN_START')) {
  6         http_response_code(404);
  7     } else {
  8 
  9         /**
 10          * Handles autoloading, errors and exceptions
 11          *
 12          * @author Art <a.molcanovas@gmail.com>
 13          */
 14         abstract class Handler {
 15 
 16             /**
 17              * Whether CSS has been injected yet
 18              *
 19              * @var bool
 20              */
 21             protected static $cssInjected = false;
 22 
 23             /**
 24              * Injects CSS into the page to preffity our output
 25              *
 26              * @author Art <a.molcanovas@gmail.com>
 27              */
 28             protected static function injectCss() {
 29                 if (!self::$cssInjected) {
 30                     self::$cssInjected = true;
 31                     echo '<style>';
 32                     include DIR_SYS . 'core' . DIRECTORY_SEPARATOR . 'error.css.php';
 33                     echo '</style>';
 34                 }
 35             }
 36 
 37             /**
 38              * The error handler
 39              *
 40              * @author Art <a.molcanovas@gmail.com>
 41              *
 42              * @param int    $errno   The level of the error raised
 43              * @param string $errstr  The error message
 44              * @param string $errfile The filename that the error was raised in
 45              * @param int    $errline The line number the error was raised at
 46              */
 47             static function error($errno, $errstr, $errfile, $errline) {
 48                 self::injectCss();
 49                 $type = $errno;
 50 
 51                 switch ($errno) {
 52                     case E_NOTICE:
 53                     case E_USER_NOTICE:
 54                         $type = 'NOTICE';
 55                         break;
 56                     case E_ERROR:
 57                     case E_USER_ERROR:
 58                     case E_COMPILE_ERROR:
 59                     case E_RECOVERABLE_ERROR:
 60                     case E_CORE_ERROR:
 61                         $type = 'ERROR';
 62                         break;
 63                     case E_WARNING:
 64                     case E_USER_WARNING:
 65                     case E_CORE_WARNING:
 66                         $type = 'WARNING';
 67                         break;
 68                 }
 69 
 70                 $f = explode(DIR_INDEX, $errfile);
 71                 $f = isset($f[1]) ? $f[1] : $f[0];
 72 
 73                 echo '<div class="alo-error-wrapper">' . '<div class="alo-error-container">' .
 74                      '<div class="alo-error-type alo-bold">' . $type . ' : ' . $errstr . '</div>' .
 75                      '<div>Raised in <span class="alo-bold">' . $f . ': ' . $errline . '</span></div>' .
 76                      '<div>Backtrace:</div>';
 77 
 78                 $trace = array_reverse(debug_backtrace());
 79                 array_pop($trace);
 80 
 81                 self::echoTrace($trace);
 82 
 83                 echo '</div>' . '</div>';
 84 
 85                 $trace = \debug_backtrace();
 86                 array_shift($trace);
 87                 \Log::error($errstr, $trace);
 88             }
 89 
 90             /**
 91              * Used to automatically load class,interface and trait files
 92              *
 93              * @author Art <a.molcanovas@gmail.com>
 94              *
 95              * @param string $name Class name
 96              */
 97             static function autoloader($name) {
 98                 $name      = ltrim(strtolower(str_replace('\\', DIRECTORY_SEPARATOR, $name)), '/') . '.php';
 99                 $locations = [DIR_APP . 'class',
100                               DIR_SYS . 'class',
101                               DIR_APP . 'interface',
102                               DIR_APP . 'traits'];
103 
104                 foreach ($locations as $l) {
105                     if (file_exists($l . DIRECTORY_SEPARATOR . $name)) {
106                         include_once $l . DIRECTORY_SEPARATOR . $name;
107                         break;
108                     }
109                 }
110             }
111 
112             /**
113              * Echoes previous exceptions if applicable
114              *
115              * @author Art <a.molcanovas@gmail.com>
116              *
117              * @param null|\Exception $e The previous exception
118              */
119             protected static function echoPreviousExceptions($e) {
120                 if ($e instanceof \Exception) {
121                     echo '<div></div>Preceded by <span style="font-weight: bold">' . $e->getCode() . ': ' .
122                          $e->getMessage() . ' @ ' . $e->getFile() . '\'s line ' . $e->getLine() . '.</span>';
123 
124                     self::echoPreviousExceptions($e->getPrevious());
125                 }
126             }
127 
128             /**
129              * Echoes the debug backtrace
130              *
131              * @author Art <a.molcanovas@gmail.com>
132              *
133              * @param array $trace The backtrace
134              */
135             protected static function echoTrace($trace) {
136                 echo '<table cellpadding="2" border="1" class="alo-trace-table">' . '<thead>' . '<tr>' . '<th>#</th>' .
137                      '<th>Function</th>' . '<th>Args</th>' . '<th>Location</th>' . '<th>Line</th>' . '</tr>' .
138                      '</thead>' . '<tbody>';
139 
140                 foreach ($trace as $k => $v) {
141                     $func = $loc = $line = '';
142 
143                     if (isset($v['class'])) {
144                         $func = $v['class'];
145                     }
146                     if (isset($v['type'])) {
147                         $func .= $v['type'];
148                     }
149                     if (isset($v['function'])) {
150                         $func .= $v['function'] . '()';
151                     }
152                     if (!$func) {
153                         $func = '[unknown]';
154                     }
155 
156                     if (isset($v['file'])) {
157                         $loc = \get(explode(DIR_INDEX, $v['file'])[1]);
158                     }
159                     if (isset($v['line'])) {
160                         $line .= $v['line'];
161                     }
162 
163                     echo '<tr>' . '<td>' . $k . '</td>' . '<td>' . $func . '</td>' . '<td style="text-align:left">' .
164                          (isset($v['args']) && $v['args'] ?
165                              '<pre>' . preg_replace("/\n(\s*)(\t*)\(/i", "$1$2(", print_r($v['args'], true)) .
166                              '</pre>' : '') . '</td>' . '<td>' . $loc . '</td>' . '<td>' . $line . '</td>' . '</tr>';
167                 }
168 
169                 echo '</tbody>' . '</table>';
170             }
171 
172             /**
173              * Exception handler
174              *
175              * @author Art <a.molcanovas@gmail.com>
176              *
177              * @param \Exception $e The exception
178              */
179             static function ecxeption(\Exception $e) {
180                 self::injectCss();
181                 $msg   = $e->getMessage();
182                 $trace = $e->getTrace();
183                 array_pop($trace);
184 
185                 echo '<div class="alo-error-wrapper">' . '<div class="alo-error-container">' .
186                      '<div class="alo-error-type alo-bold">' . '[' . $e->getCode() . '] uncaught exception: ' .
187                      $e->getMessage();
188 
189                 self::echoPreviousExceptions($e->getPrevious());
190 
191                 echo '</div>' . '<div>Raised in <span class="alo-bold">' . $e->getFile() . ': ' . $e->getLine() .
192                      '</span></div>' . '<div>Backtrace:</div>';
193 
194                 self::echoTrace($trace);
195 
196                 echo '</div>' . '</div>';
197 
198                 $trace = $e->getTrace();
199                 array_shift($trace);
200                 \Log::error($msg, $trace);
201             }
202         }
203     }
204 
AloFramework documentation API documentation generated byApiGen 2.8.0