2018-01-24 15:20:20 +01: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.
|
|
|
|
*/
|
|
|
|
|
2018-01-24 19:58:38 +01:00
|
|
|
namespace Nelmio\ApiDocBundle\ModelDescriber\Annotations;
|
2018-01-24 15:20:20 +01:00
|
|
|
|
|
|
|
use Doctrine\Common\Annotations\Reader;
|
|
|
|
use EXSyst\Component\Swagger\Schema;
|
2018-03-17 19:23:29 +01:00
|
|
|
use Nelmio\ApiDocBundle\Model\Model;
|
|
|
|
use Nelmio\ApiDocBundle\Model\ModelRegistry;
|
|
|
|
use Nelmio\ApiDocBundle\SwaggerPhp\ModelRegister;
|
|
|
|
use Swagger\Analysis;
|
2018-01-24 15:20:20 +01:00
|
|
|
use Swagger\Annotations\Definition as SwgDefinition;
|
2018-01-24 19:58:38 +01:00
|
|
|
use Swagger\Annotations\Property as SwgProperty;
|
2018-03-17 19:23:29 +01:00
|
|
|
use Swagger\Context;
|
2018-01-24 15:20:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-01-24 19:58:38 +01:00
|
|
|
class SwgAnnotationsReader
|
2018-01-24 15:20:20 +01:00
|
|
|
{
|
|
|
|
private $annotationsReader;
|
2018-03-17 19:23:29 +01:00
|
|
|
private $modelRegister;
|
2018-01-24 15:20:20 +01:00
|
|
|
|
2018-03-17 19:23:29 +01:00
|
|
|
public function __construct(Reader $annotationsReader, ModelRegistry $modelRegistry)
|
2018-01-24 15:20:20 +01:00
|
|
|
{
|
|
|
|
$this->annotationsReader = $annotationsReader;
|
2018-03-17 19:23:29 +01:00
|
|
|
$this->modelRegister = new ModelRegister($modelRegistry);
|
2018-01-24 15:20:20 +01:00
|
|
|
}
|
|
|
|
|
2018-01-24 19:58:38 +01:00
|
|
|
public function updateDefinition(\ReflectionClass $reflectionClass, Schema $schema)
|
2018-01-24 15:20:20 +01:00
|
|
|
{
|
|
|
|
/** @var SwgDefinition $swgDefinition */
|
|
|
|
if (!$swgDefinition = $this->annotationsReader->getClassAnnotation($reflectionClass, SwgDefinition::class)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-17 19:23:29 +01:00
|
|
|
// Read @Model annotations
|
|
|
|
$this->modelRegister->__invoke(new Analysis([$swgDefinition]));
|
|
|
|
|
|
|
|
if (!$swgDefinition->validate()) {
|
|
|
|
return;
|
2018-01-24 15:20:20 +01:00
|
|
|
}
|
2018-03-17 19:23:29 +01:00
|
|
|
|
|
|
|
$schema->merge(json_decode(json_encode($swgDefinition)));
|
2018-01-24 15:20:20 +01:00
|
|
|
}
|
2018-01-24 19:58:38 +01:00
|
|
|
|
2018-02-19 21:41:05 +01:00
|
|
|
public function getPropertyName(\ReflectionProperty $reflectionProperty, string $default): string
|
|
|
|
{
|
|
|
|
/** @var SwgProperty $swgProperty */
|
|
|
|
if (!$swgProperty = $this->annotationsReader->getPropertyAnnotation($reflectionProperty, SwgProperty::class)) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $swgProperty->property ?? $default;
|
|
|
|
}
|
|
|
|
|
2018-03-17 19:23:29 +01:00
|
|
|
public function updateProperty(\ReflectionProperty $reflectionProperty, Schema $property, array $serializationGroups = null)
|
2018-01-24 19:58:38 +01:00
|
|
|
{
|
|
|
|
if (!$swgProperty = $this->annotationsReader->getPropertyAnnotation($reflectionProperty, SwgProperty::class)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-17 19:23:29 +01:00
|
|
|
$declaringClass = $reflectionProperty->getDeclaringClass();
|
|
|
|
$context = new Context([
|
|
|
|
'namespace' => $declaringClass->getNamespaceName(),
|
|
|
|
'class' => $declaringClass->getShortName(),
|
|
|
|
'property' => $reflectionProperty->name,
|
|
|
|
'filename' => $declaringClass->getFileName(),
|
|
|
|
]);
|
|
|
|
$swgProperty->_context = $context;
|
|
|
|
|
|
|
|
// Read @Model annotations
|
|
|
|
$this->modelRegister->__invoke(new Analysis([$swgProperty]), $serializationGroups);
|
|
|
|
|
2018-01-24 19:58:38 +01:00
|
|
|
if (!$swgProperty->validate()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$property->merge(json_decode(json_encode($swgProperty)));
|
|
|
|
}
|
2018-01-24 15:20:20 +01:00
|
|
|
}
|