/** * Run the aplication * * @access public * @return void */ public function run(HttpRequest $request = null) { if (!$this->container->getSetting('debug')) { error_reporting(0); } else { error_reporting(E_ALL); } if (!$request) { $request = $this->container->getRequest(); } try { $parsed = $this->container->getRouter()->parse($request, $this->container->getRoutes()); if ($parsed['found'] || $this->container->hasRoute('notFound')) { if (is_string($parsed['action'])) { # Extract class controller and method list($controller, $method) = explode('::', $parsed['action']); # initialize arguments $arguments = array(); # Create a reflection method $reflectionMethod = new \ReflectionMethod($controller, $method); $reflectionParams = $reflectionMethod->getParameters(); # Create arguments foreach ($reflectionParams as $param) { if ($paramClass = $param->getClass()) { $className = $paramClass->name; if ($className === 'MVC\\MVC' || $className === '\\MVC\\MVC') { $arguments[] = $this; } elseif ($className === 'MVC\\Server\\HttpRequest' || $className === '\\MVC\\Server\\HttpRequest') { $arguments[] = $request; } } else { foreach ($parsed['params'] as $keyRouteParam => $valueRouteParam) { if ($param->name === $keyRouteParam) { $arguments[] = $valueRouteParam; break; } } } } $response = call_user_func_array($reflectionMethod->getClosure(new $controller()), $arguments); if (is_array($response) && !isset($response['body'])) { throw new \LogicException("Invalid response array. Array response haven't body. Expected array('body' => 'string')"); } elseif (is_string($response)) { $response = array('body' => $response); } } elseif (is_callable($parsed['action'])) { $this->container->getRequest()->params = $parsed['params']; $response = call_user_func_array($parsed['action'], array_values($parsed['params'])); } else { throw new \LogicException('Route haven\'t action.'); } if ($this->container->hasProvider('monolog')) { $this->container->providers['monolog']->addInfo($response, $parsed); } $this->container->getResponse()->render($response); } else { if ($this->container->getSetting('debug')) { throw new \LogicException(sprintf('Route or Resource "%s" not found.', $request->url)); } $this->defaultNotFound(); } } catch (\Exception $e) { Error::run($e); } }