Example #1
0
 /**
  * Get controller based on HTTP request
  * @param array $args Controller arguments
  * @return object Controller
  * @throws Exception\UndefinedControllerException
  * @throws Exception\InvalidControllerException
  * @throws Exception\NoIndexRouteException
  */
 public static function getController(array $args = array())
 {
     $config = ConfigFileHandler::getInstance();
     $mvc = $config->getParam("framework", "mvc");
     $requestArray = Request::getDirArray();
     $routePath = $mvc["route"];
     $controllerClassName = null;
     if (!isset($requestArray[intval($mvc["start_index"])])) {
         $controllerClassName = $routePath["."];
     } else {
         for ($i = intval($mvc["start_index"]); $i < count($requestArray) + 1; $i++) {
             if (!($routePath = self::getRouteChild($requestArray[$i], $routePath))) {
                 throw new UndefinedControllerException("Controller for this request is not defined");
             }
             if (is_string($routePath)) {
                 $controllerClassName = $routePath;
                 break;
             }
             if (!isset($requestArray[$i + 1])) {
                 if (isset($routePath["."])) {
                     $controllerClassName = $routePath["."];
                     break;
                 } else {
                     throw new NoIndexRouteException("Route has no index controller defined");
                 }
             }
         }
     }
     $controllerReflection = new \ReflectionClass(__NAMESPACE__ . "\\Controller\\" . $controllerClassName);
     if ($controllerReflection->implementsInterface(__NAMESPACE__ . "\\Controller\\Init\\IController")) {
         return $controllerReflection->newInstance($args);
     } else {
         throw new InvalidControllerException("Controller is not implementing interface IController");
     }
 }