Ejemplo n.º 1
0
 /**
  * Finds route, controller and method. Calls them with actual params and returns result
  *
  * @return mixed
  * @throws \Kernel\Exception\LoaderException When Controller class was not found or doesn't implement controller interface
  * @throws \Kernel\Exception\HttpNotFoundException When Route was not found
  */
 public function processRoute()
 {
     if (!($route = $this->_findRoute(Request::getUri()))) {
         throw new HttpNotFoundException('Page not found!');
     }
     if (!class_exists($route['controller'])) {
         throw new LoaderException(sprintf('Class "%s" was not found'));
     }
     $this->_currentRoute = $route;
     $reflection = new \ReflectionClass($route['controller']);
     $interfaceName = 'Kernel\\Controller\\ControllerInterface';
     if (!$reflection->implementsInterface($interfaceName)) {
         throw new LoaderException(sprintf('Class "%s" must implement interface "%s"', $route['controller'], $interfaceName));
     }
     $reflectionMethod = $reflection->getMethod($route['action'] . 'Action');
     //Injecting get arguments to Controller
     $args = array();
     foreach ($reflectionMethod->getParameters() as $parameter) {
         $name = $parameter->name;
         $args[$name] = isset($route['_values']) && isset($route['_values'][$name]) ? $route['_values'][$name] : $parameter->getDefaultValue();
     }
     $controller = new $route['controller']();
     return $reflectionMethod->invokeArgs($controller, $args);
 }