mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
Merge pull request #43 from willdurand/fix-13
Add support for FOSRest annotations. Fix #13
This commit is contained in:
commit
0868d39e9a
@ -54,7 +54,7 @@ class ApiDoc
|
||||
$name = $filter['name'];
|
||||
unset($filter['name']);
|
||||
|
||||
$this->filters[$name] = $filter;
|
||||
$this->addFilter($name, $filter);
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,6 +73,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
|
||||
*/
|
||||
|
@ -22,6 +22,8 @@ class ApiDocExtractor
|
||||
{
|
||||
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:
|
||||
@ -205,7 +231,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)
|
||||
|
@ -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' => '');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Value</th>
|
||||
<th>Requirement</th>
|
||||
<th>Type</th>
|
||||
<th>Description</th>
|
||||
</tr>
|
||||
@ -43,7 +43,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>
|
||||
@ -66,11 +66,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 %}
|
||||
|
Loading…
x
Reference in New Issue
Block a user