|
2 | 2 |
|
3 | 3 | namespace Contributte\Console\Extra\Command\Router; |
4 | 4 |
|
| 5 | +use Contributte\Console\Extra\Exception\LogicalException; |
5 | 6 | use Nette\Application\Routers\Route; |
6 | 7 | use Nette\Application\Routers\RouteList; |
7 | | -use Nette\Application\Routers\SimpleRouter; |
8 | 8 | use Nette\Routing\Router; |
| 9 | +use stdClass; |
9 | 10 | use Symfony\Component\Console\Attribute\AsCommand; |
10 | 11 | use Symfony\Component\Console\Command\Command; |
11 | 12 | use Symfony\Component\Console\Helper\Table; |
@@ -41,40 +42,66 @@ protected function execute(InputInterface $input, OutputInterface $output): int |
41 | 42 | } |
42 | 43 |
|
43 | 44 | /** |
44 | | - * @return mixed[] |
| 45 | + * @return array<mixed> |
45 | 46 | */ |
46 | 47 | protected function createRows(): array |
47 | 48 | { |
48 | 49 | return $this->analyse($this->router); |
49 | 50 | } |
50 | 51 |
|
51 | 52 | /** |
52 | | - * @return mixed[]|object |
| 53 | + * @return array<mixed> |
53 | 54 | */ |
54 | | - protected function analyse(Router $router, ?string $module = null): array|object |
| 55 | + protected function analyse(Router $router, ?string $module = null): array |
55 | 56 | { |
56 | 57 | if ($router instanceof RouteList) { |
57 | | - $routes = []; |
| 58 | + $routes = $this->analyseRouteList($router, $module); |
| 59 | + } elseif ($router instanceof Route) { |
| 60 | + $routes = [(array) $this->analyseRoute($router, $module)]; |
| 61 | + } else { |
| 62 | + throw new LogicalException(sprintf('Router "%s" is not supported', $router::class)); |
| 63 | + } |
58 | 64 |
|
59 | | - foreach ($router as $subRouter) { |
60 | | - $route = $this->analyse($subRouter, $module . $router->getModule()); |
| 65 | + return $routes; |
| 66 | + } |
61 | 67 |
|
62 | | - if (is_array($route)) { |
63 | | - $routes = array_merge($routes, $route); |
64 | | - } else { |
65 | | - $routes[] = (array) $route; |
66 | | - } |
| 68 | + /** |
| 69 | + * @return array<mixed> |
| 70 | + */ |
| 71 | + protected function analyseRouteList(RouteList $router, ?string $module = null): array |
| 72 | + { |
| 73 | + $routes = []; |
| 74 | + |
| 75 | + foreach ($router->getRouters() as $subRouter) { |
| 76 | + if ($subRouter instanceof RouteList) { |
| 77 | + $routes = array_merge( |
| 78 | + $routes, |
| 79 | + $this->analyseRouteList($subRouter, $module . $router->getModule()) |
| 80 | + ); |
| 81 | + } elseif ($subRouter instanceof Route) { |
| 82 | + $routes = array_merge( |
| 83 | + $routes, |
| 84 | + [(array) $this->analyseRoute($subRouter, $module . $router->getModule())] |
| 85 | + ); |
| 86 | + } else { |
| 87 | + throw new LogicalException(sprintf('Router "%s" is not supported', $router::class)); |
67 | 88 | } |
68 | | - |
69 | | - return $routes; |
70 | | - } else { |
71 | | - return (object) [ |
72 | | - 'mask' => $router instanceof Route ? $router->getMask() : null, |
73 | | - 'module' => rtrim((string) $module, ':'), |
74 | | - 'defaults' => $router instanceof Route || $router instanceof SimpleRouter ? $this->analyseDefaults($router->getDefaults()) : null, |
75 | | - 'class' => $router::class, |
76 | | - ]; |
77 | 89 | } |
| 90 | + |
| 91 | + return $routes; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @return stdClass |
| 96 | + */ |
| 97 | + protected function analyseRoute(Route $router, ?string $module = null): object |
| 98 | + { |
| 99 | + return (object) [ |
| 100 | + 'mask' => $router->getMask(), |
| 101 | + 'module' => rtrim((string) $module, ':'), |
| 102 | + 'defaults' => $this->analyseDefaults($router->getDefaults()), |
| 103 | + 'class' => $router::class, |
| 104 | + ]; |
78 | 105 | } |
79 | 106 |
|
80 | 107 | /** |
|
0 commit comments