@@ -23,7 +23,6 @@ private function extractControllers(SpecObjectInterface $specObject): array
2323 $ paths = $ openApiData ->paths ?: [];
2424 foreach ($ paths as $ routes ) {
2525 foreach ($ routes as $ method => $ route ) {
26- $ requestName = null ;
2726 $ requestClassName = null ;
2827 $ methodWithRequest = in_array (strtoupper ($ method ), $ this ->methodsWithRequests );
2928
@@ -52,15 +51,14 @@ private function extractControllers(SpecObjectInterface $specObject): array
5251
5352 if ($ methodWithRequest && empty ($ route ->{'x-lg-skip-request-generation ' })) {
5453 $ requestClassName = $ route ->{'x-lg-request-class-name ' } ?? ucfirst ($ route ->operationId ) . 'Request ' ;
55- $ requestName = lcfirst ($ requestClassName );
5654 $ controllers [$ fqcn ]['requestsNamespaces ' ][] = $ this ->getReplacedNamespace ($ handler ->namespace , 'Controllers ' , 'Requests ' ) . '\\' . ucfirst ($ requestClassName );
5755 } elseif ($ methodWithRequest ) {
5856 $ controllers [$ fqcn ]['requestsNamespaces ' ][] = 'Illuminate\Http\Request ' ;
5957 }
6058
6159 $ controllers [$ fqcn ]['actions ' ][] = [
6260 'name ' => $ handler ->method ?: '__invoke ' ,
63- 'parameters ' => array_merge ($ this ->extractPathParameters ($ route ), $ this ->getActionExtraParameters ($ methodWithRequest , $ requestName , $ requestClassName )),
61+ 'parameters ' => array_merge ($ this ->extractPathParameters ($ route ), $ this ->getActionExtraParameters ($ methodWithRequest , $ requestClassName )),
6462 ];
6563 }
6664 }
@@ -78,11 +76,11 @@ private function extractPathParameters(stdClass $route): array
7876 ], $ oasRoutePath );
7977 }
8078
81- private function getActionExtraParameters (bool $ methodWithRequest , $ requestName = null , $ requestClassName = null ): array
79+ private function getActionExtraParameters (bool $ methodWithRequest , $ requestClassName = null ): array
8280 {
8381 if ($ methodWithRequest ) {
8482 return [[
85- 'name ' => $ requestName ?? 'request ' ,
83+ 'name ' => 'request ' ,
8684 'type ' => $ requestClassName ?? 'Request ' ,
8785 ]];
8886 }
@@ -101,28 +99,13 @@ private function createControllersFiles(array $controllers, string $template): v
10199 continue ;
102100 }
103101
104- $ methodsString = '' ;
105- foreach ($ controller ['actions ' ] as $ action ) {
106- $ methodName = $ action ['name ' ];
107- $ paramsString = $ this ->formatActionParamsAsString ($ action ['parameters ' ]);
108- $ methodsString .= <<<METHOD
109-
110- public function {$ methodName }( {$ paramsString })
111- {
112- //
113- }
114-
115- METHOD ;
116- }
117- $ methodsString = trim ($ methodsString , "\n" );
118-
119102 $ this ->putWithDirectoryCheck (
120103 $ filePath ,
121104 $ this ->replacePlaceholders ($ template , [
122105 '{{ namespace }} ' => $ namespace ,
123106 '{{ className }} ' => $ className ,
124- '{{ methods }} ' => $ methodsString ,
125107 '{{ requestsNamespaces }} ' => $ this ->formatRequestNamespaces ($ controller ['requestsNamespaces ' ]),
108+ '{{ methods }} ' => $ this ->convertMethodsToString ($ controller ['actions ' ]),
126109 ])
127110 );
128111 }
@@ -133,6 +116,23 @@ private function formatActionParamsAsString(array $params): string
133116 return implode (', ' , array_map (fn (array $ param ) => $ param ['type ' ] . " $ " . $ param ['name ' ], $ params ));
134117 }
135118
119+ private function convertMethodsToString (array $ methods ): string
120+ {
121+ $ methodsStrings = [];
122+
123+ foreach ($ methods as $ method ) {
124+ $ methodsStrings [] = $ this ->replacePlaceholders (
125+ $ this ->templatesManager ->getTemplate ('ControllerMethod.template ' ),
126+ [
127+ '{{ method }} ' => $ method ['name ' ],
128+ '{{ params }} ' => $ this ->formatActionParamsAsString ($ method ['parameters ' ]),
129+ ]
130+ );
131+ }
132+
133+ return implode ("\n\n " , $ methodsStrings );
134+ }
135+
136136 protected function formatRequestNamespaces (array $ namespaces ): string
137137 {
138138 $ namespaces = array_unique ($ namespaces );
0 commit comments