johnmahugu

php - bottle.php microframework

Jun 25th, 2015
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.06 KB | None | 0 0
  1. <?php
  2.  
  3. if(!defined('APPLICATION_PATH')) {
  4.     if(substr(__FILE__, 0, 7) == 'phar://') {
  5.         $filename = substr(substr(__FILE__, 0, strrpos(__FILE__, DIRECTORY_SEPARATOR)), 7);
  6.         define('APPLICATION_PATH', realpath(dirname($filename)) . DIRECTORY_SEPARATOR);
  7.     } else {
  8.         define('APPLICATION_PATH', realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR);
  9.     }
  10. }
  11. if(substr(__FILE__, 0, 7) == 'phar://') {
  12.     define('BOTTLE_PATH', substr(__FILE__, 0, strrpos(__FILE__, DIRECTORY_SEPARATOR) + 1));
  13. } else {
  14.     define('BOTTLE_PATH', realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR);
  15. }
  16.  
  17. /**
  18.  * Framework initialization class
  19.  *
  20.  * @package bottle
  21.  * @author nergal
  22.  */
  23. class Bottle {
  24.     /**
  25.      * Autoload classes
  26.      *
  27.      * @param string $classname
  28.      * @return boolean
  29.      */
  30.     public function autoload($classname) {
  31.         $classname = strtolower($classname);
  32.         $filename = BOTTLE_PATH . str_replace('_', DIRECTORY_SEPARATOR, $classname) . '.php';
  33.  
  34.         if (file_exists($filename)) {
  35.             require_once $filename;
  36.         }
  37.  
  38.         return class_exists($classname);
  39.     }
  40.  
  41.     /**
  42.      * Exception handler
  43.      *
  44.      * @param Exception $e
  45.      * @return void
  46.      */
  47.     public function exceptionHandler($e) {
  48.         $html = '<style type="text/css">
  49.            .bottle-framework-exception {background-color: #f0f5ff;padding: 5px;border: 1px solid #d5e5ee;font-size:12px;font-family: Tahoma, Arial;}
  50.            .bottle-framework-exception h1 {background-color: #cce;margin: 0px;padding: 5px;font-size:13px;}
  51.            .bottle-framework-exception h1 span {color: #555;}
  52.            .bottle-framework-exception h1.right {float:right;margin-right: 5px;font-weight:normal}
  53.            .bottle-framework-trace {list-style-type: none;padding: 0px;border: 1px solid #cce;border-bottom: 0px;}
  54.            .bottle-framework-trace li {border-bottom: 1px solid #cce;background-color: #fff;padding: 3px 2px;}</style>';
  55.  
  56.         $type = 'Unknown exception';
  57.         switch ($e->getCode()) {
  58.             case E_USER_ERROR:
  59.             case E_ERROR:
  60.                 $type = 'Error';
  61.                 break;
  62.             case E_USER_WARNING:
  63.             case E_WARNING:
  64.                 $type = 'Warning';
  65.                 break;
  66.             case E_USER_NOTICE:
  67.             case E_NOTICE:
  68.                 $type = 'Notice';
  69.                 break;
  70.         }
  71.  
  72.         $html.= '<div class="bottle-framework-exception">';
  73.         $html.= '<h1 class="right">in file ' . $e->getFile() . ':' . $e->getLine() . '</h1>';
  74.         $html.= '<h1><span>' . $type . ':</span> ' . $e->getMessage() . '</h1>';
  75.         $html.= '<ol class="bottle-framework-trace">';
  76.  
  77.         $trace = $e->getTrace();
  78.         foreach ($trace as $key => $iteration) {
  79.             $file = (isset($iteration['line']) ? ($iteration['file'] . ':' . $iteration['line']) : '');
  80.             $function = (isset($iteration['class']) ? $iteration['class'] : '');
  81.             $function.= (isset($iteration['type']) ? $iteration['type'] : '');
  82.             $function.= $iteration['function'] . '()';
  83.             $html.= '<li>#' . $key . ' ' . $function . ' in ' . $file . '</li>';
  84.         }
  85.         $html.= '</ol></div>';
  86.  
  87.         if ($e instanceof Bottle_Forbidden_Exception) {
  88.             header("HTTP/1.0 403 Forbidden");
  89.         } elseif ($e instanceof Bottle_NotFound_Exception) {
  90.             header("HTTP/1.0 404 Not Found");
  91.         } else {
  92.             header("HTTP/1.0 500 Internal Server Error");
  93.         }
  94.         echo $html;
  95.     }
  96.  
  97.     /**
  98.      * Errors handler
  99.      *
  100.      * @param integer $errno
  101.      * @param string $errstr
  102.      * @param string $errfile
  103.      * @param string $errline
  104.      * @return void
  105.      */
  106.     public function errorHandler($errno, $errstr, $errfile, $errline) {
  107.         $exception = new ErrorException($errstr, 0, $errno, $errfile, $errline);
  108.         return $this->exceptionHandler($exception);
  109.     }
  110.  
  111.     /**
  112.      * Fatal error handler
  113.      *
  114.      * @todo некорректно работает - отстреливаются пред. обработчики
  115.      * @return void
  116.      */
  117.     public function shutdownHandler() {
  118.         $last_error = error_get_last();
  119.         if ($last_error['type'] === E_ERROR OR $last_error['type'] === E_USER_ERROR) {
  120.             ob_clean();
  121.             $this->errorHandler(
  122.                 $last_error['type'],
  123.                 $last_error['message'],
  124.                 $last_error['file'],
  125.                 $last_error['line']
  126.             );
  127.         }
  128.     }
  129.  
  130.     /**
  131.      * Setting up global handlers
  132.      *
  133.      * @construct
  134.      * @return void
  135.      */
  136.     public function __construct() {
  137.         set_exception_handler(array($this, 'exceptionHandler'));
  138.         set_error_handler(array($this, 'errorHandler'));
  139.  
  140.         register_shutdown_function(array($this, 'shutdownHandler'));
  141.         spl_autoload_register(array($this, 'autoload'), TRUE, TRUE);
  142.  
  143.         Bottle_Core::start();
  144.     }
  145. }
  146.  
  147. // creating the global $request and $response objects
  148. $request = $response = null;
  149. new Bottle;
Advertisement
Add Comment
Please, Sign In to add comment