vendor/nelmio/api-doc-bundle/SwaggerPhp/ModelRegister.php line 89

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the NelmioApiDocBundle package.
  4.  *
  5.  * (c) Nelmio
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Nelmio\ApiDocBundle\SwaggerPhp;
  11. use Nelmio\ApiDocBundle\Annotation\Model as ModelAnnotation;
  12. use Nelmio\ApiDocBundle\Model\Model;
  13. use Nelmio\ApiDocBundle\Model\ModelRegistry;
  14. use Swagger\Analysis;
  15. use Swagger\Annotations\AbstractAnnotation;
  16. use Swagger\Annotations\Items;
  17. use Swagger\Annotations\Parameter;
  18. use Swagger\Annotations\Response;
  19. use Swagger\Annotations\Schema;
  20. use Symfony\Component\PropertyInfo\Type;
  21. /**
  22.  * Resolves the path in SwaggerPhp annotation when needed.
  23.  *
  24.  * @internal
  25.  */
  26. final class ModelRegister
  27. {
  28.     private $modelRegistry;
  29.     public function __construct(ModelRegistry $modelRegistry)
  30.     {
  31.         $this->modelRegistry $modelRegistry;
  32.     }
  33.     public function __invoke(Analysis $analysis, array $parentGroups null)
  34.     {
  35.         $modelsRegistered = [];
  36.         foreach ($analysis->annotations as $annotation) {
  37.             // @Model using the ref field
  38.             if ($annotation instanceof Schema && $annotation->ref instanceof ModelAnnotation) {
  39.                 $model $annotation->ref;
  40.                 $annotation->ref $this->modelRegistry->register(new Model($this->createType($model->type), $this->getGroups($model$parentGroups), $model->options));
  41.                 // It is no longer an unmerged annotation
  42.                 $this->detach($model$annotation$analysis);
  43.                 continue;
  44.             }
  45.             // Implicit usages
  46.             if ($annotation instanceof Response) {
  47.                 $annotationClass Schema::class;
  48.             } elseif ($annotation instanceof Parameter) {
  49.                 if ('array' === $annotation->type) {
  50.                     $annotationClass Items::class;
  51.                 } else {
  52.                     $annotationClass Schema::class;
  53.                 }
  54.             } elseif ($annotation instanceof Schema) {
  55.                 $annotationClass Items::class;
  56.             } else {
  57.                 continue;
  58.             }
  59.             $model null;
  60.             foreach ($annotation->_unmerged as $unmerged) {
  61.                 if ($unmerged instanceof ModelAnnotation) {
  62.                     $model $unmerged;
  63.                     break;
  64.                 }
  65.             }
  66.             if (null === $model || !$model instanceof ModelAnnotation) {
  67.                 continue;
  68.             }
  69.             if (!is_string($model->type)) {
  70.                 // Ignore invalid annotations, they are validated later
  71.                 continue;
  72.             }
  73.             if ($annotation instanceof Schema) {
  74.                 @trigger_error(sprintf('Using `@Model` implicitly in a `@SWG\Schema`, `@SWG\Items` or `@SWG\Property` annotation in %s is deprecated since version 3.2 and won\'t be supported in 4.0. Use `ref=@Model()` instead.'$annotation->_context->getDebugLocation()), E_USER_DEPRECATED);
  75.             }
  76.             $annotation->merge([new $annotationClass([
  77.                 'ref' => $this->modelRegistry->register(new Model($this->createType($model->type), $this->getGroups($model$parentGroups), $model->options)),
  78.             ])]);
  79.             // It is no longer an unmerged annotation
  80.             $this->detach($model$annotation$analysis);
  81.         }
  82.     }
  83.     private function getGroups(ModelAnnotation $model, array $parentGroups null)
  84.     {
  85.         if (null === $model->groups) {
  86.             return $parentGroups;
  87.         }
  88.         return array_merge($parentGroups ?? [], $model->groups);
  89.     }
  90.     private function detach(ModelAnnotation $modelAbstractAnnotation $annotationAnalysis $analysis)
  91.     {
  92.         foreach ($annotation->_unmerged as $key => $unmerged) {
  93.             if ($unmerged === $model) {
  94.                 unset($annotation->_unmerged[$key]);
  95.                 break;
  96.             }
  97.         }
  98.         $analysis->annotations->detach($model);
  99.     }
  100.     private function createType(string $type): Type
  101.     {
  102.         if ('[]' === substr($type, -2)) {
  103.             return new Type(Type::BUILTIN_TYPE_ARRAYfalsenulltruenull$this->createType(substr($type0, -2)));
  104.         }
  105.         return new Type(Type::BUILTIN_TYPE_OBJECTfalse$type);
  106.     }
  107. }