From f193fdb1f16fe2aece157ea5bae917dc17b898d6 Mon Sep 17 00:00:00 2001 From: Guilhem N Date: Wed, 24 Jan 2018 19:58:38 +0100 Subject: [PATCH] Inject the AnnotationsReader in Model describers instead of internal classes (#1203) --- DependencyInjection/NelmioApiDocExtension.php | 4 +- .../Annotations/AnnotationsReader.php | 45 +++++++++++++++++++ .../PropertyPhpDocReader.php} | 14 +++--- .../SwgAnnotationsReader.php} | 21 +++++++-- ModelDescriber/JMSModelDescriber.php | 26 ++++------- ModelDescriber/ObjectModelDescriber.php | 15 +++---- .../SwaggerPropertyAnnotationReader.php | 43 ------------------ Resources/config/services.xml | 17 +------ 8 files changed, 85 insertions(+), 100 deletions(-) create mode 100644 ModelDescriber/Annotations/AnnotationsReader.php rename ModelDescriber/{PhpdocPropertyAnnotationReader.php => Annotations/PropertyPhpDocReader.php} (75%) rename ModelDescriber/{SwaggerDefinitionAnnotationReader.php => Annotations/SwgAnnotationsReader.php} (56%) delete mode 100644 ModelDescriber/SwaggerPropertyAnnotationReader.php diff --git a/DependencyInjection/NelmioApiDocExtension.php b/DependencyInjection/NelmioApiDocExtension.php index 1f303f0..22ad615 100644 --- a/DependencyInjection/NelmioApiDocExtension.php +++ b/DependencyInjection/NelmioApiDocExtension.php @@ -130,9 +130,7 @@ final class NelmioApiDocExtension extends Extension implements PrependExtensionI ->setArguments([ new Reference('jms_serializer.metadata_factory'), new Reference('jms_serializer.naming_strategy'), - new Reference('nelmio_api_doc.model_describers.swagger_property_annotation_reader'), - new Reference('nelmio_api_doc.model_describers.swagger_definition_annotation_reader'), - new Reference('nelmio_api_doc.model_describers.phpdoc_property_annotation_reader'), + new Reference('annotation_reader'), ]) ->addTag('nelmio_api_doc.model_describer', ['priority' => 50]); } diff --git a/ModelDescriber/Annotations/AnnotationsReader.php b/ModelDescriber/Annotations/AnnotationsReader.php new file mode 100644 index 0000000..294d62c --- /dev/null +++ b/ModelDescriber/Annotations/AnnotationsReader.php @@ -0,0 +1,45 @@ +annotationsReader = $annotationsReader; + + $this->phpDocReader = new PropertyPhpDocReader(); + $this->swgAnnotationsReader = new SwgAnnotationsReader($annotationsReader); + } + + public function updateDefinition(\ReflectionClass $reflectionClass, Schema $schema) + { + $this->swgAnnotationsReader->updateDefinition($reflectionClass, $schema); + } + + public function updateProperty(\ReflectionProperty $reflectionProperty, Schema $property) + { + $this->phpDocReader->updateProperty($reflectionProperty, $property); + $this->swgAnnotationsReader->updateProperty($reflectionProperty, $property); + } +} diff --git a/ModelDescriber/PhpdocPropertyAnnotationReader.php b/ModelDescriber/Annotations/PropertyPhpDocReader.php similarity index 75% rename from ModelDescriber/PhpdocPropertyAnnotationReader.php rename to ModelDescriber/Annotations/PropertyPhpDocReader.php index fd19cef..1e8c6a3 100644 --- a/ModelDescriber/PhpdocPropertyAnnotationReader.php +++ b/ModelDescriber/Annotations/PropertyPhpDocReader.php @@ -9,34 +9,30 @@ * file that was distributed with this source code. */ -namespace Nelmio\ApiDocBundle\ModelDescriber; +namespace Nelmio\ApiDocBundle\ModelDescriber\Annotations; use EXSyst\Component\Swagger\Schema; use phpDocumentor\Reflection\DocBlock\Tags\Var_; use phpDocumentor\Reflection\DocBlockFactory; -use phpDocumentor\Reflection\DocBlockFactoryInterface; /** * Extract information about properties of a model from the DocBlock comment. * * @internal */ -class PhpdocPropertyAnnotationReader +class PropertyPhpDocReader { private $docBlockFactory; - public function __construct(DocBlockFactoryInterface $docBlockFactory = null) + public function __construct() { - if (null === $docBlockFactory) { - $docBlockFactory = DocBlockFactory::createInstance(); - } - $this->docBlockFactory = $docBlockFactory; + $this->docBlockFactory = DocBlockFactory::createInstance(); } /** * Update the Swagger information with information from the DocBlock comment. */ - public function updateWithPhpdoc(\ReflectionProperty $reflectionProperty, Schema $property) + public function updateProperty(\ReflectionProperty $reflectionProperty, Schema $property) { try { $docBlock = $this->docBlockFactory->create($reflectionProperty); diff --git a/ModelDescriber/SwaggerDefinitionAnnotationReader.php b/ModelDescriber/Annotations/SwgAnnotationsReader.php similarity index 56% rename from ModelDescriber/SwaggerDefinitionAnnotationReader.php rename to ModelDescriber/Annotations/SwgAnnotationsReader.php index 848a638..767ca78 100644 --- a/ModelDescriber/SwaggerDefinitionAnnotationReader.php +++ b/ModelDescriber/Annotations/SwgAnnotationsReader.php @@ -9,16 +9,17 @@ * file that was distributed with this source code. */ -namespace Nelmio\ApiDocBundle\ModelDescriber; +namespace Nelmio\ApiDocBundle\ModelDescriber\Annotations; use Doctrine\Common\Annotations\Reader; use EXSyst\Component\Swagger\Schema; use Swagger\Annotations\Definition as SwgDefinition; +use Swagger\Annotations\Property as SwgProperty; /** * @internal */ -class SwaggerDefinitionAnnotationReader +class SwgAnnotationsReader { private $annotationsReader; @@ -27,7 +28,7 @@ class SwaggerDefinitionAnnotationReader $this->annotationsReader = $annotationsReader; } - public function updateWithSwaggerDefinitionAnnotation(\ReflectionClass $reflectionClass, Schema $schema) + public function updateDefinition(\ReflectionClass $reflectionClass, Schema $schema) { /** @var SwgDefinition $swgDefinition */ if (!$swgDefinition = $this->annotationsReader->getClassAnnotation($reflectionClass, SwgDefinition::class)) { @@ -38,4 +39,18 @@ class SwaggerDefinitionAnnotationReader $schema->setRequired($swgDefinition->required); } } + + public function updateProperty(\ReflectionProperty $reflectionProperty, Schema $property) + { + /** @var SwgProperty $swgProperty */ + if (!$swgProperty = $this->annotationsReader->getPropertyAnnotation($reflectionProperty, SwgProperty::class)) { + return; + } + + if (!$swgProperty->validate()) { + return; + } + + $property->merge(json_decode(json_encode($swgProperty))); + } } diff --git a/ModelDescriber/JMSModelDescriber.php b/ModelDescriber/JMSModelDescriber.php index 45f5915..8c2d981 100644 --- a/ModelDescriber/JMSModelDescriber.php +++ b/ModelDescriber/JMSModelDescriber.php @@ -11,6 +11,7 @@ namespace Nelmio\ApiDocBundle\ModelDescriber; +use Doctrine\Common\Annotations\Reader; use EXSyst\Component\Swagger\Schema; use JMS\Serializer\Exclusion\GroupsExclusionStrategy; use JMS\Serializer\Metadata\PropertyMetadata; @@ -20,6 +21,7 @@ use Metadata\MetadataFactoryInterface; use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface; use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait; use Nelmio\ApiDocBundle\Model\Model; +use Nelmio\ApiDocBundle\ModelDescriber\Annotations\AnnotationsReader; use Symfony\Component\PropertyInfo\Type; /** @@ -30,27 +32,17 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn use ModelRegistryAwareTrait; private $factory; - private $namingStrategy; - - private $swaggerPropertyAnnotationReader; - - private $swaggerDefinitionAnnotationReader; - - private $phpdocPropertyAnnotationsReader; + private $annotationsReader; public function __construct( MetadataFactoryInterface $factory, PropertyNamingStrategyInterface $namingStrategy, - SwaggerPropertyAnnotationReader $swaggerPropertyAnnotationReader, - SwaggerDefinitionAnnotationReader $swaggerDefinitionAnnotationReader, - PhpdocPropertyAnnotationReader $phpdocPropertyAnnotationReader = null + Reader $reader ) { $this->factory = $factory; $this->namingStrategy = $namingStrategy; - $this->swaggerPropertyAnnotationReader = $swaggerPropertyAnnotationReader; - $this->swaggerDefinitionAnnotationReader = $swaggerDefinitionAnnotationReader; - $this->phpdocPropertyAnnotationsReader = $phpdocPropertyAnnotationReader; + $this->annotationsReader = new AnnotationsReader($reader); } /** @@ -67,7 +59,8 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn $groupsExclusion = null !== $model->getGroups() ? new GroupsExclusionStrategy($model->getGroups()) : null; $schema->setType('object'); - $this->swaggerDefinitionAnnotationReader->updateWithSwaggerDefinitionAnnotation(new \ReflectionClass($className), $schema); + $this->annotationsReader->updateDefinition(new \ReflectionClass($className), $schema); + $properties = $schema->getProperties(); foreach ($metadata->propertyMetadata as $item) { // filter groups @@ -80,10 +73,7 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn // read property options from Swagger Property annotation if it exists if (null !== $item->reflection) { - if ($this->phpdocPropertyAnnotationsReader) { - $this->phpdocPropertyAnnotationsReader->updateWithPhpdoc($item->reflection, $property); - } - $this->swaggerPropertyAnnotationReader->updateWithSwaggerPropertyAnnotation($item->reflection, $property); + $this->annotationsReader->updateProperty($item->reflection, $property); } if (null !== $property->getType()) { diff --git a/ModelDescriber/ObjectModelDescriber.php b/ModelDescriber/ObjectModelDescriber.php index 1dea437..820d86f 100644 --- a/ModelDescriber/ObjectModelDescriber.php +++ b/ModelDescriber/ObjectModelDescriber.php @@ -11,10 +11,12 @@ namespace Nelmio\ApiDocBundle\ModelDescriber; +use Doctrine\Common\Annotations\Reader; use EXSyst\Component\Swagger\Schema; use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface; use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait; use Nelmio\ApiDocBundle\Model\Model; +use Nelmio\ApiDocBundle\ModelDescriber\Annotations\AnnotationsReader; use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; use Symfony\Component\PropertyInfo\Type; @@ -23,19 +25,16 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar use ModelRegistryAwareTrait; private $propertyInfo; - - private $swaggerPropertyAnnotationReader; + private $annotationsReader; private $swaggerDefinitionAnnotationReader; public function __construct( PropertyInfoExtractorInterface $propertyInfo, - SwaggerPropertyAnnotationReader $swaggerPropertyAnnotationReader, - SwaggerDefinitionAnnotationReader $swaggerDefinitionAnnotationReader + Reader $reader ) { $this->propertyInfo = $propertyInfo; - $this->swaggerPropertyAnnotationReader = $swaggerPropertyAnnotationReader; - $this->swaggerDefinitionAnnotationReader = $swaggerDefinitionAnnotationReader; + $this->annotationsReader = new AnnotationsReader($reader); } public function describe(Model $model, Schema $schema) @@ -48,7 +47,7 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar if (null !== $model->getGroups()) { $context = ['serializer_groups' => $model->getGroups()]; } - $this->swaggerDefinitionAnnotationReader->updateWithSwaggerDefinitionAnnotation(new \ReflectionClass($class), $schema); + $this->annotationsReader->updateDefinition(new \ReflectionClass($class), $schema); $propertyInfoProperties = $this->propertyInfo->getProperties($class, $context); if (null === $propertyInfoProperties) { @@ -60,7 +59,7 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar // read property options from Swagger Property annotation if it exists if (property_exists($class, $propertyName)) { - $this->swaggerPropertyAnnotationReader->updateWithSwaggerPropertyAnnotation( + $this->annotationsReader->updateProperty( new \ReflectionProperty($class, $propertyName), $property ); diff --git a/ModelDescriber/SwaggerPropertyAnnotationReader.php b/ModelDescriber/SwaggerPropertyAnnotationReader.php deleted file mode 100644 index 79492c8..0000000 --- a/ModelDescriber/SwaggerPropertyAnnotationReader.php +++ /dev/null @@ -1,43 +0,0 @@ -annotationsReader = $annotationsReader; - } - - public function updateWithSwaggerPropertyAnnotation(\ReflectionProperty $reflectionProperty, Schema $property) - { - /** @var SwgProperty $swgProperty */ - if (!$swgProperty = $this->annotationsReader->getPropertyAnnotation($reflectionProperty, SwgProperty::class)) { - return; - } - - if (!$swgProperty->validate()) { - return; - } - - $property->merge(\json_decode(\json_encode($swgProperty))); - } -} diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 97def1b..ecceef3 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -39,24 +39,9 @@ - - - - - - - - - - - - +