Skip to content

Commit 7c0dab2

Browse files
committed
make format-suffix default behavior configurable in constructor
1 parent 13fc83d commit 7c0dab2

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

src/OpenApiRouteLoader.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,49 @@
1313
class OpenApiRouteLoader implements RouteLoaderInterface
1414
{
1515
/**
16-
* @var string[]
16+
* @var Finder
1717
*/
18-
private $sourceDirectories;
18+
private $finder;
1919

2020
/**
21-
* @var string
21+
* @var bool
2222
*/
23-
private $sourcePattern;
23+
private $addFormatSuffix;
2424

2525
/**
26-
* @var array<string, int>
26+
* @var string|null
2727
*/
28-
private $routeNames = [];
28+
private $defaultFormatPattern;
2929

3030
/**
31-
* @param string[] $sourceDirectories
31+
* @var array<string, int>
3232
*/
33+
private $routeNames = [];
34+
3335
public function __construct(
34-
array $sourceDirectories,
35-
string $sourcePattern = '/\.php/'
36+
?Finder $finder = null,
37+
bool $addFormatSuffix = false,
38+
?string $defaultFormatPattern = null
3639
) {
37-
$this->sourceDirectories = $sourceDirectories;
38-
$this->sourcePattern = $sourcePattern;
40+
if (null === $finder) {
41+
// try to use the symfony flex default src directory based on a composer install
42+
$srcDir = __DIR__.'/../../../../src';
43+
$realPath = realpath($srcDir);
44+
if (!$realPath || !is_dir($realPath)) {
45+
throw new \LogicException(sprintf('The default directory to look for OpenAPI/Swagger annotations "%s" does not exist. Please configure the finder explicitly.'));
46+
}
47+
48+
$finder = (new Finder())->in($realPath)->files()->name('*.php')->sortByName()->followLinks();
49+
}
50+
51+
$this->finder = $finder;
52+
$this->addFormatSuffix = $addFormatSuffix;
53+
$this->defaultFormatPattern = $defaultFormatPattern;
3954
}
4055

4156
public function __invoke(): RouteCollection
4257
{
43-
$finder = new Finder();
44-
$finder->in($this->sourceDirectories)->path($this->sourcePattern);
45-
46-
$fullSwagger = \Swagger\scan($finder);
58+
$fullSwagger = \Swagger\scan($this->finder);
4759
$routeCollection = new RouteCollection();
4860

4961
foreach ($fullSwagger->paths as $path) {
@@ -75,13 +87,13 @@ private function addRouteFromSwaggerOperation(RouteCollection $routeCollection,
7587

7688
private function createRoute(Operation $operation, string $controller): Route
7789
{
78-
$formatSuffix = $operation->x['format-suffix'] ?? true;
90+
$formatSuffix = $operation->x['format-suffix'] ?? $this->addFormatSuffix;
7991
$path = $formatSuffix ? $operation->path.'.{_format}' : $operation->path;
8092
$route = new Route($path);
8193
$route->setMethods($operation->method);
8294
$route->setDefault('_controller', $controller);
8395
if ($formatSuffix) {
84-
$formatPattern = $operation->x['format-pattern'] ?? 'json|xml';
96+
$formatPattern = $operation->x['format-pattern'] ?? $this->defaultFormatPattern;
8597
$route->setDefault('_format', null);
8698
$route->setRequirement('_format', $formatPattern);
8799
}

0 commit comments

Comments
 (0)