/** * @param Request $request * @param callable $next * * @return mixed * @throws \Exception */ public function handle(Request $request, callable $next) { $resource = $request->param('__NAMESPACE__'); $controllerName = $request->param('__CONTROLLER__'); $method = $request->param('__ACTION__'); if ($controllerName) { if (FALSE === strstr($controllerName, '\\')) { $controllerName = Word::dashToCamelCase($controllerName); $controllerName .= 'Controller'; $controllerName = rtrim($resource, '\\') . '\\' . $controllerName; } } else { $controllerName = $resource; } if (FALSE !== strstr($controllerName, '@')) { $controllerName = explode('@', $controllerName); $method = $controllerName[1]; $controllerName = $controllerName[0]; } $method = lcfirst(Word::dashToCamelCase($method)); $request->set('__CONTROLLER__', $controllerName); $request->set('__ACTION__', $method); if (!class_exists($controllerName)) { throw new \Exception(sprintf('Class "%s" does not exist', $controllerName), Response::HTTP_NOT_FOUND); } if (!method_exists($controllerName, strtoupper($method))) { throw new \Exception(sprintf('Method "%s" does not exist in class "%s"', $method, $controllerName), Response::HTTP_NOT_FOUND); } $controller = $this->serviceManager->load($controllerName); $params = $request->query(); $params = ArrayUtils::merge($params, $request->post(), FALSE, TRUE); // $params = ArrayUtils::merge($params, $request->attributes->all(), false, true); try { $mvcResponse = $this->serviceManager->call([$controller, $method], $params); } catch (ValidationError $error) { $method = $request->post('__PREVIOUS__', 'get'); $mvcResponse = $this->serviceManager->call([$controller, strtolower($method)], $params); } if ($mvcResponse instanceof Response) { return $mvcResponse; } $mvcResponse = new DispatchResult($mvcResponse, $controllerName, $method); $result = $next($request); if ($result) { return $result; } return $mvcResponse; }
/** * @param string $path * @param null $default * * @return array|null */ public function get($path, $default = NULL) { $item = ArrayUtils::getValueOrNull($path, $this->container); return $item ? $item : $default; }