|
13 | 13 | class OpenApiRouteLoader implements RouteLoaderInterface |
14 | 14 | { |
15 | 15 | /** |
16 | | - * @var string[] |
| 16 | + * @var Finder |
17 | 17 | */ |
18 | | - private $sourceDirectories; |
| 18 | + private $finder; |
19 | 19 |
|
20 | 20 | /** |
21 | | - * @var string |
| 21 | + * @var bool |
22 | 22 | */ |
23 | | - private $sourcePattern; |
| 23 | + private $addFormatSuffix; |
24 | 24 |
|
25 | 25 | /** |
26 | | - * @var array<string, int> |
| 26 | + * @var string|null |
27 | 27 | */ |
28 | | - private $routeNames = []; |
| 28 | + private $defaultFormatPattern; |
29 | 29 |
|
30 | 30 | /** |
31 | | - * @param string[] $sourceDirectories |
| 31 | + * @var array<string, int> |
32 | 32 | */ |
| 33 | + private $routeNames = []; |
| 34 | + |
33 | 35 | public function __construct( |
34 | | - array $sourceDirectories, |
35 | | - string $sourcePattern = '/\.php/' |
| 36 | + ?Finder $finder = null, |
| 37 | + bool $addFormatSuffix = false, |
| 38 | + ?string $defaultFormatPattern = null |
36 | 39 | ) { |
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; |
39 | 54 | } |
40 | 55 |
|
41 | 56 | public function __invoke(): RouteCollection |
42 | 57 | { |
43 | | - $finder = new Finder(); |
44 | | - $finder->in($this->sourceDirectories)->path($this->sourcePattern); |
45 | | - |
46 | | - $fullSwagger = \Swagger\scan($finder); |
| 58 | + $fullSwagger = \Swagger\scan($this->finder); |
47 | 59 | $routeCollection = new RouteCollection(); |
48 | 60 |
|
49 | 61 | foreach ($fullSwagger->paths as $path) { |
@@ -75,13 +87,13 @@ private function addRouteFromSwaggerOperation(RouteCollection $routeCollection, |
75 | 87 |
|
76 | 88 | private function createRoute(Operation $operation, string $controller): Route |
77 | 89 | { |
78 | | - $formatSuffix = $operation->x['format-suffix'] ?? true; |
| 90 | + $formatSuffix = $operation->x['format-suffix'] ?? $this->addFormatSuffix; |
79 | 91 | $path = $formatSuffix ? $operation->path.'.{_format}' : $operation->path; |
80 | 92 | $route = new Route($path); |
81 | 93 | $route->setMethods($operation->method); |
82 | 94 | $route->setDefault('_controller', $controller); |
83 | 95 | if ($formatSuffix) { |
84 | | - $formatPattern = $operation->x['format-pattern'] ?? 'json|xml'; |
| 96 | + $formatPattern = $operation->x['format-pattern'] ?? $this->defaultFormatPattern; |
85 | 97 | $route->setDefault('_format', null); |
86 | 98 | $route->setRequirement('_format', $formatPattern); |
87 | 99 | } |
|
0 commit comments