@@ -14,8 +14,13 @@ class ControllersGenerator extends BaseGenerator implements GeneratorInterface
1414
1515 private array $ methodsWithRequests = ['PATCH ' , 'POST ' , 'PUT ' , 'DELETE ' ];
1616
17+ private string $ serversUrl ;
18+
1719 public function generate (SpecObjectInterface $ specObject ): void
1820 {
21+ $ openApiData = $ specObject ->getSerializableData ();
22+ $ this ->serversUrl = $ openApiData ?->servers[0 ]?->url ?? '' ;
23+
1924 $ controllers = $ this ->extractControllers ($ specObject );
2025 $ this ->createControllersFiles ($ controllers );
2126 }
@@ -26,7 +31,7 @@ private function extractControllers(SpecObjectInterface $specObject): array
2631
2732 $ controllers = [];
2833 $ paths = $ openApiData ->paths ?: [];
29- foreach ($ paths as $ routes ) {
34+ foreach ($ paths as $ path => $ routes ) {
3035 foreach ($ routes as $ method => $ route ) {
3136 $ requestClassName = null ;
3237 $ methodWithRequest = in_array (strtoupper ($ method ), $ this ->methodsWithRequests );
@@ -60,10 +65,17 @@ private function extractControllers(SpecObjectInterface $specObject): array
6065 $ controllers [$ fqcn ]['requestsNamespaces ' ][$ namespace ] = $ namespace ;
6166 }
6267
68+ $ responses = $ route ->responses ?? null ;
6369 $ controllers [$ fqcn ]['actions ' ][] = [
6470 'name ' => $ handler ->method ?: '__invoke ' ,
6571 'with_request_namespace ' => $ methodWithRequest && !empty ($ route ->{'x-lg-skip-request-generation ' }),
6672 'parameters ' => array_merge ($ this ->extractPathParameters ($ route ), $ this ->getActionExtraParameters ($ methodWithRequest , $ requestClassName )),
73+
74+ 'route ' => [
75+ 'method ' => $ method ,
76+ 'path ' => $ path ,
77+ 'responseCodes ' => $ responses ? array_keys (get_object_vars ($ responses )) : [],
78+ ],
6779 ];
6880 }
6981 }
@@ -73,7 +85,7 @@ private function extractControllers(SpecObjectInterface $specObject): array
7385
7486 private function extractPathParameters (stdClass $ route ): array
7587 {
76- $ oasRoutePath = array_filter ($ route ->parameters ?? [], fn (stdClass $ param ) => $ param ?->in === "path " );
88+ $ oasRoutePath = array_filter ($ route ->parameters ?? [], fn (stdClass $ param ) => $ param ?->in === "path " );
7789
7890 return array_map (fn (stdClass $ param ) => [
7991 'name ' => $ param ->name ,
@@ -100,7 +112,8 @@ private function createControllersFiles(array $controllers): void
100112 $ className = $ controller ['className ' ];
101113
102114 $ filePath = $ this ->getNamespacedFilePath ($ className , $ namespace );
103- if (!$ this ->filesystem ->exists ($ filePath )) {
115+ $ controllerExists = $ this ->filesystem ->exists ($ filePath );
116+ if (!$ controllerExists ) {
104117 $ this ->createEmptyControllerFile ($ filePath , $ controller );
105118 }
106119
@@ -109,6 +122,8 @@ private function createControllersFiles(array $controllers): void
109122 $ newMethods = $ this ->convertMethodsToString ($ class , $ controller ['actions ' ], $ controller ['requestsNamespaces ' ]);
110123 if (!empty ($ newMethods )) {
111124 $ controller ['requestsNamespaces ' ][static ::RESPONSABLE_NAMESPACE ] = static ::RESPONSABLE_NAMESPACE ;
125+ } elseif ($ controllerExists ) {
126+ continue ;
112127 }
113128
114129 $ content = $ class ->getContentWithAdditionalMethods ($ newMethods , $ controller ['requestsNamespaces ' ]);
@@ -172,6 +187,13 @@ private function convertMethodsToString(ClassParser $class, array $methods, arra
172187 '{{ params }} ' => $ this ->formatActionParamsAsString ($ method ['parameters ' ]),
173188 ]
174189 );
190+
191+ static ::markNewControllerMethod (
192+ serversUrl: $ this ->serversUrl ,
193+ path: $ method ['route ' ]['path ' ],
194+ method: $ method ['route ' ]['method ' ],
195+ responseCodes: $ method ['route ' ]['responseCodes ' ],
196+ );
175197 }
176198
177199 $ prefix = !empty ($ methodsStrings ) ? static ::DELIMITER : '' ;
0 commit comments