Merge branch 'master' of https://github.com/nelmio/NelmioApiDocBundle into feature-cache

This commit is contained in:
Florent DUBOST 2013-03-27 15:46:28 +01:00
commit 5ec931185f
6 changed files with 52 additions and 12 deletions

View File

@ -70,11 +70,21 @@ class ApiDocExtractor
* You can extend this method if you don't want all the routes * You can extend this method if you don't want all the routes
* to be included. * to be included.
* *
* @return array of Route * @return \Traversable Iterator for a RouteCollection
*/ */
public function getRoutes() public function getRoutes()
{ {
return $this->router->getRouteCollection()->all(); return $this->router->getRouteCollection()->getIterator();
}
/**
* Extracts annotations from all known routes
*
* @return array
*/
public function all()
{
return $this->extractAnnotations($this->getRoutes());
} }
/** /**
@ -82,14 +92,16 @@ class ApiDocExtractor
* - annotation * - annotation
* - resource * - resource
* *
* @param \Traversable $routes The routes for which the annotations should be extracted
*
* @return array * @return array
*/ */
public function all() public function extractAnnotations(\Traversable $routes)
{ {
$array = array(); $array = array();
$resources = array(); $resources = array();
foreach ($this->getRoutes() as $route) { foreach ($routes as $route) {
if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) { if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) {
if ($annotation = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS)) { if ($annotation = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS)) {
if ($annotation->isResource()) { if ($annotation->isResource()) {

View File

@ -15,6 +15,7 @@ use Metadata\MetadataFactoryInterface;
use Nelmio\ApiDocBundle\Util\DocCommentExtractor; use Nelmio\ApiDocBundle\Util\DocCommentExtractor;
use JMS\Serializer\Metadata\PropertyMetadata; use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Metadata\VirtualPropertyMetadata; use JMS\Serializer\Metadata\VirtualPropertyMetadata;
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;
/** /**
* Uses the JMS metadata factory to extract input/output model information * Uses the JMS metadata factory to extract input/output model information
@ -27,6 +28,11 @@ class JmsMetadataParser implements ParserInterface
*/ */
private $factory; private $factory;
/**
* @var PropertyNamingStrategyInterface
*/
private $namingStrategy;
/** /**
* @var \Nelmio\ApiDocBundle\Util\DocCommentExtractor * @var \Nelmio\ApiDocBundle\Util\DocCommentExtractor
*/ */
@ -35,9 +41,13 @@ class JmsMetadataParser implements ParserInterface
/** /**
* Constructor, requires JMS Metadata factory * Constructor, requires JMS Metadata factory
*/ */
public function __construct(MetadataFactoryInterface $factory, DocCommentExtractor $commentExtractor) public function __construct(
{ MetadataFactoryInterface $factory,
PropertyNamingStrategyInterface $namingStrategy,
DocCommentExtractor $commentExtractor
) {
$this->factory = $factory; $this->factory = $factory;
$this->namingStrategy = $namingStrategy;
$this->commentExtractor = $commentExtractor; $this->commentExtractor = $commentExtractor;
} }
@ -85,7 +95,7 @@ class JmsMetadataParser implements ParserInterface
// iterate over property metadata // iterate over property metadata
foreach ($meta->propertyMetadata as $item) { foreach ($meta->propertyMetadata as $item) {
if (!is_null($item->type)) { if (!is_null($item->type)) {
$name = isset($item->serializedName) ? $item->serializedName : $item->name; $name = $this->namingStrategy->translateName($item);
$dataType = $this->processDataType($item); $dataType = $this->processDataType($item);

View File

@ -10,6 +10,7 @@
<services> <services>
<service id="nelmio_api_doc.parser.jms_metadata_parser" class="%nelmio_api_doc.parser.jms_metadata_parser.class%"> <service id="nelmio_api_doc.parser.jms_metadata_parser" class="%nelmio_api_doc.parser.jms_metadata_parser.class%">
<argument type="service" id="jms_serializer.metadata_factory" /> <argument type="service" id="jms_serializer.metadata_factory" />
<argument type="service" id="jms_serializer.naming_strategy" />
<argument type="service" id="nelmio_api_doc.doc_comment_extractor" /> <argument type="service" id="nelmio_api_doc.doc_comment_extractor" />
<tag name="nelmio_api_doc.extractor.parser" /> <tag name="nelmio_api_doc.extractor.parser" />
</service> </service>

View File

@ -250,13 +250,13 @@ nested[parent][nested]:
* required: false * required: false
* description: No description. * description: No description.
nested[parent][nestedArray][]: nested[parent][nested_array][]:
* type: array of objects (JmsNested) * type: array of objects (JmsNested)
* required: false * required: false
* description: No description. * description: No description.
nestedArray[]: nested_array[]:
* type: array of objects (JmsNested) * type: array of objects (JmsNested)
* required: false * required: false

View File

@ -345,7 +345,7 @@ With multiple lines.',
'description' => 'No description.', 'description' => 'No description.',
'readonly' => false, 'readonly' => false,
), ),
'nestedArray' => 'nested_array' =>
array ( array (
'dataType' => 'array of objects (JmsNested)', 'dataType' => 'array of objects (JmsNested)',
'required' => false, 'required' => false,
@ -356,7 +356,7 @@ With multiple lines.',
), ),
), ),
), ),
'nestedArray' => 'nested_array' =>
array ( array (
'dataType' => 'array of objects (JmsNested)', 'dataType' => 'array of objects (JmsNested)',
'required' => false, 'required' => false,

View File

@ -44,6 +44,23 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase
$metadata->addPropertyMetadata($propertyMetadataBar); $metadata->addPropertyMetadata($propertyMetadataBar);
$metadata->addPropertyMetadata($propertyMetadataBaz); $metadata->addPropertyMetadata($propertyMetadataBaz);
$propertyNamingStrategy = $this->getMock('JMS\Serializer\Naming\PropertyNamingStrategyInterface');
$propertyNamingStrategy
->expects($this->at(0))
->method('translateName')
->will($this->returnValue('foo'));
$propertyNamingStrategy
->expects($this->at(1))
->method('translateName')
->will($this->returnValue('bar'));
$propertyNamingStrategy
->expects($this->at(2))
->method('translateName')
->will($this->returnValue('baz'));
$input = new JmsNested(); $input = new JmsNested();
$metadataFactory->expects($this->once()) $metadataFactory->expects($this->once())
@ -51,7 +68,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase
->with($input) ->with($input)
->will($this->returnValue($metadata)); ->will($this->returnValue($metadata));
$jmsMetadataParser = new JmsMetadataParser($metadataFactory, $docCommentExtractor); $jmsMetadataParser = new JmsMetadataParser($metadataFactory, $propertyNamingStrategy, $docCommentExtractor);
$output = $jmsMetadataParser->parse($input); $output = $jmsMetadataParser->parse($input);