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;
|
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;
|
2016-07-15 00:40:30 +02:00
|
|
|
private $describers;
|
2016-11-30 16:52:13 +01:00
|
|
|
private $cacheItemPool;
|
2016-06-30 23:30:37 +02:00
|
|
|
|
|
|
|
/**
|
2016-07-15 00:40:30 +02:00
|
|
|
* @param DescriberInterface[] $describers
|
2016-06-30 23:30:37 +02:00
|
|
|
*/
|
2016-11-30 16:52:13 +01:00
|
|
|
public function __construct(array $describers, CacheItemPoolInterface $cacheItemPool = null)
|
2016-06-30 23:30:37 +02:00
|
|
|
{
|
2016-07-15 00:40:30 +02:00
|
|
|
$this->describers = $describers;
|
2016-11-30 16:52:13 +01:00
|
|
|
$this->cacheItemPool = $cacheItemPool;
|
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) {
|
|
|
|
$item = $this->cacheItemPool->getItem('swagger_doc');
|
|
|
|
if ($item->isHit()) {
|
|
|
|
return $this->swagger = $item->get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:05:14 +02:00
|
|
|
$this->swagger = new Swagger();
|
2016-07-15 00:40:30 +02:00
|
|
|
foreach ($this->describers as $describer) {
|
|
|
|
$describer->describe($this->swagger);
|
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
|
|
|
}
|
|
|
|
}
|