NelmioApiDocBundle/ModelDescriber/ObjectModelDescriber.php
2019-12-13 21:40:42 +01:00

116 lines
4.6 KiB
PHP

<?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\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 Nelmio\ApiDocBundle\PropertyDescriber\PropertyDescriberInterface;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
use Symfony\Component\PropertyInfo\Type;
class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwareInterface
{
use ModelRegistryAwareTrait;
/** @var PropertyInfoExtractorInterface */
private $propertyInfo;
/** @var Reader */
private $doctrineReader;
/** @var PropertyDescriberInterface[] */
private $propertyDescribers;
private $swaggerDefinitionAnnotationReader;
public function __construct(
PropertyInfoExtractorInterface $propertyInfo,
Reader $reader,
array $propertyDescribers
) {
$this->propertyInfo = $propertyInfo;
$this->propertyDescribers = $propertyDescribers;
$this->doctrineReader = $reader;
}
public function describe(Model $model, Schema $schema)
{
$schema->setType('object');
$properties = $schema->getProperties();
$class = $model->getType()->getClassName();
$context = [];
if (null !== $model->getGroups()) {
$context = ['serializer_groups' => array_filter($model->getGroups(), 'is_string')];
}
$annotationsReader = new AnnotationsReader($this->doctrineReader, $this->modelRegistry);
$annotationsReader->updateDefinition(new \ReflectionClass($class), $schema);
$propertyInfoProperties = $this->propertyInfo->getProperties($class, $context);
if (null === $propertyInfoProperties) {
return;
}
foreach ($propertyInfoProperties as $propertyName) {
// read property options from Swagger Property annotation if it exists
if (property_exists($class, $propertyName)) {
$reflectionProperty = new \ReflectionProperty($class, $propertyName);
$property = $properties->get($annotationsReader->getPropertyName($reflectionProperty, $propertyName));
$groups = $model->getGroups();
if (isset($groups[$propertyName]) && is_array($groups[$propertyName])) {
$groups = $model->getGroups()[$propertyName];
}
$annotationsReader->updateProperty($reflectionProperty, $property, $groups);
} else {
$property = $properties->get($propertyName);
}
// If type manually defined
if (null !== $property->getType() || null !== $property->getRef()) {
continue;
}
$types = $this->propertyInfo->getTypes($class, $propertyName);
if (null === $types || 0 === count($types)) {
throw new \LogicException(sprintf('The PropertyInfo component was not able to guess the type of %s::$%s. You may need to add a `@var` annotation or use `@SWG\Property(type="")` to make its type explicit.', $class, $propertyName));
}
if (count($types) > 1) {
throw new \LogicException(sprintf('Property %s::$%s defines more than one type. You can specify the one that should be documented using `@SWG\Property(type="")`.', $class, $propertyName));
}
$type = $types[0];
foreach ($this->propertyDescribers as $propertyDescriber) {
if ($propertyDescriber instanceof ModelRegistryAwareInterface) {
$propertyDescriber->setModelRegistry($this->modelRegistry);
}
if ($propertyDescriber->supports($type)) {
$propertyDescriber->describe($type, $property, $model->getGroups());
break;
}
}
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));
}
}
public function supports(Model $model): bool
{
return Type::BUILTIN_TYPE_OBJECT === $model->getType()->getBuiltinType() && class_exists($model->getType()->getClassName());
}
}