Add exntensible Property Describers

This commit is contained in:
Filip Benčo 2019-12-13 21:40:42 +01:00
parent f596adfb4d
commit f9eacee3fd
10 changed files with 310 additions and 30 deletions

View File

@ -17,6 +17,7 @@ use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
use Nelmio\ApiDocBundle\Model\Model;
use Nelmio\ApiDocBundle\ModelDescriber\Annotations\AnnotationsReader;
use Nelmio\ApiDocBundle\PropertyDescriber\PropertyDescriberInterface;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
use Symfony\Component\PropertyInfo\Type;
@ -24,16 +25,22 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
{
use ModelRegistryAwareTrait;
/** @var PropertyInfoExtractorInterface */
private $propertyInfo;
/** @var Reader */
private $doctrineReader;
/** @var PropertyDescriberInterface[] */
private $propertyDescribers;
private $swaggerDefinitionAnnotationReader;
public function __construct(
PropertyInfoExtractorInterface $propertyInfo,
Reader $reader
Reader $reader,
array $propertyDescribers
) {
$this->propertyInfo = $propertyInfo;
$this->propertyDescribers = $propertyDescribers;
$this->doctrineReader = $reader;
}
@ -86,39 +93,18 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
}
$type = $types[0];
if ($type->isCollection()) {
$type = $type->getCollectionValueType();
if (null === $type) {
throw new \LogicException(sprintf('Property "%s:%s" is an array, but its items type isn\'t specified. You can specify that by using the type `string[]` for instance or `@SWG\Property(type="array", @SWG\Items(type="string"))`.', $class, $propertyName));
foreach ($this->propertyDescribers as $propertyDescriber) {
if ($propertyDescriber instanceof ModelRegistryAwareInterface) {
$propertyDescriber->setModelRegistry($this->modelRegistry);
}
if ($propertyDescriber->supports($type)) {
$propertyDescriber->describe($type, $property, $model->getGroups());
$property->setType('array');
$property = $property->getItems();
break;
}
}
if (Type::BUILTIN_TYPE_STRING === $type->getBuiltinType()) {
$property->setType('string');
} elseif (Type::BUILTIN_TYPE_BOOL === $type->getBuiltinType()) {
$property->setType('boolean');
} elseif (Type::BUILTIN_TYPE_INT === $type->getBuiltinType()) {
$property->setType('integer');
} elseif (Type::BUILTIN_TYPE_FLOAT === $type->getBuiltinType()) {
$property->setType('number');
$property->setFormat('float');
} elseif (Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()) {
if (is_a($type->getClassName(), \DateTimeInterface::class, true)) {
$property->setType('string');
$property->setFormat('date-time');
} else {
$type = new Type($type->getBuiltinType(), false, $type->getClassName(), $type->isCollection(), $type->getCollectionKeyType(), $type->getCollectionValueType()); // ignore nullable field
$property->setRef(
$this->modelRegistry->register(new Model($type, $model->getGroups()))
);
}
} else {
throw new \Exception(sprintf('Type "%s" is not supported in %s::$%s. You may use the `@SWG\Property(type="")` annotation to specify it manually.', $type->getBuiltinType(), $class, $propertyName));
}
throw new \Exception(sprintf('Type "%s" is not supported in %s::$%s. You may use the `@SWG\Property(type="")` annotation to specify it manually.', $type->getBuiltinType(), $class, $propertyName));
}
}

View File

@ -0,0 +1,58 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use EXSyst\Component\Swagger\Schema;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
use Symfony\Component\PropertyInfo\Type;
class ArrayPropertyDescriber implements PropertyDescriberInterface, ModelRegistryAwareInterface
{
use ModelRegistryAwareTrait;
/** @var PropertyDescriberInterface[] */
private $propertyDescribers;
public function __construct(array $propertyDescribers)
{
$this->propertyDescribers = $propertyDescribers;
}
public function describe(Type $type, Schema $property, array $groups)
{
$type = $type->getCollectionValueType();
if (null === $type) {
throw new \LogicException(sprintf('Property "%s:%s" is an array, but its items type isn\'t specified. You can specify that by using the type `string[]` for instance or `@SWG\Property(type="array", @SWG\Items(type="string"))`.', $class, $propertyName));
}
$property->setType('array');
$property = $property->getItems();
foreach ($this->propertyDescribers as $propertyDescriber) {
if ($propertyDescriber instanceof ModelRegistryAwareInterface) {
$propertyDescriber->setModelRegistry($this->modelRegistry);
}
if ($propertyDescriber->supports($type)) {
$propertyDescriber->describe($type, $property);
break;
}
}
}
public function supports(Type $type): bool
{
return $type->isCollection();
}
}

View File

@ -0,0 +1,29 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use EXSyst\Component\Swagger\Schema;
use Symfony\Component\PropertyInfo\Type;
class BooleanPropertyDescriber implements PropertyDescriberInterface
{
public function describe(Type $type, Schema $property, array $groups)
{
$property->setType('boolean');
}
public function supports(Type $type): bool
{
return Type::BUILTIN_TYPE_BOOL === $type->getBuiltinType();
}
}

