NelmioApiDocBundle/ApiDocGenerator.php

115 lines
3.2 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\DefaultOperationId;
use Nelmio\ApiDocBundle\OpenApiPhp\ModelRegister;
use OpenApi\Analysis;
use OpenApi\Annotations\OpenApi;
2016-11-30 16:52:13 +01:00
use Psr\Cache\CacheItemPoolInterface;
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
{
/** @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
}
}
$this->openApi = new OpenApi([]);
$modelRegistry = new ModelRegistry($this->modelDescribers, $this->openApi, $this->alternativeNames);
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();
$analysis->addAnnotation($this->openApi, null);
// 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
$defaultOperationIdProcessor = new DefaultOperationId();
$defaultOperationIdProcessor($analysis);
$analysis->process();
$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
}
}