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-08-01 19:58:57 +02:00
|
|
|
use EXSyst\Component\Swagger\Swagger;
|
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;
|
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
|
|
|
{
|
2016-07-13 23:05:14 +02:00
|
|
|
private $swagger;
|
2017-12-22 17:42:18 +00:00
|
|
|
|
2016-07-15 00:40:30 +02:00
|
|
|
private $describers;
|
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
|
|
|
|
2016-11-30 16:52:13 +01:00
|
|
|
private $cacheItemPool;
|
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
|
|
|
}
|
|
|
|
|
2016-11-18 18:00:09 +01:00
|
|
|
public function generate(): Swagger
|
2016-06-30 23:30:37 +02:00
|
|
|
{
|
2016-07-13 23:05:14 +02:00
|
|
|
if (null !== $this->swagger) {
|
|
|
|
return $this->swagger;
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:52:13 +01:00
|
|
|
if ($this->cacheItemPool) {
|
2018-01-05 13:08:02 +01:00
|
|
|
$item = $this->cacheItemPool->getItem($this->cacheItemId ?? 'swagger_doc');
|
2016-11-30 16:52:13 +01:00
|
|
|
if ($item->isHit()) {
|
|
|
|
return $this->swagger = $item->get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:05:14 +02:00
|
|
|
$this->swagger = new Swagger();
|
2017-01-14 17:36:56 +01:00
|
|
|
$modelRegistry = new ModelRegistry($this->modelDescribers, $this->swagger);
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-15 00:40:30 +02:00
|
|
|
$describer->describe($this->swagger);
|
2016-06-30 23:30:37 +02:00
|
|
|
}
|
2017-01-14 17:36:56 +01:00
|
|
|
$modelRegistry->registerDefinitions();
|
2016-06-30 23:30:37 +02:00
|
|
|
|
2016-11-30 16:52:13 +01:00
|
|
|
if (isset($item)) {
|
|
|
|
$this->cacheItemPool->save($item->set($this->swagger));
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:05:14 +02:00
|
|
|
return $this->swagger;
|
2016-06-30 23:30:37 +02:00
|
|
|
}
|
|
|
|
}
|