public function dispatch()
 {
     if ($this->router == null) {
         throw new \Exception('No valid router found', 500);
     }
     $configParams = null;
     $_uri = $this->router->getUri();
     $routes = App::getInstance()->getConfig()->routes;
     if (is_array($routes) && count($routes) > 0) {
         foreach ($routes as $key => $value) {
             if (stripos($_uri, $key) === 0 && ($_uri == $key || stripos($_uri, $key . '/') === 0) && $value['namespace']) {
                 $this->namespace = $value['namespace'];
                 $_uri = substr($_uri, strlen($key) + 1);
                 $configParams = $value;
                 break;
             }
         }
     } else {
         throw new \Exception('Default route missing', 500);
     }
     if ($this->namespace == null && $routes['*']['namespace']) {
         $this->namespace = $routes['*']['namespace'];
         $configParams = $routes['*'];
     } else {
         if ($this->namespace == null && !$routes['*']['namespace']) {
             throw new \Exception('Default route missing', 500);
         }
     }
     $this->input = InputData::getInstance();
     $_params = explode('/', $_uri);
     if ($_params[0]) {
         $this->controller = strtolower($_params[0]);
         if ($_params[1]) {
             $this->method = strtolower($_params[1]);
             unset($_params[0], $_params[1]);
             $this->params = array_values($_params);
             $this->input->setGet($this->params);
         } else {
             $this->method = $this->getDefaultMethod();
         }
     } else {
         $this->controller = $this->getDefaultController();
         $this->method = $this->getDefaultMethod();
     }
     if (is_array($configParams) && $configParams['controllers']) {
         if ($configParams['controllers'][$this->controller]['methods'][$this->method]) {
             $this->method = strtolower($configParams['controllers'][$this->controller]['methods'][$this->method]);
         }
         if (isset($configParams['controllers'][$this->controller]['to'])) {
             $this->controller = strtolower($configParams['controllers'][$this->controller]['to']);
         }
     }
     $this->input->setPost($this->router->getPost());
     $controller = $this->namespace . '\\' . ucfirst($this->controller);
     $newController = new $controller();
     $this->loadMethod($newController);
 }
 public function dispatch()
 {
     if ($this->_router == null) {
         throw new \Exception('No valid router found.', 500);
     }
     $uri = $this->_router->getURI();
     $routes = App::getInstance()->getConfig()->routes;
     $_rc = null;
     if (is_array($routes) && count($routes) > 0) {
         foreach ($routes as $route => $value) {
             if (stripos($uri, $route) === 0 && ($uri == $route || strpos($uri, $route . '/') === 0) && $value['namespace']) {
                 $this->_namespace = $value['namespace'];
                 $uri = substr($uri, strlen($route) + 1);
                 $_rc = $value;
                 break;
             }
         }
     } else {
         throw new \Exception('Default route missing', 500);
     }
     if ($this->_namespace == null && $routes['*']['namespace']) {
         $this->_namespace = $routes['*']['namespace'];
         $_rc = $routes['*'];
     } else {
         if ($this->_namespace == null && !$routes['*']['namespace']) {
             throw new \Exception('Default route missing', 500);
         }
     }
     $input = InputData::getInstance();
     $params = explode('/', $uri);
     if ($params[0]) {
         $this->_controller = strtolower($params[0]);
         if ($params[1]) {
             $this->_method = strtolower($params[1]);
             unset($params[0], $params[1]);
             $input->setGet(array_values($params));
         } else {
             $this->_method = $this->getDefaultMethod();
         }
     } else {
         $this->_controller = $this->getDefaultController();
         $this->_method = $this->getDefaultMethod();
     }
     if (is_array($_rc) && $_rc['controllers']) {
         if ($_rc['controllers'][$this->_controller]['methods'][$this->_method]) {
             $this->_method = strtolower($_rc['controllers'][$this->_controller]['methods'][$this->_method]);
         }
         if (isset($_rc['controllers'][$this->_controller]['to'])) {
             $this->_controller = strtolower($_rc['controllers'][$this->_controller]['to']);
         }
     }
     $input->setPost($this->_router->getPost());
     $contructed = $this->_namespace . '\\' . ucfirst($this->_controller);
     $newController = new $contructed();
     $newController->{$this->_method}();
 }
Ejemplo n.º 3
0
 private function processController()
 {
     $input = InputData::getInstance();
     $input->setGet($this->_params);
     $input->setPost($this->_router->GetPost());
     $file = ucfirst($this->_namespace) . '\\' . ucfirst($this->_controller);
     $this->_controller = $file;
     $realPath = str_replace('\\', DIRECTORY_SEPARATOR, $file);
     $realPath = realpath('ShoppingCart\\' . $realPath . '.php');
     if (file_exists($realPath) && is_readable($realPath)) {
         $calledController = new $file();
         if (method_exists($calledController, $this->_method)) {
             if ($this->isValidRequestMethod($calledController, $this->_method)) {
                 // Create binding model
                 $refMethod = new \ReflectionMethod($calledController, $this->_method);
                 $doc = $refMethod->getDocComment();
                 // Validate accessibility
                 $this->ValidateAuthorization($doc);
                 if (preg_match('/@param\\s+\\\\?([\\s\\S]+BindingModel)\\s+\\$/', $doc, $match)) {
                     $bindingModelName = $match[1];
                     $bindingModelsNamespace = App::getInstance()->getConfig()->app['namespaces']['Models'] . 'BindingModels/';
                     $bindingModelsNamespace = str_replace('../', '', $bindingModelsNamespace);
                     $bindingModelPath = str_replace('/', '\\', $bindingModelsNamespace . $bindingModelName);
                     $bindingModelPath = substr($bindingModelPath, 13);
                     $bindingReflection = new \ReflectionClass($bindingModelPath);
                     $properties = $bindingReflection->getProperties();
                     $params = [];
                     foreach ($properties as $property) {
                         $name = $property->getName();
                         $value = $input->postForDb($name);
                         if ($value === null) {
                             throw new \Exception("Invalid binding model! Property '{$name}' not found", 400);
                         } else {
                             $params[$name] = $value;
                         }
                     }
                     $bindingModel = new $bindingModelPath($params);
                     Injector::getInstance()->loadDependencies($calledController);
                     $calledController->{strtolower($this->_method)}($bindingModel);
                 } else {
                     Injector::getInstance()->loadDependencies($calledController);
                     $calledController->{strtolower($this->_method)}();
                 }
                 exit;
             } else {
                 throw new \Exception("Method does not allow '" . ucfirst($this->_requestMethod) . "' requests!", 500);
             }
         } else {
             throw new \Exception("'" . $this->_method . "' not found in '" . $file . '.php', 404);
         }
     } else {
         throw new \Exception("File '" . $file . '.php' . "' not found!", 404);
     }
 }