2020-05-28 13:19:11 +02:00
|
|
|
<?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\Annotations;
|
|
|
|
|
|
|
|
use Doctrine\Common\Annotations\Reader;
|
|
|
|
use Nelmio\ApiDocBundle\Model\ModelRegistry;
|
|
|
|
use Nelmio\ApiDocBundle\OpenApiPhp\ModelRegister;
|
2021-08-18 07:52:21 +12:00
|
|
|
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
|
2020-05-29 21:52:06 +02:00
|
|
|
use OpenApi\Analyser;
|
2020-05-28 13:19:11 +02:00
|
|
|
use OpenApi\Analysis;
|
|
|
|
use OpenApi\Annotations as OA;
|
|
|
|
use OpenApi\Context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
class OpenApiAnnotationsReader
|
|
|
|
{
|
|
|
|
private $annotationsReader;
|
|
|
|
private $modelRegister;
|
|
|
|
|
|
|
|
public function __construct(Reader $annotationsReader, ModelRegistry $modelRegistry, array $mediaTypes)
|
|
|
|
{
|
|
|
|
$this->annotationsReader = $annotationsReader;
|
|
|
|
$this->modelRegister = new ModelRegister($modelRegistry, $mediaTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateSchema(\ReflectionClass $reflectionClass, OA\Schema $schema): void
|
|
|
|
{
|
|
|
|
/** @var OA\Schema $oaSchema */
|
|
|
|
if (!$oaSchema = $this->annotationsReader->getClassAnnotation($reflectionClass, OA\Schema::class)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read @Model annotations
|
2021-08-18 07:52:21 +12:00
|
|
|
$this->modelRegister->__invoke(new Analysis([$oaSchema], Util::createContext()));
|
2020-05-28 13:19:11 +02:00
|
|
|
|
|
|
|
if (!$oaSchema->validate()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$schema->mergeProperties($oaSchema);
|
|
|
|
}
|
|
|
|
|
2020-07-12 15:04:20 +02:00
|
|
|
public function getPropertyName($reflection, string $default): string
|
2020-05-28 13:19:11 +02:00
|
|
|
{
|
|
|
|
/** @var OA\Property $oaProperty */
|
2020-07-12 15:04:20 +02:00
|
|
|
if ($reflection instanceof \ReflectionProperty && !$oaProperty = $this->annotationsReader->getPropertyAnnotation($reflection, OA\Property::class)) {
|
|
|
|
return $default;
|
|
|
|
} elseif ($reflection instanceof \ReflectionMethod && !$oaProperty = $this->annotationsReader->getMethodAnnotation($reflection, OA\Property::class)) {
|
2020-05-28 13:19:11 +02:00
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OA\UNDEFINED !== $oaProperty->property ? $oaProperty->property : $default;
|
|
|
|
}
|
|
|
|
|
2020-07-12 15:04:20 +02:00
|
|
|
public function updateProperty($reflection, OA\Property $property, array $serializationGroups = null): void
|
2020-05-28 13:19:11 +02:00
|
|
|
{
|
2020-05-29 21:52:06 +02:00
|
|
|
// In order to have nicer errors
|
2020-07-12 15:04:20 +02:00
|
|
|
$declaringClass = $reflection->getDeclaringClass();
|
2020-05-29 21:52:06 +02:00
|
|
|
Analyser::$context = new Context([
|
2020-05-28 13:19:11 +02:00
|
|
|
'namespace' => $declaringClass->getNamespaceName(),
|
|
|
|
'class' => $declaringClass->getShortName(),
|
2020-07-12 15:04:20 +02:00
|
|
|
'property' => $reflection->name,
|
2020-05-28 13:19:11 +02:00
|
|
|
'filename' => $declaringClass->getFileName(),
|
|
|
|
]);
|
2020-05-29 21:52:06 +02:00
|
|
|
|
|
|
|
/** @var OA\Property $oaProperty */
|
2020-07-12 15:04:20 +02:00
|
|
|
if ($reflection instanceof \ReflectionProperty && !$oaProperty = $this->annotationsReader->getPropertyAnnotation($reflection, OA\Property::class)) {
|
|
|
|
return;
|
|
|
|
} elseif ($reflection instanceof \ReflectionMethod && !$oaProperty = $this->annotationsReader->getMethodAnnotation($reflection, OA\Property::class)) {
|
2020-05-29 21:52:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Analyser::$context = null;
|
2020-05-28 13:19:11 +02:00
|
|
|
|
|
|
|
// Read @Model annotations
|
2021-08-18 07:52:21 +12:00
|
|
|
$this->modelRegister->__invoke(new Analysis([$oaProperty], Util::createContext()), $serializationGroups);
|
2020-05-28 13:19:11 +02:00
|
|
|
|
|
|
|
if (!$oaProperty->validate()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$property->mergeProperties($oaProperty);
|
|
|
|
}
|
|
|
|
}
|