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

Namespaces

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

Classes

  • Cron
  • cURL
  • Email
  • File
  • Handler
  • Profiler
  • SFTP
  1 <?php
  2 
  3    namespace Alo;
  4 
  5    if (!defined('GEN_START')) {
  6       http_response_code(404);
  7       die();
  8    }
  9 
 10    /**
 11     * Handles autoloading, errors and exceptions
 12     *
 13     * @author Art <a.molcanovas@gmail.com>
 14     */
 15    abstract class Handler {
 16 
 17       /**
 18        * Whether CSS has been injected yet
 19        *
 20        * @var bool
 21        */
 22       protected static $css_injected = false;
 23 
 24       /**
 25        * Injects CSS into the page to preffity our output
 26        *
 27        * @author Art <a.molcanovas@gmail.com>
 28        */
 29       protected static function inject_css() {
 30          if (!self::$css_injected) {
 31             self::$css_injected = true;
 32             echo '<style>';
 33             include DIR_SYS . 'core' . DIRECTORY_SEPARATOR . 'error.css.php';
 34             echo '</style>';
 35          }
 36       }
 37 
 38       /**
 39        * The error handler
 40        *
 41        * @author Art <a.molcanovas@gmail.com>
 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::inject_css();
 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">'
 74             . '<div class="alo-error-container">'
 75             . '<div class="alo-error-type alo-bold">'
 76             . $type . ' : ' . $errstr
 77             . '</div>'
 78             . '<div>Raised in <span class="alo-bold">' . $f . ': ' . $errline . '</span></div>'
 79             . '<div>Backtrace:</div>';
 80 
 81          $trace = array_reverse(debug_backtrace());
 82          array_pop($trace);
 83 
 84          self::echo_trace($trace);
 85 
 86          echo '</div>'
 87             . '</div>';
 88 
 89          $trace = \debug_backtrace();
 90          array_shift($trace);
 91          \Log::error($errstr, $trace);
 92       }
 93 
 94       /**
 95        * Used to automatically load class,interface and trait files
 96        *
 97        * @author Art <a.molcanovas@gmail.com>
 98        * @param string $name Class name
 99        */
100       static function autoloader($name) {
101          $name = ltrim(strtolower(str_replace('\\', DIRECTORY_SEPARATOR, $name)), '/') . '.php';
102          $locations = [
103             DIR_APP . 'class',
104             DIR_SYS . 'class',
105             DIR_APP . 'interface',
106             DIR_APP . 'traits'
107          ];
108 
109          foreach ($locations as $l) {
110             if (file_exists($l . DIRECTORY_SEPARATOR . $name)) {
111                include_once $l . DIRECTORY_SEPARATOR . $name;
112                break;
113             }
114          }
115       }
116 
117       /**
118        * Echoes previous exceptions if applicable
119        *
120        * @author Art <a.molcanovas@gmail.com>
121        * @param null|\Exception $e The previous exception
122        */
123       protected static function echo_previous_exceptions($e) {
124          if ($e instanceof \Exception) {
125             echo '<div></div>Preceded by <span style="font-weight: bold">'
126                . $e->getCode() . ': ' . $e->getMessage() . ' @ ' . $e->getFile() . '\'s line ' . $e->getLine()
127                . '.</span>';
128 
129             self::echo_previous_exceptions($e->getPrevious());
130          }
131       }
132 
133       /**
134        * Echoes the debug backtrace
135        *
136        * @author Art <a.molcanovas@gmail.com>
137        * @param array $trace The backtrace
138        */
139       protected static function echo_trace($trace) {
140          echo '<table cellpadding="2" border="1" class="alo-trace-table">'
141             . '<thead>'
142             . '<tr>'
143             . '<th>#</th>'
144             . '<th>Function</th>'
145             . '<th>Args</th>'
146             . '<th>Location</th>'
147             . '<th>Line</th>'
148             . '</tr>'
149             . '</thead>'
150             . '<tbody>';
151 
152          foreach ($trace as $k => $v) {
153             $func = $loc = $line = '';
154 
155             if (isset($v['class'])) {
156                $func = $v['class'];
157             }
158             if (isset($v['type'])) {
159                $func .= $v['type'];
160             }
161             if (isset($v['function'])) {
162                $func .= $v['function'] . '()';
163             }
164             if (!$func) {
165                $func = '[unknown]';
166             }
167 
168             if (isset($v['file'])) {
169                $loc = \get(explode(DIR_INDEX, $v['file'])[1]);
170             }
171             if (isset($v['line'])) {
172                $line .= $v['line'];
173             }
174 
175             echo '<tr>'
176                . '<td>' . $k . '</td>'
177                . '<td>' . $func . '</td>'
178                . '<td style="text-align:left">' . (isset($v['args']) && $v['args'] ? '<pre>' . preg_replace("/\n(\s*)(\t*)\(/i", "$1$2(", print_r($v['args'], true)) . '</pre>' : '') . '</td>'
179                . '<td>' . $loc . '</td>'
180                . '<td>' . $line . '</td>'
181                . '</tr>';
182          }
183 
184          echo '</tbody>'
185             . '</table>';
186       }
187 
188       /**
189        * Exception handler
190        *
191        * @author Art <a.molcanovas@gmail.com>
192        * @param \Exception $e The exception
193        */
194       static function ecxeption(\Exception $e) {
195          self::inject_css();
196          $msg = $e->getMessage();
197          $trace = $e->getTrace();
198          array_pop($trace);
199 
200          echo '<div class="alo-error-wrapper">'
201             . '<div class="alo-error-container">'
202             . '<div class="alo-error-type alo-bold">'
203             . '[' . $e->getCode() . '] uncaught exception: ' . $e->getMessage();
204 
205          self::echo_previous_exceptions($e->getPrevious());
206 
207          echo '</div>'
208             . '<div>Raised in <span class="alo-bold">' . $e->getFile() . ': ' . $e->getLine() . '</span></div>'
209             . '<div>Backtrace:</div>';
210 
211          self::echo_trace($trace);
212 
213          echo '</div>'
214             . '</div>';
215 
216          $trace = $e->getTrace();
217          array_shift($trace);
218          \Log::error($msg, $trace);
219       }
220    }
AloFramework documentation API documentation generated by ApiGen 2.8.0