/** * @param $route * @return array */ private function addRouteDefinition(\DC\Router\IRoute $route) { $path = $this->simplifyPathForSwagger($route->getPath()); $method = strtolower($route->getMethod()); $callable = $route->getCallable(); $reflection = $this->reflector->getReflectionFunctionForCallable($callable); if ($reflection instanceof \ReflectionMethod) { if (!$reflection->getDeclaringClass()->implementsInterface('\\DC\\Router\\Swagger\\ISwaggerAPI')) { return; } } $phpdoc = new \phpDocumentor\Reflection\DocBlock($reflection); if (count($phpdoc->getTagsByName(SwaggerExcludeTag::$name)) > 0) { return; } $routeDef = ["produces" => ["application/json"], "responses" => []]; if ($method == "post" || $method == "put") { $routeDef["consumes"] = ["application/json"]; } if ($reflection instanceof \ReflectionMethod) { $reflectionClass = $reflection->getDeclaringClass(); $routeDef["tags"] = [$this->addTagForController($reflectionClass)]; } $parameters = $this->routeMatcher->getParameterInfo($route); $paramTags = $phpdoc->getTagsByName("param"); $reflectionParameters = $reflection->getParameters(); $reflectionParametersByName = []; foreach ($reflectionParameters as $parameter) { $reflectionParametersByName[$parameter->getName()] = $parameter; } foreach ($parameters as $parameter) { $matchingParamTags = array_filter($paramTags, function ($t) use($parameter) { return $t->getVariableName() == '$' . $parameter->getInternalName(); }); $paramTag = reset($matchingParamTags); $reflectionParameter = $reflectionParametersByName[$parameter->getInternalName()]; $required = !isset($reflectionParameter) || !$reflectionParameter->isOptional(); $paramDef = ["name" => $parameter->getQueryName(), "in" => $parameter->getPlacement(), "required" => $required]; if ($paramTag != null) { $paramDef["description"] = $paramTag->getDescription(); $paramDef += $this->getTypeDefinition($paramTag->getType()); } else { $paramDef["type"] = "string"; } $routeDef["parameters"][] = $paramDef; } /** * @var $returnTag \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag[] */ $returnTag = $phpdoc->getTagsByName("return"); if (count($returnTag) > 0) { $returnTypes = $returnTag[0]->getTypes(); if (!isset($returnTypes)) { $returnTypes = [$returnTag[0]->getType()]; } foreach ($returnTypes as $type) { $defRef = $this->getTypeDefinitionOrReference($type); $routeDef["responses"][200] = ["schema" => $defRef, "description" => $returnTag[0]->getDescription()]; } } /** @var $throwsTags \phpDocumentor\Reflection\DocBlock\Tag\ThrowsTag[] */ $throwsTags = $phpdoc->getTagsByName("throws"); if (count($throwsTags) > 0) { foreach ($throwsTags as $throwsTag) { $type = $throwsTag->getType(); $defRef = $this->getTypeDefinitionOrReference($type); $routeDef["responses"][500] = ["schema" => $defRef, "description" => $throwsTag->getDescription()]; } } $routeDef["summary"] = $phpdoc->getShortDescription(); $description = $phpdoc->getLongDescription()->getContents(); if (strlen($description) > 0) { $routeDef["description"] = $description; } $this->def['paths'][$path][$method] = $routeDef; }