Advertisement
fabi0

Untitled

Jun 5th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.76 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Load;
  4.  
  5. class Loader {
  6.  
  7.     private static
  8.             $_controller = null,
  9.             $_method = null,
  10.             $_params = [];
  11.  
  12.     private function __construct() {
  13.        
  14.     }
  15.  
  16.     public static function start() {
  17.         self::autoload();
  18.         self::defaultRouter();
  19.     }
  20.  
  21.     private static function autoload() {
  22.         spl_autoload_register(function($class) {
  23.             $explodedPath = explode("\\", trim($class, '\\'));
  24.             $count = count($explodedPath);
  25.             $filename = DIRECTORY_SEPARATOR . ucfirst(strtolower($explodedPath[$count - 1])) . '.php';
  26.             unset($explodedPath[$count - 1]);
  27.             $path = implode(DIRECTORY_SEPARATOR, $explodedPath);
  28.             if (file_exists($path . $filename)) {
  29.                 require_once $path . $filename;
  30.             } else {
  31.                 throw new \Exception("Page not found", "500");
  32.             }
  33.         });
  34.     }
  35.  
  36.     private static function defaultRouter() {
  37.  
  38.         self::$_controller = \Models\Config::default_controller;
  39.         self::$_method = \Models\Config::default_method;
  40.         $url = explode("/", \Models\Sanitaze::clear(rtrim($_GET['url'], '/')));
  41.         if (isset($url[0]) && $url[0] != '') {
  42.             self::$_controller = "\\Controllers\\" . $url[0];
  43.             unset($url[0]);
  44.         }
  45.  
  46.         self::$_controller = new self::$_controller;
  47.  
  48.         if (isset($url[1]) && $url[1] != '') {
  49.             if (method_exists(self::$_controller, $url[1])) {
  50.                 self::$_method = $url[1];
  51.                 unset($url[1]);
  52.             }
  53.         }
  54.  
  55.         self::$_params = $url ? array_values($url) : [];
  56.  
  57.         call_user_func_array([self::$_controller, self::$_method], self::$_params);
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement