Add support for FOSRest annotations. Fix #13

This commit is contained in:
William DURAND 2012-07-18 12:23:57 +02:00
parent b74273b72d
commit 163d96a295
4 changed files with 55 additions and 17 deletions

View File

@ -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
*/

View File

@ -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)

View File

@ -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' => '');
}
}
}

View File

@ -29,7 +29,7 @@
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Requirement</th>
<th>Type</th>
<th>Description</th>
</tr>
@ -38,7 +38,7 @@
{% for name, infos in data.requirements %}
<tr>
<td>{{ name }}</td>
<td>{{ infos.value }}</td>
<td>{{ infos.requirement }}</td>
<td>{{ infos.type }}</td>
<td>{{ infos.description }}</td>
</tr>
@ -61,11 +61,14 @@
<tr>
<td>{{ name }}</td>
<td>
<ul>
<table>
{% for key, value in infos %}
<li><em>{{ key }}</em> : {{ value|json_encode|trim('"') }}</li>
<tr>
<td>{{ key|title }}</td>
<td>{{ value|json_encode|trim('"') }}</td>
</tr>
{% endfor %}
</ul>
</table>
</td>
</tr>
{% endfor %}