Пример #1
0
 public static function run()
 {
     // default request when no path information is provided
     $request = ['http_method' => strtolower($_SERVER['REQUEST_METHOD']), 'class' => 'index', 'method' => 'index', 'params' => []];
     // extract request routing
     if (array_key_exists('PATH_INFO', $_SERVER)) {
         $path_tmp = $_SERVER['PATH_INFO'];
         if (substr($path_tmp, 0, 1) == '/') {
             $path_tmp = substr($path_tmp, 1);
         }
         $path = explode('/', $path_tmp);
         if (isset($path[0])) {
             $request['class'] = $path[0];
         }
         if (isset($path[1])) {
             $request['method'] = $path[1];
         }
         if (count($path > 2)) {
             $request['params'] = array_slice($path, 2);
         }
     }
     \My\Env::set('request', $request);
     // find controller class path and check if it exists
     $class_path = \My\Env::get('controller_path') . $request['class'] . '.ctrl.php';
     #@TODO replace with nice 404 handling
     if (!file_exists($class_path)) {
         die('404 controller "' . $request['class'] . '" not found');
     }
     // load class
     require_once $class_path;
     $class_name = '\\My\\Controller\\' . $request['class'];
     $controller_instance = new $class_name();
     // check if method exists
     $method_name = $request['http_method'] . '_' . $request['method'];
     #@TODO replace with nice 404 handling
     if (!method_exists($controller_instance, $method_name)) {
         die('404 method "' . $class_name . '::' . $method_name . '" not found');
     }
     // call method as defined by the request
     $result = call_user_func_array([$controller_instance, $method_name], $request['params']);
     // if response is an instance of \My\Response_html execute it
     if ($result instanceof \My\Response_html) {
         $result->exec();
         die;
     } else {
         die($class_name . '::' . $method_name . ' returned something which I have no clue how to handle.');
     }
 }