Example #1
0
 private function _dispatcher($routeInfo)
 {
     $_handler = ['module' => 'Home', 'controller' => 'Index', 'action' => 'index'];
     $handler = $routeInfo['r_handle'];
     $type = gettype($handler);
     switch ($type) {
         case 'string':
             if (false != strpos($handler, '@')) {
                 list($_handler['module'], $handler) = explode('@', $handler);
             }
             if (false != strpos($handler, '_')) {
                 list($_handler['controller'], $_handler['action']) = explode('_', $handler);
             } elseif (false != strpos($handler, '/')) {
                 list($_handler['controller'], $_handler['action']) = explode('/', $handler);
             }
             break;
         case 'array':
             $_handler = array_merge($_handler, $handler);
             break;
         case 'object':
             if ($handler instanceof \Closure) {
                 $this->_response->end(call_user_func($handler));
                 return;
             }
     }
     empty($_handler['namespace']) || Router::setModule(ucfirst($_handler['namespace']));
     empty($_handler['module']) || Router::setModule(ucfirst($_handler['module']));
     empty($_handler['controller']) || Router::setController(ucfirst($_handler['controller']));
     empty($_handler['action']) || Router::setAction($_handler['action']);
     $controller = Router::getNamespace() . '\\' . Router::getModule() . '\\' . Router::getControllerSuffix() . '\\' . Router::getController() . Router::getControllerSuffix();
     $action = Router::getAction() . Router::getActionSuffix();
     /**
      * @var Controller $obj ;
      */
     $obj = new $controller($routeInfo['request'], $this->_response);
     $rel = new \ReflectionClass($obj->setDI($this->_di));
     if ($rel->hasMethod($action)) {
         $method = $rel->getMethod($action);
         if ($method->isPublic() && !$method->isStatic()) {
             if ($scheduler = $this->_di->get('scheduler')) {
                 if ($method->getNumberOfParameters() > 0) {
                     $result = $method->invokeArgs($obj, $routeInfo['r_var']);
                 } else {
                     $result = $method->invoke($obj);
                 }
                 $scheduler->newTask($result)->run();
                 return;
             }
         }
     }
     $this->_response->getSwResponse()->status(404);
     $this->_response->getSwResponse()->end('<h1>404</h1>');
 }