/** * @SuppressWarnings(PHPMD.UnusedFormalParameter) * * @param mixed $resource * @param null $type * * @return RouteCollection */ public function load($resource, $type = null) : RouteCollection { $resource = (string) $resource; if (in_array($resource, $this->descriptions)) { throw new \RuntimeException("Resource '{$resource}' was already loaded"); } $description = $this->repository->get($resource); $routes = new RouteCollection(); $router = $description->getExtension('router') ?: 'swagger.controller'; $routerController = $description->getExtension('router-controller'); foreach ($description->getPaths() as $pathItem) { $relativePath = ltrim($pathItem->getPath(), '/'); $resourceName = strpos($relativePath, '/') ? substr($relativePath, 0, strpos($relativePath, '/')) : $relativePath; $routerController = $pathItem->getExtension('router-controller') ?: $routerController; foreach ($pathItem->getOperations() as $operation) { $controllerKey = $this->resolveControllerKey($operation, $resourceName, $router, $routerController); $defaults = ['_controller' => $controllerKey, RequestMeta::ATTRIBUTE_URI => $resource, RequestMeta::ATTRIBUTE_PATH => $pathItem->getPath()]; $route = new Route($pathItem->getPath(), $defaults, $this->resolveRequirements($operation)); $route->setMethods($operation->getMethod()); $routes->add($this->createRouteId($resource, $pathItem->getPath(), $controllerKey), $route); } } $this->descriptions[] = $resource; return $routes; }
/** * @param Request $request * * @throws ValidationException * @throws MalformedContentException */ public function process(Request $request) { if (!$request->attributes->has(RequestMeta::ATTRIBUTE_URI)) { throw new \UnexpectedValueException("Missing document URI"); } $description = $this->repository->get($request->attributes->get(RequestMeta::ATTRIBUTE_URI)); $operation = $description->getPath($request->attributes->get(RequestMeta::ATTRIBUTE_PATH))->getOperation($request->getMethod()); $body = null; if ($request->getContent()) { $body = json_decode($request->getContent()); if (json_last_error() !== JSON_ERROR_NONE) { throw new MalformedContentException(json_last_error_msg()); } } $result = $this->validator->validate($operation->getRequestSchema(), $coercedParams = $this->parametersAssembler->assemble($operation, $request->query->all(), $request->attributes->all(), $request->headers->all(), $body)); foreach ($coercedParams as $attribute => $value) { /** @var ScalarSchema $schema*/ if (($schema = $operation->getParameter($attribute)->getSchema()) instanceof ScalarSchema) { if ($schema->isDateTime()) { $value = $this->dateTimeSerializer->deserialize($value, $schema); } } $request->attributes->set($attribute, $value); } if ($this->hydrator && ($bodyParam = $description->getRequestBodyParameter($operation->getPath(), $operation->getMethod()))) { $body = $this->hydrator->hydrate($body, $bodyParam->getSchema()); $request->attributes->set($bodyParam->getName(), $body); } $request->attributes->set(RequestMeta::ATTRIBUTE, new RequestMeta($description, $operation)); if (!$result->isValid()) { throw new ValidationException($result->getErrorMessages()); } }