NelmioApiDocBundle/Describer/SwaggerPhpDescriber.php

63 lines
1.7 KiB
PHP
Raw Normal View History

2016-07-28 10:20:59 +02:00
<?php
/*
2016-12-29 12:09:26 +01:00
* This file is part of the NelmioApiDocBundle package.
2016-07-28 10:20:59 +02:00
*
2016-12-29 12:09:26 +01:00
* (c) Nelmio
2016-07-28 10:20:59 +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\Describer;
2016-07-28 10:20:59 +02:00
2017-01-14 17:36:56 +01:00
use Nelmio\ApiDocBundle\SwaggerPhp\AddDefaults;
use Nelmio\ApiDocBundle\SwaggerPhp\ModelRegister;
use Nelmio\ApiDocBundle\SwaggerPhp\OperationResolver;
2017-01-14 17:36:56 +01:00
use Swagger\Analyser;
use Swagger\Analysis;
2017-01-14 17:36:56 +01:00
final class SwaggerPhpDescriber extends ExternalDocDescriber implements ModelRegistryAwareInterface
2016-07-28 10:20:59 +02:00
{
2017-01-14 17:36:56 +01:00
use ModelRegistryAwareTrait;
private $operationResolver;
2016-07-28 10:20:59 +02:00
public function __construct(string $projectPath, bool $overwrite = false)
{
2017-01-14 17:36:56 +01:00
$nelmioNamespace = 'Nelmio\\ApiDocBundle\\';
if (!in_array($nelmioNamespace, Analyser::$whitelist)) {
Analyser::$whitelist[] = $nelmioNamespace;
}
2016-07-29 10:22:40 +02:00
parent::__construct(function () use ($projectPath) {
2017-01-14 17:36:56 +01:00
$options = ['processors' => $this->getProcessors()];
$annotation = \Swagger\scan($projectPath, $options);
return json_decode(json_encode($annotation));
2016-07-29 10:22:40 +02:00
}, $overwrite);
2016-07-28 10:20:59 +02:00
}
/**
* If set, the describer will try to complete paths and create
* implicit operations.
*/
public function setOperationResolver(OperationResolver $operationResolver)
{
$this->operationResolver = $operationResolver;
}
2017-01-14 17:36:56 +01:00
private function getProcessors(): array
{
$processors = [
new AddDefaults(),
new ModelRegister($this->modelRegistry),
];
if (null !== $this->operationResolver) {
$processors[] = $this->operationResolver;
}
return array_merge($processors, Analysis::processors());
}
2016-07-28 10:20:59 +02:00
}