Example #1
0
 static function action($method, $path, $function)
 {
     $restInput = array();
     $me = self::getInstance();
     if (Input::has('__request_method')) {
         $request_method = Input::get('__request_method');
     } else {
         $request_method = $_SERVER['REQUEST_METHOD'];
     }
     if ($me->is_called == true) {
         return IgnoreProcessor::getInstance();
     }
     if ($method != 'otherwise') {
         if ($method != $request_method) {
             return IgnoreProcessor::getInstance();
         }
         $path_array = preg_split("/\\//", $path);
         foreach ($path_array as $key => $path_element) {
             if (strlen(trim($path_element)) == 0) {
                 unset($path_array[$key]);
             }
         }
         $path_array = array_values($path_array);
         if (count($path_array) != count($me->path)) {
             return $me->getPostProcessor();
         }
         foreach ($path_array as $key => $path_element) {
             if (preg_match("/^\\:(.*)\$/", $path_element, $match) || preg_match("/^\\{(.*)\\}\$/", $path_element, $match)) {
                 Input::set($match[1], $me->path[$key]);
                 $restInput[] = $me->path[$key];
             } else {
                 if (!isset($me->path[$key]) || $me->path[$key] != $path_element) {
                     return IgnoreProcessor::getInstance();
                 }
             }
         }
     }
     if ($me->skip) {
         return $me->getPostProcessor();
     }
     try {
         if (is_callable($function)) {
             $response = $function();
         } else {
             $function_array = preg_split("/@/", $function);
             if (!isset($function_array[1])) {
                 throw FrameworkException::internalError('Routing Error');
             }
             $controller_namespace = Config::get("app.controller_namespace", "App\\Controller\\");
             $class_name = $controller_namespace . $function_array[0];
             $method_name = $function_array[1];
             //$response = $class_name::$method_name();
             // Initialization controller object
             $controller = new $class_name();
             $response = call_user_func_array(array($controller, $method_name), $restInput);
             //$response = $controller->$method_name();
         }
         if ($response instanceof Response) {
             $response->display();
         } else {
             $rs = new Response();
             $rs->setContentType('text/html')->setContent($response)->display();
         }
         $me->is_called = true;
     } catch (FrameworkException $e) {
         $me->handleError($e);
         $me->is_called = true;
         return IgnoreProcessor::getInstance();
     } catch (Exception $e) {
         $exception = FrameworkException::internalError('Internal Error: ' . $e->getMessage());
         $me->handleError($exception);
         $me->is_called = true;
         return IgnoreProcessor::getInstance();
     }
     return $me->getPostProcessor();
 }