mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
ab005c4129
* implement alternative naming system via configuration * use strict comparison * test di configs * rever * test naming aliases are applied * set "default" as default area * test names are passed to generators * cs formatting * added extra check for built-int types * cs * added documentation about alternative names * allow to create the same alias in two different areas * document and test better aliasing strategy * specify that the last matching rule is used * Make last matching rule wins * Fix documentation
81 lines
2.3 KiB
PHP
81 lines
2.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the NelmioApiDocBundle package.
|
|
*
|
|
* (c) Nelmio
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Nelmio\ApiDocBundle;
|
|
|
|
use EXSyst\Component\Swagger\Swagger;
|
|
use Nelmio\ApiDocBundle\Describer\DescriberInterface;
|
|
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
|
|
use Nelmio\ApiDocBundle\Model\ModelRegistry;
|
|
use Nelmio\ApiDocBundle\ModelDescriber\ModelDescriberInterface;
|
|
use Psr\Cache\CacheItemPoolInterface;
|
|
|
|
final class ApiDocGenerator
|
|
{
|
|
private $swagger;
|
|
|
|
private $describers;
|
|
|
|
private $modelDescribers;
|
|
|
|
private $cacheItemPool;
|
|
|
|
private $alternativeNames = [];
|
|
|
|
/**
|
|
* @param DescriberInterface[]|iterable $describers
|
|
* @param ModelDescriberInterface[]|iterable $modelDescribers
|
|
*/
|
|
public function __construct($describers, $modelDescribers, CacheItemPoolInterface $cacheItemPool = null, string $cacheItemId = null)
|
|
{
|
|
$this->describers = $describers;
|
|
$this->modelDescribers = $modelDescribers;
|
|
$this->cacheItemPool = $cacheItemPool;
|
|
$this->cacheItemId = $cacheItemId;
|
|
}
|
|
|
|
public function setAlternativeNames(array $alternativeNames)
|
|
{
|
|
$this->alternativeNames = $alternativeNames;
|
|
}
|
|
|
|
public function generate(): Swagger
|
|
{
|
|
if (null !== $this->swagger) {
|
|
return $this->swagger;
|
|
}
|
|
|
|
if ($this->cacheItemPool) {
|
|
$item = $this->cacheItemPool->getItem($this->cacheItemId ?? 'swagger_doc');
|
|
if ($item->isHit()) {
|
|
return $this->swagger = $item->get();
|
|
}
|
|
}
|
|
|
|
$this->swagger = new Swagger();
|
|
$modelRegistry = new ModelRegistry($this->modelDescribers, $this->swagger, $this->alternativeNames);
|
|
foreach ($this->describers as $describer) {
|
|
if ($describer instanceof ModelRegistryAwareInterface) {
|
|
$describer->setModelRegistry($modelRegistry);
|
|
}
|
|
|
|
$describer->describe($this->swagger);
|
|
}
|
|
$modelRegistry->registerDefinitions();
|
|
|
|
if (isset($item)) {
|
|
$this->cacheItemPool->save($item->set($this->swagger));
|
|
}
|
|
|
|
return $this->swagger;
|
|
}
|
|
}
|