Skip to content

Commit

Permalink
Fix #49: Add ability to configure OpenApi\Annotations\OpenAPI to `Y…
Browse files Browse the repository at this point in the history
…iisoft\Swagger\Service\SwaggerService`
  • Loading branch information
devanych committed Feb 9, 2022
1 parent 7731178 commit ee5c624
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
@@ -1,13 +1,13 @@
# Yii Swagger Change Log

## 1.1.1 under development
## 1.2.0 under development

- no changes in this release.
- Enh #49: Add ability to configure `OpenApi\Annotations\OpenAPI` to `Yiisoft\Swagger\Service\SwaggerService` (devanych)

## 1.1.0 January 14, 2022

- Enh #48: Add ability to configure `Yii\Swagger\Middleware\SwaggerJson` via config params (rustamwin)

## 1.0.0 December 15, 2021

- Initial release.
- Initial release.
1 change: 1 addition & 0 deletions config/params.php
Expand Up @@ -8,5 +8,6 @@
'persistAuthorization' => true,
],
'annotation-paths' => [],
'swagger-options' => [],
],
];
10 changes: 9 additions & 1 deletion config/web.php
Expand Up @@ -11,22 +11,30 @@
/** @var array $params */

return [
SwaggerService::class => SwaggerService::class,
SwaggerService::class => [
'withOptions()' => [
$params['yiisoft/yii-swagger']['swagger-options'],
],
],

SwaggerUi::class => [
'__construct()' => [
'params' => $params['yiisoft/yii-swagger']['ui-params'],
],
],

SwaggerJson::class => static function (
CacheInterface $cache,
DataResponseFactoryInterface $responseFactory,
SwaggerService $swaggerService
) use ($params) {
$params = $params['yiisoft/yii-swagger'];
$swaggerJson = new SwaggerJson($cache, $responseFactory, $swaggerService);

if (array_key_exists('cacheTTL', $params)) {
$swaggerJson = $swaggerJson->withCache($params['cacheTTL']);
}

return $swaggerJson->withAnnotationPaths(...$params['annotation-paths']);
},
];
22 changes: 16 additions & 6 deletions src/Service/SwaggerService.php
Expand Up @@ -22,15 +22,11 @@ final class SwaggerService

private string $viewPath;
private string $viewName;
private array $options = [];

public function __construct(Aliases $aliases)
{
$this->aliases = $aliases;
$this->setupDefaults();
}

private function setupDefaults(): void
{
$this->viewPath = dirname(__DIR__, 2) . '/views';
$this->viewName = 'swagger-ui';
}
Expand All @@ -45,14 +41,28 @@ public function getViewName(): string
return $this->viewName;
}

/**
* Returns a new instance with the specified swagger options.
*
* @param array $options For {@see Generator::scan()}.
*
* @return self
*/
public function withOptions(array $options): self
{
$new = clone $this;
$new->options = $options;
return $new;
}

public function fetch(array $annotationPaths): OpenApi
{
if ($annotationPaths === []) {
throw new InvalidArgumentException('Annotation paths cannot be empty array.');
}

$directories = array_map(fn (string $path): string => $this->aliases->get($path), $annotationPaths);
$openApi = Generator::scan(Util::finder($directories));
$openApi = Generator::scan(Util::finder($directories), $this->options);

if ($openApi === null) {
throw new RuntimeException(sprintf(
Expand Down
7 changes: 7 additions & 0 deletions tests/SwaggerServiceTest.php
Expand Up @@ -29,6 +29,13 @@ public function testSwaggerServiceEmptyArrayFetch(): void
$this->createService()->fetch([]);
}

public function testImmutability(): void
{
$service = $this->createService();

$this->assertNotSame($service, $service->withOptions([]));
}

private function createService(): SwaggerService
{
return new SwaggerService(new Aliases());
Expand Down

0 comments on commit ee5c624

Please sign in to comment.