|
13 | 13 | class OpenApiRouteLoader implements RouteLoaderInterface |
14 | 14 | { |
15 | 15 | /** |
16 | | - * @var string[] |
| 16 | + * @var Finder |
17 | 17 | */ |
18 | | - private $sourceDirectories; |
19 | | - |
20 | | - /** |
21 | | - * @var string |
22 | | - */ |
23 | | - private $sourcePattern; |
| 18 | + private $finder; |
24 | 19 |
|
25 | 20 | /** |
26 | 21 | * @var array<string, int> |
27 | 22 | */ |
28 | 23 | private $routeNames = []; |
29 | 24 |
|
30 | | - /** |
31 | | - * @param string[] $sourceDirectories |
32 | | - */ |
33 | 25 | public function __construct( |
34 | | - array $sourceDirectories, |
35 | | - string $sourcePattern = '/\.php/' |
| 26 | + ?Finder $finder = null |
36 | 27 | ) { |
37 | | - $this->sourceDirectories = $sourceDirectories; |
38 | | - $this->sourcePattern = $sourcePattern; |
| 28 | + if (null === $finder) { |
| 29 | + // try to use the symfony flex default src directory based on a composer install |
| 30 | + $srcDir = __DIR__.'/../../../../src'; |
| 31 | + $realPath = realpath($srcDir); |
| 32 | + if (!$realPath || !is_dir($realPath)) { |
| 33 | + throw new \LogicException(sprintf('The default directory to look for OpenAPI/Swagger annotations "%s" does not exist. Please configure the finder explicitly.')); |
| 34 | + } |
| 35 | + |
| 36 | + $finder = (new Finder())->in($realPath)->files()->name('*.php')->sortByName()->followLinks(); |
| 37 | + } |
| 38 | + |
| 39 | + $this->finder = $finder; |
39 | 40 | } |
40 | 41 |
|
41 | 42 | public function __invoke(): RouteCollection |
42 | 43 | { |
43 | | - $finder = new Finder(); |
44 | | - $finder->in($this->sourceDirectories)->path($this->sourcePattern); |
45 | | - |
46 | | - $fullSwagger = \Swagger\scan($finder); |
| 44 | + $fullSwagger = \Swagger\scan($this->finder); |
47 | 45 | $routeCollection = new RouteCollection(); |
48 | 46 |
|
| 47 | + $globalFormatSuffixConfig = FormatSuffixConfig::fromAnnotation($fullSwagger); |
| 48 | + |
49 | 49 | foreach ($fullSwagger->paths as $path) { |
50 | | - $this->addRouteFromSwaggerOperation($routeCollection, $path->get); |
51 | | - $this->addRouteFromSwaggerOperation($routeCollection, $path->put); |
52 | | - $this->addRouteFromSwaggerOperation($routeCollection, $path->post); |
53 | | - $this->addRouteFromSwaggerOperation($routeCollection, $path->delete); |
54 | | - $this->addRouteFromSwaggerOperation($routeCollection, $path->options); |
55 | | - $this->addRouteFromSwaggerOperation($routeCollection, $path->head); |
56 | | - $this->addRouteFromSwaggerOperation($routeCollection, $path->patch); |
| 50 | + $pathFormatSuffixConfig = FormatSuffixConfig::fromAnnotation($path, $globalFormatSuffixConfig); |
| 51 | + |
| 52 | + $this->addRouteFromSwaggerOperation($routeCollection, $path->get, $pathFormatSuffixConfig); |
| 53 | + $this->addRouteFromSwaggerOperation($routeCollection, $path->put, $pathFormatSuffixConfig); |
| 54 | + $this->addRouteFromSwaggerOperation($routeCollection, $path->post, $pathFormatSuffixConfig); |
| 55 | + $this->addRouteFromSwaggerOperation($routeCollection, $path->delete, $pathFormatSuffixConfig); |
| 56 | + $this->addRouteFromSwaggerOperation($routeCollection, $path->options, $pathFormatSuffixConfig); |
| 57 | + $this->addRouteFromSwaggerOperation($routeCollection, $path->head, $pathFormatSuffixConfig); |
| 58 | + $this->addRouteFromSwaggerOperation($routeCollection, $path->patch, $pathFormatSuffixConfig); |
57 | 59 | } |
58 | 60 |
|
59 | 61 | $this->routeNames = []; |
60 | 62 |
|
61 | 63 | return $routeCollection; |
62 | 64 | } |
63 | 65 |
|
64 | | - private function addRouteFromSwaggerOperation(RouteCollection $routeCollection, ?Operation $operation): void |
| 66 | + private function addRouteFromSwaggerOperation(RouteCollection $routeCollection, ?Operation $operation, FormatSuffixConfig $parentFormatSuffixConfig): void |
65 | 67 | { |
66 | 68 | if (null === $operation) { |
67 | 69 | return; |
68 | 70 | } |
69 | 71 |
|
70 | 72 | $controller = $this->getControllerFromSwaggerOperation($operation); |
71 | 73 | $name = $this->getRouteName($operation, $controller); |
72 | | - $route = $this->createRoute($operation, $controller); |
| 74 | + $route = $this->createRoute($operation, $controller, $parentFormatSuffixConfig); |
73 | 75 | $routeCollection->add($name, $route); |
74 | 76 | } |
75 | 77 |
|
76 | | - private function createRoute(Operation $operation, string $controller): Route |
| 78 | + private function createRoute(Operation $operation, string $controller, FormatSuffixConfig $parentFormatSuffixConfig): Route |
77 | 79 | { |
78 | | - $formatSuffix = $operation->x['format-suffix'] ?? true; |
79 | | - $path = $formatSuffix ? $operation->path.'.{_format}' : $operation->path; |
| 80 | + $formatSuffixConfig = FormatSuffixConfig::fromAnnotation($operation, $parentFormatSuffixConfig); |
| 81 | + |
| 82 | + $path = $formatSuffixConfig->enabled ? $operation->path.'.{_format}' : $operation->path; |
80 | 83 | $route = new Route($path); |
81 | 84 | $route->setMethods($operation->method); |
82 | 85 | $route->setDefault('_controller', $controller); |
83 | | - if ($formatSuffix) { |
84 | | - $formatPattern = $operation->x['format-pattern'] ?? 'json|xml'; |
| 86 | + |
| 87 | + if ($formatSuffixConfig->enabled) { |
85 | 88 | $route->setDefault('_format', null); |
86 | | - $route->setRequirement('_format', $formatPattern); |
| 89 | + |
| 90 | + if (null !== $formatSuffixConfig->pattern) { |
| 91 | + $route->setRequirement('_format', $formatSuffixConfig->pattern); |
| 92 | + } |
87 | 93 | } |
88 | 94 | if (null !== $operation->parameters) { |
89 | 95 | foreach ($operation->parameters as $parameter) { |
|
0 commit comments