/** @Factory("zool.events") */
 public function events()
 {
     return Events::instance();
 }
 /**
  * Handles PHP execution errors such as warnings, notices.
  *
  * This method is implemented as a PHP error handler. It requires
  * that constant ENABLE_ERROR_HANDLER be defined true.
  *
  * This method will first raise an {@link onError} event.
  * If the error is not handled by any event handler, it will call
  * {@link getErrorHandler errorHandler} to process the error.
  *
  * The application will be terminated by this method.
  *
  * @param integer $code the level of the error raised
  * @param string $message the error message
  * @param string $file the filename that the error was raised in
  * @param integer $line the line number the error was raised at
  */
 public function handleError($code, $message, $file, $line)
 {
     if ($code & error_reporting()) {
         // disable error capturing to avoid recursive errors
         restore_error_handler();
         restore_exception_handler();
         $log = "{$message} ({$file}:{$line})\nStack trace:\n";
         $trace = debug_backtrace();
         // skip the first 3 stacks as they do not tell the error position
         if (count($trace) > 3) {
             $trace = array_slice($trace, 3);
         }
         foreach ($trace as $i => $t) {
             if (!isset($t['file'])) {
                 $t['file'] = 'unknown';
             }
             if (!isset($t['line'])) {
                 $t['line'] = 0;
             }
             if (!isset($t['function'])) {
                 $t['function'] = 'unknown';
             }
             $log .= "#{$i} {$t['file']}({$t['line']}): ";
             if (isset($t['object']) && is_object($t['object'])) {
                 $log .= get_class($t['object']) . '->';
             }
             $log .= "{$t['function']}()\n";
         }
         if (isset($_SERVER['REQUEST_URI'])) {
             $log .= 'REQUEST_URI=' . $_SERVER['REQUEST_URI'];
         }
         $this->displayError($code, $message, $file, $line);
         try {
             Events::instance()->raise('zool.error', $this, $code, $message, $file, $line);
         } catch (Exception $e) {
             $this->displayException($e);
         }
         try {
             $this->end(1);
         } catch (Exception $e) {
             // use the most primitive way to log error
             $msg = get_class($e) . ': ' . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")\n";
             $msg .= $e->getTraceAsString() . "\n";
             $msg .= "Previous error:\n";
             $msg .= $log . "\n";
             $msg .= '$_SERVER=' . var_export($_SERVER, true);
             error_log($msg);
             exit(1);
         }
     }
 }