View File

@ -0,0 +1,31 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use EXSyst\Component\Swagger\Schema;
use Symfony\Component\PropertyInfo\Type;
class DateTimePropertyDescriber implements PropertyDescriberInterface
{
public function describe(Type $type, Schema $property, array $groups)
{
$property->setType('string');
$property->setFormat('date-time');
}
public function supports(Type $type): bool
{
return Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()
&& is_a($type->getClassName(), \DateTimeInterface::class, true);
}
}

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use EXSyst\Component\Swagger\Schema;
use Symfony\Component\PropertyInfo\Type;
class FloatPropertyDescriber implements PropertyDescriberInterface
{
public function describe(Type $type, Schema $property, array $groups)
{
$property->setType('number');
$property->setFormat('float');
}
public function supports(Type $type): bool
{
return Type::BUILTIN_TYPE_FLOAT === $type->getBuiltinType();
}
}

View File

@ -0,0 +1,29 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use EXSyst\Component\Swagger\Schema;
use Symfony\Component\PropertyInfo\Type;
class IntegerPropertyDescriber implements PropertyDescriberInterface
{
public function describe(Type $type, Schema $property, array $groups)
{
$property->setType('integer');
}
public function supports(Type $type): bool
{
return Type::BUILTIN_TYPE_INT === $type->getBuiltinType();
}
}

View File

@ -0,0 +1,36 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use EXSyst\Component\Swagger\Schema;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
use Nelmio\ApiDocBundle\Model\Model;
use Symfony\Component\PropertyInfo\Type;
class ObjectPropertyDescriber implements PropertyDescriberInterface
{
use ModelRegistryAwareTrait;
public function describe(Type $type, Schema $property, array $groups)
{
$type = new Type($type->getBuiltinType(), false, $type->getClassName(), $type->isCollection(), $type->getCollectionKeyType(), $type->getCollectionValueType()); // ignore nullable field
$property->setRef(
$this->modelRegistry->register(new Model($type, $groups))
);
}
public function supports(Type $type): bool
{
return Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType();
}
}

View File

@ -0,0 +1,22 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use Symfony\Component\PropertyInfo\Type;
use EXSyst\Component\Swagger\Schema;
interface PropertyDescriberInterface
{
public function describe(Type $type, Schema $property, array $groups);
public function supports(Type $type): bool;
}

View File

@ -0,0 +1,29 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use EXSyst\Component\Swagger\Schema;
use Symfony\Component\PropertyInfo\Type;
class StringPropertyDescriber implements PropertyDescriberInterface
{
public function describe(Type $type, Schema $property, array $groups)
{
$property->setType('string');
}
public function supports(Type $type): bool
{
return Type::BUILTIN_TYPE_STRING === $type->getBuiltinType();
}
}

View File

@ -41,6 +41,7 @@
<service id="nelmio_api_doc.model_describers.object" class="Nelmio\ApiDocBundle\ModelDescriber\ObjectModelDescriber" public="false">
<argument type="service" id="property_info" />
<argument type="service" id="annotation_reader" />
<argument type="tagged_iterator" id="nelmio_api_doc.property_describer" />
<tag name="nelmio_api_doc.model_describer" />
</service>
@ -49,6 +50,35 @@
<tag name="nelmio_api_doc.model_describer" priority="-1000" />
</service>
<!-- Property Describers -->
<service id="nelmio_api_doc.property_describers.array" class="Nelmio\ApiDocBundle\ModelDescriber\ArrayPropertyDescriber" public="false">
<tag name="nelmio_api_doc.property_describer" />
</service>
<service id="nelmio_api_doc.property_describers.boolean" class="Nelmio\ApiDocBundle\ModelDescriber\BooleanPropertyDescriber" public="false">
<tag name="nelmio_api_doc.property_describer" />
</service>
<service id="nelmio_api_doc.property_describers.float" class="Nelmio\ApiDocBundle\ModelDescriber\FloatPropertyDescriber" public="false">
<tag name="nelmio_api_doc.property_describer" />
</service>
<service id="nelmio_api_doc.property_describers.integer" class="Nelmio\ApiDocBundle\ModelDescriber\IntegerPropertyDescriber" public="false">
<tag name="nelmio_api_doc.property_describer" />
</service>
<service id="nelmio_api_doc.property_describers.string" class="Nelmio\ApiDocBundle\ModelDescriber\StringPropertyDescriber" public="false">
<tag name="nelmio_api_doc.property_describer" />
</service>
<service id="nelmio_api_doc.property_describers.date_time" class="Nelmio\ApiDocBundle\ModelDescriber\DateTimePropertyDescriber" public="false">
<tag name="nelmio_api_doc.property_describer" />
</service>
<service id="nelmio_api_doc.property_describers.object" class="Nelmio\ApiDocBundle\ModelDescriber\ObjectPropertyDescriber" public="false">
<tag name="nelmio_api_doc.property_describer" priority="-1000" />
</service>
<!-- Form Type Extensions -->
<service id="nelmio_api_doc.form.documentation_extension" class="Nelmio\ApiDocBundle\Form\Extension\DocumentationExtension">