From 163d96a295ee15555508c46b83b05e498b4ba243 Mon Sep 17 00:00:00 2001 From: William DURAND Date: Wed, 18 Jul 2012 12:23:57 +0200 Subject: [PATCH] Add support for FOSRest annotations. Fix #13 --- Annotation/ApiDoc.php | 11 ++++++++++- Extractor/ApiDocExtractor.php | 34 ++++++++++++++++++++++++++++---- Formatter/AbstractFormatter.php | 14 ++++++------- Resources/views/method.html.twig | 13 +++++++----- 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index 1d69036..ebc75fb 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -49,7 +49,7 @@ class ApiDoc $name = $filter['name']; unset($filter['name']); - $this->filters[$name] = $filter; + $this->addFilter($name, $filter); } } @@ -68,6 +68,15 @@ class ApiDoc return $this->filters; } + /** + * @param string $name + * @param array $filter + */ + public function addFilter($name, array $filter) + { + $this->filters[$name] = $filter; + } + /** * @return string|null */ diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 42ef1ef..ff120b4 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -20,7 +20,9 @@ use Symfony\Component\HttpFoundation\Request; class ApiDocExtractor { - const ANNOTATION_CLASS = 'Nelmio\\ApiDocBundle\\Annotation\\ApiDoc'; + const ANNOTATION_CLASS = 'Nelmio\\ApiDocBundle\\Annotation\\ApiDoc'; + + const FOS_REST_PARAM_CLASS = 'FOS\\RestBundle\\Controller\\Annotations\\Param'; /** * @var \Symfony\Component\DependencyInjection\ContainerInterface @@ -64,7 +66,7 @@ class ApiDocExtractor $resources[] = $route->getPattern(); } - $array[] = $this->getData($annot, $route, $method); + $array[] = $this->parseAnnotations($annot, $method, $route); } } } @@ -158,7 +160,7 @@ class ApiDocExtractor if ($method = $this->getReflectionMethod($controller)) { if ($annot = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS)) { if ($route = $this->router->getRouteCollection()->get($route)) { - return $this->getData($annot, $route, $method); + return $this->parseAnnotations($annot, $method, $route); } } } @@ -166,6 +168,30 @@ class ApiDocExtractor return null; } + protected function parseAnnotations($annotation, $method, $route) + { + $data = $this->getData($annotation, $route, $method); + + foreach ($this->reader->getMethodAnnotations($method) as $annot) { + if (is_subclass_of($annot, self::FOS_REST_PARAM_CLASS)) { + if ($annot->strict) { + $data['requirements'][$annot->name] = array( + 'requirement' => $annot->requirements, + 'type' => '', + 'description' => $annot->description, + ); + } else { + $data['annotation']->addFilter($annot->name, array( + 'requirement' => $annot->requirements, + 'description' => $annot->description, + )); + } + } + } + + return $data; + } + /** * Allows to add more data to the ApiDoc object, and * returns an array containing the following keys: @@ -202,7 +228,7 @@ class ApiDocExtractor $route->setOptions(array_merge($route->getOptions(), array('_paramDocs' => $paramDocs))); - return array('annotation' => $annotation, 'route' => $route); + return array('annotation' => $annotation, 'route' => $route, 'requirements' => array()); } protected function getDocComment(\Reflector $reflected) diff --git a/Formatter/AbstractFormatter.php b/Formatter/AbstractFormatter.php index a6f9199..938d819 100644 --- a/Formatter/AbstractFormatter.php +++ b/Formatter/AbstractFormatter.php @@ -47,7 +47,7 @@ abstract class AbstractFormatter implements FormatterInterface $array[$resource] = array(); } - $array[$resource][] = $this->getData($coll['annotation'], $coll['route']); + $array[$resource][] = $this->getData($coll['annotation'], $coll['route'], $coll['requirements']); } return $this->render($array); @@ -72,9 +72,10 @@ abstract class AbstractFormatter implements FormatterInterface /** * @param ApiDoc $apiDoc * @param Route $route + * @param array $requirements * @return array */ - protected function getData(ApiDoc $apiDoc, Route $route) + protected function getData(ApiDoc $apiDoc, Route $route, array $requirements = array()) { $method = $route->getRequirement('_method'); $data = array( @@ -82,11 +83,10 @@ abstract class AbstractFormatter implements FormatterInterface 'uri' => $route->compile()->getPattern(), ); - $requirements = array(); foreach ($route->compile()->getRequirements() as $name => $value) { if ('_method' !== $name) { $requirements[$name] = array( - 'value' => $value, + 'requirement' => $value, 'type' => '', 'description' => '', ); @@ -102,8 +102,8 @@ abstract class AbstractFormatter implements FormatterInterface $requirements[$var]['type'] = isset($matches[1]) ? $matches[1] : ''; $requirements[$var]['description'] = $matches[2]; - if (!isset($requirements[$var]['value'])) { - $requirements[$var]['value'] = ''; + if (!isset($requirements[$var]['requirement'])) { + $requirements[$var]['requirement'] = ''; } $found = true; @@ -112,7 +112,7 @@ abstract class AbstractFormatter implements FormatterInterface } if (!isset($requirements[$var]) && false === $found) { - $requirements[$var] = array('value' => '', 'type' => '', 'description' => ''); + $requirements[$var] = array('requirement' => '', 'type' => '', 'description' => ''); } } } diff --git a/Resources/views/method.html.twig b/Resources/views/method.html.twig index a59857c..6593dab 100644 --- a/Resources/views/method.html.twig +++ b/Resources/views/method.html.twig @@ -29,7 +29,7 @@ Name - Value + Requirement Type Description @@ -38,7 +38,7 @@ {% for name, infos in data.requirements %} {{ name }} - {{ infos.value }} + {{ infos.requirement }} {{ infos.type }} {{ infos.description }} @@ -61,11 +61,14 @@ {{ name }} -