NelmioApiDocBundle/Model/ModelRegistry.php

223 lines
7.6 KiB
PHP
Raw Normal View History

2017-01-14 17:36:56 +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.
*/
namespace Nelmio\ApiDocBundle\Model;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\ModelDescriber\ModelDescriberInterface;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
2021-08-17 21:51:11 +02:00
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
2017-01-14 17:36:56 +01:00
use Symfony\Component\PropertyInfo\Type;
final class ModelRegistry
{
2021-08-17 21:51:11 +02:00
use LoggerAwareTrait;
private $registeredModelNames = [];
private $alternativeNames = [];
2017-01-14 17:36:56 +01:00
private $unregistered = [];
2017-12-22 17:42:18 +00:00
2017-01-14 17:36:56 +01:00
private $models = [];
2017-12-22 17:42:18 +00:00
2017-01-14 17:36:56 +01:00
private $names = [];
2017-12-22 17:42:18 +00:00
2017-01-14 17:36:56 +01:00
private $modelDescribers = [];
2017-12-22 17:42:18 +00:00
2017-01-14 17:36:56 +01:00
private $api;
/**
2018-01-04 11:34:23 +01:00
* @param ModelDescriberInterface[]|iterable $modelDescribers
2017-01-14 17:36:56 +01:00
*
* @internal
*/
public function __construct($modelDescribers, OA\OpenApi $api, array $alternativeNames = [])
2017-01-14 17:36:56 +01:00
{
$this->modelDescribers = $modelDescribers;
$this->api = $api;
2021-08-17 21:51:11 +02:00
$this->logger = new NullLogger();
foreach (array_reverse($alternativeNames) as $alternativeName => $criteria) {
$this->alternativeNames[] = $model = new Model(new Type('object', false, $criteria['type']), $criteria['groups']);
$this->names[$model->getHash()] = $alternativeName;
2021-08-17 21:51:11 +02:00
$this->registeredModelNames[$alternativeName] = $model;
Util::getSchema($this->api, $alternativeName);
}
2017-01-14 17:36:56 +01:00
}
public function register(Model $model): string
{
$hash = $model->getHash();
if (!isset($this->models[$hash])) {
$this->models[$hash] = $model;
$this->unregistered[] = $hash;
}
if (!isset($this->names[$hash])) {
$this->names[$hash] = $this->generateModelName($model);
2021-08-17 21:51:11 +02:00
$this->registeredModelNames[$this->names[$hash]] = $model;
2017-01-14 17:36:56 +01:00
}
// Reserve the name
Util::getSchema($this->api, $this->names[$hash]);
2017-01-14 17:36:56 +01:00
return OA\Components::SCHEMA_REF.$this->names[$hash];
2017-01-14 17:36:56 +01:00
}
/**
* @internal
*/
public function registerSchemas(): void
2017-01-14 17:36:56 +01:00
{
while (count($this->unregistered)) {
$tmp = [];
foreach ($this->unregistered as $hash) {
$tmp[$this->names[$hash]] = $this->models[$hash];
}
$this->unregistered = [];
foreach ($tmp as $name => $model) {
$schema = null;
foreach ($this->modelDescribers as $modelDescriber) {
if ($modelDescriber instanceof ModelRegistryAwareInterface) {
$modelDescriber->setModelRegistry($this);
}
if ($modelDescriber->supports($model)) {
$schema = Util::getSchema($this->api, $name);
2017-01-14 17:36:56 +01:00
$modelDescriber->describe($model, $schema);
break;
}
}
if (null === $schema) {
$errorMessage = sprintf('Schema of type "%s" can\'t be generated, no describer supports it.', $this->typeToString($model->getType()));
if (Type::BUILTIN_TYPE_OBJECT === $model->getType()->getBuiltinType() && !class_exists($className = $model->getType()->getClassName())) {
$errorMessage .= sprintf(' Class "\\%s" does not exist, did you forget a use statement, or typed it wrong?', $className);
}
throw new \LogicException($errorMessage);
2017-01-14 17:36:56 +01:00
}
}
2018-11-04 12:24:45 +01:00
}
2018-11-04 12:24:45 +01:00
if (empty($this->unregistered) && !empty($this->alternativeNames)) {
foreach ($this->alternativeNames as $model) {
$this->register($model);
}
2018-11-04 12:24:45 +01:00
$this->alternativeNames = [];
$this->registerSchemas();
2017-01-14 17:36:56 +01:00
}
}
private function generateModelName(Model $model): string
{
$name = $base = $this->getTypeShortName($model->getType());
$names = array_column(
$this->api->components instanceof OA\Components && is_array($this->api->components->schemas) ? $this->api->components->schemas : [],
'schema'
);
2017-01-14 17:36:56 +01:00
$i = 1;
while (\in_array($name, $names, true)) {
2021-08-17 21:51:11 +02:00
if (isset($this->registeredModelNames[$name])) {
$this->logger->info(sprintf('Can not assign a name for the model, the name "%s" has already been taken.', $name), [
'model' => $this->modelToArray($model),
'taken_by' => $this->modelToArray($this->registeredModelNames[$name]),
]);
}
2017-01-14 17:36:56 +01:00
++$i;
$name = $base.$i;
}
return $name;
}
2021-08-17 21:51:11 +02:00
private function modelToArray(Model $model): array
{
$getType = function (Type $type) use (&$getType) {
return [
'class' => $type->getClassName(),
'built_in_type' => $type->getBuiltinType(),
'nullable' => $type->isNullable(),
'collection' => $type->isCollection(),
'collection_key_types' => $type->isCollection() ? array_map($getType, $this->getCollectionKeyTypes($type)) : null,
'collection_value_types' => $type->isCollection() ? array_map($getType, $this->getCollectionValueTypes($type)) : null,
2021-08-17 21:51:11 +02:00
];
};
return [
'type' => $getType($model->getType()),
'options' => $model->getOptions(),
'groups' => $model->getGroups(),
];
}
2017-03-17 19:37:41 +01:00
private function getTypeShortName(Type $type): string
2017-01-14 17:36:56 +01:00
{
if (null !== $collectionType = $this->getCollectionValueType($type)) {
return $this->getTypeShortName($collectionType).'[]';
2017-01-14 17:36:56 +01:00
}
2017-03-17 19:37:41 +01:00
2017-01-14 17:36:56 +01:00
if (Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()) {
$parts = explode('\\', $type->getClassName());
return end($parts);
}
return $type->getBuiltinType();
}
private function typeToString(Type $type): string
{
if (Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()) {
return '\\'.$type->getClassName();
2017-01-14 17:36:56 +01:00
} elseif ($type->isCollection()) {
if (null !== $collectionType = $this->getCollectionValueType($type)) {
return $this->typeToString($collectionType).'[]';
2017-01-14 17:36:56 +01:00
} else {
return 'mixed[]';
}
} else {
return $type->getBuiltinType();
}
}
private function getCollectionKeyTypes(Type $type): array
{
// BC layer, this condition should be removed after removing support for symfony < 5.3
if (!method_exists($type, 'getCollectionKeyTypes')) {
return null !== $type->getCollectionKeyType() ? [$type->getCollectionKeyType()] : [];
}
return $type->getCollectionKeyTypes();
}
private function getCollectionValueTypes(Type $type): array
{
// BC layer, this condition should be removed after removing support for symfony < 5.3
if (!method_exists($type, 'getCollectionValueTypes')) {
return null !== $type->getCollectionValueType() ? [$type->getCollectionValueType()] : [];
}
return $type->getCollectionValueTypes();
}
private function getCollectionValueType(Type $type): ?Type
{
// BC layer, this condition should be removed after removing support for symfony < 5.3
if (!method_exists($type, 'getCollectionValueTypes')) {
return $type->getCollectionValueType();
}
return $type->getCollectionValueTypes()[0] ?? null;
}
2017-01-14 17:36:56 +01:00
}