NelmioApiDocBundle/ApiDocGenerator.php

129 lines
3.9 KiB
PHP
Raw Normal View History

2016-06-30 23:30:37 +02:00
<?php
/*
2016-12-29 12:09:26 +01:00
* This file is part of the NelmioApiDocBundle package.
2016-06-30 23:30:37 +02:00
*
2016-12-29 12:09:26 +01:00
* (c) Nelmio
2016-06-30 23:30:37 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2016-12-29 12:09:26 +01:00
namespace Nelmio\ApiDocBundle;
2016-06-30 23:30:37 +02:00
2016-12-29 12:09:26 +01:00
use Nelmio\ApiDocBundle\Describer\DescriberInterface;
2017-01-14 17:36:56 +01:00
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Model\ModelRegistry;
use Nelmio\ApiDocBundle\ModelDescriber\ModelDescriberInterface;
use Nelmio\ApiDocBundle\OpenApiPhp\ModelRegister;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Analysis;
use OpenApi\Annotations\OpenApi;
use OpenApi\Generator;
2016-11-30 16:52:13 +01:00
use Psr\Cache\CacheItemPoolInterface;
2021-08-17 21:51:11 +02:00
use Psr\Log\LoggerAwareTrait;
2016-06-30 23:30:37 +02:00
2016-11-30 15:10:01 +01:00
final class ApiDocGenerator
2016-06-30 23:30:37 +02:00
{
2021-08-17 21:51:11 +02:00
use LoggerAwareTrait;
/** @var OpenApi */
private $openApi;
2017-12-22 17:42:18 +00:00
/** @var iterable|DescriberInterface[] */
2016-07-15 00:40:30 +02:00
private $describers;
2017-12-22 17:42:18 +00:00
/** @var iterable|ModelDescriberInterface[] */
2017-01-14 17:36:56 +01:00
private $modelDescribers;
2017-12-22 17:42:18 +00:00
/** @var CacheItemPoolInterface|null */
2016-11-30 16:52:13 +01:00
private $cacheItemPool;
2016-06-30 23:30:37 +02:00
/** @var string|null */
private $cacheItemId;
/** @var string[] */
private $alternativeNames = [];
/** @var string[] */
private $mediaTypes = ['json'];
2016-06-30 23:30:37 +02:00
/**
2018-01-04 11:34:23 +01:00
* @param DescriberInterface[]|iterable $describers
* @param ModelDescriberInterface[]|iterable $modelDescribers
2016-06-30 23:30:37 +02:00
*/
public function __construct($describers, $modelDescribers, CacheItemPoolInterface $cacheItemPool = null, string $cacheItemId = null)
2016-06-30 23:30:37 +02:00
{
2016-07-15 00:40:30 +02:00
$this->describers = $describers;
2017-01-14 17:36:56 +01:00
$this->modelDescribers = $modelDescribers;
2016-11-30 16:52:13 +01:00
$this->cacheItemPool = $cacheItemPool;
$this->cacheItemId = $cacheItemId;
2016-06-30 23:30:37 +02:00
}
public function setAlternativeNames(array $alternativeNames)
{
$this->alternativeNames = $alternativeNames;
}
public function setMediaTypes(array $mediaTypes)
{
$this->mediaTypes = $mediaTypes;
}
public function generate(): OpenApi
2016-06-30 23:30:37 +02:00
{
if (null !== $this->openApi) {
return $this->openApi;
2016-07-13 23:05:14 +02:00
}
2016-11-30 16:52:13 +01:00
if ($this->cacheItemPool) {
$item = $this->cacheItemPool->getItem($this->cacheItemId ?? 'openapi_doc');
2016-11-30 16:52:13 +01:00
if ($item->isHit()) {
return $this->openApi = $item->get();
2016-11-30 16:52:13 +01:00
}
}
$generator = new Generator();
// Remove OperationId processor as we use a lot of generated annotations which do not have enough information in their context
// to generate these ids properly.
// @see https://github.com/zircote/swagger-php/issues/1153
$generator->setProcessors(array_filter($generator->getProcessors(), function ($processor) {
return !$processor instanceof \OpenApi\Processors\OperationId;
}));
$context = Util::createContext(['version' => $generator->getVersion()]);
2022-04-30 20:06:37 +02:00
$this->openApi = new OpenApi(['_context' => $context]);
$modelRegistry = new ModelRegistry($this->modelDescribers, $this->openApi, $this->alternativeNames);
2021-08-17 21:51:11 +02:00
if (null !== $this->logger) {
$modelRegistry->setLogger($this->logger);
}
2016-07-15 00:40:30 +02:00
foreach ($this->describers as $describer) {
2017-01-14 17:36:56 +01:00
if ($describer instanceof ModelRegistryAwareInterface) {
$describer->setModelRegistry($modelRegistry);
}
$describer->describe($this->openApi);
2016-06-30 23:30:37 +02:00
}
$analysis = new Analysis([], $context);
$analysis->addAnnotation($this->openApi, $context);
// Register model annotations
$modelRegister = new ModelRegister($modelRegistry, $this->mediaTypes);
$modelRegister($analysis);
// Calculate the associated schemas
$modelRegistry->registerSchemas();
2016-06-30 23:30:37 +02:00
$analysis->process($generator->getProcessors());
$analysis->validate();
2016-11-30 16:52:13 +01:00
if (isset($item)) {
$this->cacheItemPool->save($item->set($this->openApi));
2016-11-30 16:52:13 +01:00
}
return $this->openApi;
2016-06-30 23:30:37 +02:00
}
}