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;
|
2020-07-24 08:37:58 +02:00
|
|
|
use Nelmio\ApiDocBundle\OpenApiPhp\DefaultOperationId;
|
2020-07-06 19:50:34 +02:00
|
|
|
use Nelmio\ApiDocBundle\OpenApiPhp\ModelRegister;
|
2021-08-18 07:52:21 +12:00
|
|
|
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
|
2020-07-06 19:50:34 +02:00
|
|
|
use OpenApi\Analysis;
|
2020-05-28 13:19:11 +02:00
|
|
|
use OpenApi\Annotations\OpenApi;
|
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;
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
/** @var OpenApi */
|
|
|
|
private $openApi;
|
2017-12-22 17:42:18 +00:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
/** @var iterable|DescriberInterface[] */
|
2016-07-15 00:40:30 +02:00
|
|
|
private $describers;
|
2017-12-22 17:42:18 +00:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
/** @var iterable|ModelDescriberInterface[] */
|
2017-01-14 17:36:56 +01:00
|
|
|
private $modelDescribers;
|
2017-12-22 17:42:18 +00:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
/** @var CacheItemPoolInterface|null */
|
2016-11-30 16:52:13 +01:00
|
|
|
private $cacheItemPool;
|
2016-06-30 23:30:37 +02:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
/** @var string|null */
|
|
|
|
private $cacheItemId;
|
|
|
|
|
|
|
|
/** @var string[] */
|
2018-06-10 09:56:38 +02:00
|
|
|
private $alternativeNames = [];
|
|
|
|
|
2020-07-06 19:50:34 +02:00
|
|
|
/** @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
|
|
|
*/
|
2018-01-05 13:08:02 +01: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;
|
2018-01-05 13:08:02 +01:00
|
|
|
$this->cacheItemId = $cacheItemId;
|
2016-06-30 23:30:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 09:56:38 +02:00
|
|
|
public function setAlternativeNames(array $alternativeNames)
|
|
|
|
{
|
|
|
|
$this->alternativeNames = $alternativeNames;
|
|
|
|
}
|
|
|
|
|
2020-07-06 19:50:34 +02:00
|
|
|
public function setMediaTypes(array $mediaTypes)
|
|
|
|
{
|
|
|
|
$this->mediaTypes = $mediaTypes;
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
public function generate(): OpenApi
|
2016-06-30 23:30:37 +02:00
|
|
|
{
|
2020-05-28 13:19:11 +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) {
|
2020-05-28 13:19:11 +02:00
|
|
|
$item = $this->cacheItemPool->getItem($this->cacheItemId ?? 'openapi_doc');
|
2016-11-30 16:52:13 +01:00
|
|
|
if ($item->isHit()) {
|
2020-05-28 13:19:11 +02:00
|
|
|
return $this->openApi = $item->get();
|
2016-11-30 16:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->openApi = new OpenApi([]);
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$describer->describe($this->openApi);
|
2016-06-30 23:30:37 +02:00
|
|
|
}
|
2020-07-06 19:50:34 +02:00
|
|
|
|
2021-08-18 07:52:21 +12:00
|
|
|
$context = Util::createContext();
|
|
|
|
$analysis = new Analysis([], $context);
|
|
|
|
$analysis->addAnnotation($this->openApi, $context);
|
2020-07-06 19:50:34 +02:00
|
|
|
|
|
|
|
// Register model annotations
|
|
|
|
$modelRegister = new ModelRegister($modelRegistry, $this->mediaTypes);
|
|
|
|
$modelRegister($analysis);
|
|
|
|
|
|
|
|
// Calculate the associated schemas
|
2020-05-28 13:19:11 +02:00
|
|
|
$modelRegistry->registerSchemas();
|
2016-06-30 23:30:37 +02:00
|
|
|
|
2020-07-24 08:37:58 +02:00
|
|
|
$defaultOperationIdProcessor = new DefaultOperationId();
|
|
|
|
$defaultOperationIdProcessor($analysis);
|
|
|
|
|
2020-07-06 19:50:34 +02:00
|
|
|
$analysis->process();
|
|
|
|
$analysis->validate();
|
|
|
|
|
2016-11-30 16:52:13 +01:00
|
|
|
if (isset($item)) {
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->cacheItemPool->save($item->set($this->openApi));
|
2016-11-30 16:52:13 +01:00
|
|
|
}
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
return $this->openApi;
|
2016-06-30 23:30:37 +02:00
|
|
|
}
|
|
|
|
}
|