Skip to content

Commit 52e59ac

Browse files
authored
Merge pull request #9 from ensi-platform/task-81772
#81772
2 parents 462d141 + d209f5d commit 52e59ac

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/Generators/RoutesGenerator.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function generate(SpecObjectInterface $specObject): void
1919

2020
$routesStrings = '';
2121

22+
$controllerNamespaces = [];
2223
$paths = $openApiData->paths ?: [];
2324
foreach ($paths as $path => $routes) {
2425
foreach ($routes as $method => $route) {
@@ -27,7 +28,11 @@ public function generate(SpecObjectInterface $specObject): void
2728
$routeMiddleware = $route->{'x-lg-middleware'} ?? null;
2829
$routeWithoutMiddleware = $route->{'x-lg-without-middleware'} ?? null;
2930
if ($handler) {
30-
$routesStrings .= "Route::{$method}('{$this->trimPath($path)}', {$this->formatHandler($handler)})";
31+
[$handler, $controllerNamespace] = $this->getControllerInfoFromHandler($handler);
32+
33+
$controllerNamespaces[] = $controllerNamespace;
34+
35+
$routesStrings .= "Route::{$method}('{$this->trimPath($path)}', {$handler})";
3136
$routesStrings .= $routeName ? "->name('{$routeName}')": "";
3237
$routesStrings .= $routeMiddleware ? "->middleware({$this->formatMiddleware($routeMiddleware)})": "";
3338
$routesStrings .= $routeWithoutMiddleware ? "->withoutMiddleware({$this->formatMiddleware($routeWithoutMiddleware)})": "";
@@ -36,6 +41,8 @@ public function generate(SpecObjectInterface $specObject): void
3641
}
3742
}
3843

44+
$controllerNamespacesStrings = $this->formatControllerNamespaces($controllerNamespaces);
45+
3946
$routesPath = $this->getNamespacedFilePath("routes", $namespace);
4047
if ($this->filesystem->exists($routesPath)) {
4148
$this->filesystem->delete($routesPath);
@@ -44,17 +51,20 @@ public function generate(SpecObjectInterface $specObject): void
4451
$template = $this->templatesManager->getTemplate('routes.template');
4552
$this->filesystem->put(
4653
$routesPath,
47-
$this->replacePlaceholders($template, ['{{ routes }}' => $routesStrings])
54+
$this->replacePlaceholders($template, [
55+
'{{ controller_namespaces }}' => $controllerNamespacesStrings,
56+
'{{ routes }}' => $routesStrings,
57+
])
4858
);
4959
}
5060

51-
private function formatHandler(string $handler): string
61+
private function getControllerInfoFromHandler(string $handler): array
5262
{
5363
$parsedRouteHandler = $this->routeHandlerParser->parse($handler);
54-
$class = '\\' . $parsedRouteHandler->fqcn . '::class';
64+
$class = $parsedRouteHandler->class . '::class';
5565
$method = $parsedRouteHandler->method;
5666

57-
return $method ? "[$class, '$method']" : "$class";
67+
return [$method ? "[$class, '$method']" : "$class", "{$parsedRouteHandler->namespace}\\{$parsedRouteHandler->class}"];
5868
}
5969

6070
private function formatMiddleware(string $middleware): string
@@ -67,4 +77,12 @@ private function formatMiddleware(string $middleware): string
6777

6878
return '[' . implode(', ', $parts) . ']';
6979
}
80+
81+
private function formatControllerNamespaces(array $controllerNamespaces): string
82+
{
83+
$controllerNamespaces = array_unique($controllerNamespaces);
84+
sort($controllerNamespaces);
85+
86+
return implode("\n", array_map(fn (string $controllerNamespace) => "use {$controllerNamespace};", $controllerNamespaces));
87+
}
7088
}

templates/routes.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Do NOT edit it manually. Run `php artisan openapi:generate-server`.
66
*/
77

8+
{{ controller_namespaces }}
89
use Illuminate\Support\Facades\Route;
910

1011
{{ routes }}

0 commit comments

Comments
 (0)