@@ -17,25 +17,13 @@ class OpenApiRouteLoader implements RouteLoaderInterface
1717 */
1818 private $ finder ;
1919
20- /**
21- * @var bool
22- */
23- private $ addFormatSuffix ;
24-
25- /**
26- * @var string|null
27- */
28- private $ defaultFormatPattern ;
29-
3020 /**
3121 * @var array<string, int>
3222 */
3323 private $ routeNames = [];
3424
3525 public function __construct (
36- ?Finder $ finder = null ,
37- bool $ addFormatSuffix = false ,
38- ?string $ defaultFormatPattern = null
26+ ?Finder $ finder = null
3927 ) {
4028 if (null === $ finder ) {
4129 // try to use the symfony flex default src directory based on a composer install
@@ -49,55 +37,58 @@ public function __construct(
4937 }
5038
5139 $ this ->finder = $ finder ;
52- $ this ->addFormatSuffix = $ addFormatSuffix ;
53- $ this ->defaultFormatPattern = $ defaultFormatPattern ;
5440 }
5541
5642 public function __invoke (): RouteCollection
5743 {
5844 $ fullSwagger = \Swagger \scan ($ this ->finder );
5945 $ routeCollection = new RouteCollection ();
6046
47+ $ globalFormatSuffixConfig = FormatSuffixConfig::fromAnnotation ($ fullSwagger );
48+
6149 foreach ($ fullSwagger ->paths as $ path ) {
62- $ this ->addRouteFromSwaggerOperation ($ routeCollection , $ path ->get );
63- $ this ->addRouteFromSwaggerOperation ($ routeCollection , $ path ->put );
64- $ this ->addRouteFromSwaggerOperation ($ routeCollection , $ path ->post );
65- $ this ->addRouteFromSwaggerOperation ($ routeCollection , $ path ->delete );
66- $ this ->addRouteFromSwaggerOperation ($ routeCollection , $ path ->options );
67- $ this ->addRouteFromSwaggerOperation ($ routeCollection , $ path ->head );
68- $ 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 );
6959 }
7060
7161 $ this ->routeNames = [];
7262
7363 return $ routeCollection ;
7464 }
7565
76- private function addRouteFromSwaggerOperation (RouteCollection $ routeCollection , ?Operation $ operation ): void
66+ private function addRouteFromSwaggerOperation (RouteCollection $ routeCollection , ?Operation $ operation, FormatSuffixConfig $ parentFormatSuffixConfig ): void
7767 {
7868 if (null === $ operation ) {
7969 return ;
8070 }
8171
8272 $ controller = $ this ->getControllerFromSwaggerOperation ($ operation );
8373 $ name = $ this ->getRouteName ($ operation , $ controller );
84- $ route = $ this ->createRoute ($ operation , $ controller );
74+ $ route = $ this ->createRoute ($ operation , $ controller, $ parentFormatSuffixConfig );
8575 $ routeCollection ->add ($ name , $ route );
8676 }
8777
88- private function createRoute (Operation $ operation , string $ controller ): Route
78+ private function createRoute (Operation $ operation , string $ controller, FormatSuffixConfig $ parentFormatSuffixConfig ): Route
8979 {
90- $ formatSuffix = $ operation ->x ['format-suffix ' ] ?? $ this ->addFormatSuffix ;
91- $ path = $ formatSuffix ? $ operation ->path .'.{_format} ' : $ operation ->path ;
80+ $ formatSuffixConfig = FormatSuffixConfig::fromAnnotation ($ operation , $ parentFormatSuffixConfig );
81+
82+ $ path = $ formatSuffixConfig ->enabled ? $ operation ->path .'.{_format} ' : $ operation ->path ;
9283 $ route = new Route ($ path );
9384 $ route ->setMethods ($ operation ->method );
9485 $ route ->setDefault ('_controller ' , $ controller );
95- if ( $ formatSuffix ) {
96- $ formatPattern = $ operation -> x [ ' format-pattern ' ] ?? $ this -> defaultFormatPattern ;
86+
87+ if ( $ formatSuffixConfig -> enabled ) {
9788 $ route ->setDefault ('_format ' , null );
9889
99- if (null !== $ formatPattern ) {
100- $ route ->setRequirement ('_format ' , $ formatPattern );
90+ if (null !== $ formatSuffixConfig -> pattern ) {
91+ $ route ->setRequirement ('_format ' , $ formatSuffixConfig -> pattern );
10192 }
10293 }
10394 if (null !== $ operation ->parameters ) {
0 commit comments