public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method) { foreach ($annotations as $annot) { if ($annot instanceof Cache) { $annotation->setCache($annot->getMaxAge()); } elseif ($annot instanceof Security) { $annotation->setAuthentication(true); } } }
public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method) { foreach ($annotations as $annot) { if ($annot instanceof PreAuthorize) { $annotation->setAuthentication(true); } elseif ($annot instanceof Secure) { $annotation->setAuthentication(true); $annotation->setAuthenticationRoles(is_array($annot->roles) ? $annot->roles : explode(',', $annot->roles)); } } }
/** * @inheritdoc */ public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method) { foreach ($annotations as $annot) { if ($annot instanceof RequestParam) { $requirements = $this->handleRequirements($annot->requirements); $data = array('required' => $annot->strict && $annot->nullable === false && $annot->default === null, 'dataType' => $requirements . ($annot->array ? '[]' : ''), 'actualType' => $this->inferType($requirements), 'subType' => null, 'description' => $annot->description, 'readonly' => false); if ($annot->strict === false) { $data['default'] = $annot->default; } $annotation->addParameter($annot->name, $data); } elseif ($annot instanceof QueryParam) { if ($annot->strict && $annot->nullable === false && $annot->default === null) { $annotation->addRequirement($annot->name, array('requirement' => $this->handleRequirements($annot->requirements) . ($annot->array ? '[]' : ''), 'dataType' => '', 'description' => $annot->description)); } elseif ($annot->default !== null) { $annotation->addFilter($annot->name, array('requirement' => $this->handleRequirements($annot->requirements) . ($annot->array ? '[]' : ''), 'description' => $annot->description, 'default' => $annot->default)); } else { $annotation->addFilter($annot->name, array('requirement' => $this->handleRequirements($annot->requirements) . ($annot->array ? '[]' : ''), 'description' => $annot->description)); } } } }
/** * Builds ApiDoc annotation from DunglasApiBundle data. * * @param bool $collection * @param ResourceInterface $resource * @param OperationInterface $operation * @param array $resourceHydraDoc * @param array $entrypointHydraDoc * * @return ApiDoc */ private function getApiDoc($collection, ResourceInterface $resource, OperationInterface $operation, array $resourceHydraDoc, array $entrypointHydraDoc = []) { $method = $operation->getRoute()->getMethods()[0]; if ($collection) { $operationHydraDoc = $this->getCollectionOperationHydraDoc($resource->getShortName(), $method, $entrypointHydraDoc); } else { $operationHydraDoc = $this->getOperationHydraDoc($operation->getRoute()->getMethods()[0], $resourceHydraDoc); } $route = $operation->getRoute(); $data = ['resource' => $route->getPath(), 'description' => $operationHydraDoc['hydra:title'], 'resourceDescription' => $resourceHydraDoc['hydra:title'], 'section' => $resourceHydraDoc['hydra:title']]; $entityClass = $resource->getEntityClass(); if (isset($operationHydraDoc['expects']) && 'owl:Nothing' !== $operationHydraDoc['expects']) { $data['input'] = sprintf('%s:%s', DunglasApiParser::IN_PREFIX, $entityClass); } if (isset($operationHydraDoc['returns']) && 'owl:Nothing' !== $operationHydraDoc['returns']) { $data['output'] = sprintf('%s:%s', DunglasApiParser::OUT_PREFIX, $entityClass); } if (Request::METHOD_GET === $method && $collection) { $data['filters'] = []; foreach ($resource->getFilters() as $filter) { foreach ($filter->getDescription($resource) as $name => $definition) { $data['filters'][] = ['name' => $name] + $definition; } } } $apiDoc = new ApiDoc($data); $apiDoc->setRoute($route); return $apiDoc; }
public function testAlignmentOfOutputAndResponseModels2() { $data = array('responseMap' => array(200 => 'FooBar', 400 => 'Foo\\ValidationErrorCollection')); $apiDoc = new ApiDoc($data); $map = $apiDoc->getResponseMap(); $this->assertCount(2, $map); $this->assertArrayHasKey(200, $map); $this->assertArrayHasKey(400, $map); $this->assertEquals($apiDoc->getOutput(), $map[200]); }
/** * {@inheritdoc} */ public function formatOne(ApiDoc $annotation) { return $annotation->toArray(); }
/** * {@inheritdoc} */ public function formatOne(ApiDoc $annotation) { return $this->renderOne($this->processAnnotation($annotation->toArray())); }
public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method) { // description if (null === $annotation->getDescription()) { $comments = explode("\n", $annotation->getDocumentation()); // just set the first line $comment = trim($comments[0]); $comment = preg_replace("#\n+#", ' ', $comment); $comment = preg_replace('#\\s+#', ' ', $comment); $comment = preg_replace('#[_`*]+#', '', $comment); if ('@' !== substr($comment, 0, 1)) { $annotation->setDescription($comment); } } // schema if ($annotation->getSchema() !== null) { $path = 'file://' . realpath($this->schemaPath . $annotation->getSchema()); $properties = (array) json_decode(file_get_contents($path))->properties; $tab = []; foreach ($properties as $objectName => $property) { if (isset($property->properties)) { $tab = array_merge($tab, $annotation->schemaFormat($property->properties, $property->required, $objectName)); } else { $required = json_decode(file_get_contents($path))->required; $tab = array_merge($tab, $annotation->schemaFormat((object) [$objectName, $property], $required)); } } $annotation->setParameters($tab); } // requirements $requirements = $annotation->getRequirements(); foreach ($route->getRequirements() as $name => $value) { if (!isset($requirements[$name]) && '_method' !== $name && '_scheme' !== $name) { $requirements[$name] = array('requirement' => $value, 'dataType' => '', 'description' => ''); } if ('_scheme' === $name) { $https = 'https' == $value; $annotation->setHttps($https); } } if (method_exists($route, 'getSchemes')) { $annotation->setHttps(in_array('https', $route->getSchemes())); } $paramDocs = array(); foreach (explode("\n", $this->commentExtractor->getDocComment($method)) as $line) { if (preg_match('{^@param (.+)}', trim($line), $matches)) { $paramDocs[] = $matches[1]; } if (preg_match('{^@deprecated}', trim($line))) { $annotation->setDeprecated(true); } if (preg_match('{^@link (.+)}', trim($line), $matches)) { $annotation->setLink($matches[1]); } } $regexp = '{(\\w*) *\\$%s\\b *(.*)}i'; foreach ($route->compile()->getVariables() as $var) { $found = false; foreach ($paramDocs as $paramDoc) { if (preg_match(sprintf($regexp, preg_quote($var)), $paramDoc, $matches)) { $annotationRequirements = $annotation->getrequirements(); if (!isset($annotationRequirements[$var]['dataType'])) { $requirements[$var]['dataType'] = isset($matches[1]) ? $matches[1] : ''; } if (!isset($annotationRequirements[$var]['description'])) { $requirements[$var]['description'] = $matches[2]; } if (!isset($requirements[$var]['requirement']) && !isset($annotationRequirements[$var]['requirement'])) { $requirements[$var]['requirement'] = ''; } $found = true; break; } } if (!isset($requirements[$var]) && false === $found) { $requirements[$var] = array('requirement' => '', 'dataType' => '', 'description' => ''); } } $annotation->setRequirements($requirements); }