|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Spatie\TypeScriptTransformer\Laravel; |
| 4 | + |
| 5 | +use Illuminate\Routing\Route; |
| 6 | +use Illuminate\Routing\Router; |
| 7 | +use Spatie\TypeScriptTransformer\DefaultTypeProviders\DefaultTypesProvider; |
| 8 | +use Spatie\TypeScriptTransformer\Laravel\Routes\InvokableRouteController; |
| 9 | +use Spatie\TypeScriptTransformer\Laravel\Routes\RouteController; |
| 10 | +use Spatie\TypeScriptTransformer\Laravel\Routes\RouteControllerAction; |
| 11 | +use Spatie\TypeScriptTransformer\Laravel\Routes\RouteControllerCollection; |
| 12 | +use Spatie\TypeScriptTransformer\Laravel\Routes\RouteParameter; |
| 13 | +use Spatie\TypeScriptTransformer\Laravel\Routes\RouteParameterCollection; |
| 14 | +use Spatie\TypeScriptTransformer\Transformed\Transformed; |
| 15 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptAlias; |
| 16 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptArray; |
| 17 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptConditional; |
| 18 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptFunctionDefinition; |
| 19 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptGeneric; |
| 20 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptGenericTypeVariable; |
| 21 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptIdentifier; |
| 22 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptIndexedAccess; |
| 23 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptInterface; |
| 24 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptObject; |
| 25 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptOperator; |
| 26 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptParameter; |
| 27 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptProperty; |
| 28 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptRaw; |
| 29 | +use Spatie\TypeScriptTransformer\TypeScript\TypeScriptString; |
| 30 | + |
| 31 | +// @todo implement the method, probably using a RawTypeScriptNode, creating individual notes for each JS construct is probably a bit far fetched |
| 32 | +// @todo make sure we support __invoke routes without action |
| 33 | +// @todo add support for nullable parameters, these should be inferred |
| 34 | +// @todo a syntax like route(['Controller', 'action'], {params}), route(['Controller', 'action'], param), route(InvokeableController) would be even cooler but maybe too complicated at the moment |
| 35 | + |
| 36 | +/** |
| 37 | + * function route< |
| 38 | + * TController extends keyof Routes, |
| 39 | + * TAction extends keyof Routes[TController], |
| 40 | + * TParams extends Routes[TController][TAction]["parameters"] |
| 41 | + * >(action: [TController, TAction] | TController, params?: TParams): string { |
| 42 | + * |
| 43 | + * } |
| 44 | + */ |
| 45 | +class RouterGenerator implements DefaultTypesProvider |
| 46 | +{ |
| 47 | + public function provide(): array |
| 48 | + { |
| 49 | + $controllers = $this->resolveRoutes(); |
| 50 | + |
| 51 | + $transformedRoutes = new Transformed( |
| 52 | + new TypeScriptAlias( |
| 53 | + new TypeScriptIdentifier('Routes'), |
| 54 | + $controllers->toTypeScriptNode(), |
| 55 | + ), |
| 56 | + null, |
| 57 | + 'Routes', |
| 58 | + true, |
| 59 | + [], |
| 60 | + ); |
| 61 | + |
| 62 | + $actionParam = new Transformed( |
| 63 | + new TypeScriptAlias( |
| 64 | + new TypeScriptGeneric( |
| 65 | + new TypeScriptIdentifier('ActionParam'), |
| 66 | + [ |
| 67 | + new TypeScriptGenericTypeVariable(new TypeScriptIdentifier('TController')), |
| 68 | + new TypeScriptGenericTypeVariable(new TypeScriptIdentifier('TAction')), |
| 69 | + ] |
| 70 | + ), |
| 71 | + new TypeScriptConditional( |
| 72 | + TypeScriptOperator::extends( |
| 73 | + new TypeScriptIndexedAccess( |
| 74 | + new TypeScriptIdentifier('Routes'), |
| 75 | + [new TypeScriptIdentifier('TController')], |
| 76 | + ), |
| 77 | + new TypeScriptObject([ |
| 78 | + new TypeScriptProperty('invokable', new TypeScriptRaw('true')), |
| 79 | + ]) |
| 80 | + ), |
| 81 | + new TypeScriptIdentifier('TController'), |
| 82 | + new TypeScriptArray([ |
| 83 | + new TypeScriptIdentifier('TController'), |
| 84 | + new TypeScriptIdentifier('TAction'), |
| 85 | + ]) |
| 86 | + ) |
| 87 | + ), |
| 88 | + null, |
| 89 | + 'ActionParam', |
| 90 | + true, |
| 91 | + [], |
| 92 | + ); |
| 93 | + |
| 94 | + $transformedAction = new Transformed( |
| 95 | + new TypeScriptFunctionDefinition( |
| 96 | + new TypeScriptGeneric( |
| 97 | + new TypeScriptIdentifier('route'), |
| 98 | + [ |
| 99 | + new TypeScriptGenericTypeVariable( |
| 100 | + new TypeScriptIdentifier('TController'), |
| 101 | + extends: new TypeScriptIdentifier('keyof Routes'), |
| 102 | + ), |
| 103 | + new TypeScriptGenericTypeVariable( |
| 104 | + new TypeScriptIdentifier('TAction'), |
| 105 | + extends: new TypeScriptIdentifier('keyof Routes[TController]["actions"]'), |
| 106 | + ), |
| 107 | + new TypeScriptGenericTypeVariable( |
| 108 | + new TypeScriptIdentifier('TParams'), |
| 109 | + extends: new TypeScriptIdentifier('Routes[TController]["actions"][TAction]["parameters"]'), |
| 110 | + ), |
| 111 | + ] |
| 112 | + ), |
| 113 | + [ |
| 114 | + new TypeScriptParameter('action', new TypeScriptGeneric( |
| 115 | + new TypeScriptIdentifier('ActionParam'), |
| 116 | + [ |
| 117 | + new TypeScriptGenericTypeVariable(new TypeScriptIdentifier('TController')), |
| 118 | + new TypeScriptGenericTypeVariable(new TypeScriptIdentifier('TAction')), |
| 119 | + ] |
| 120 | + )), |
| 121 | + new TypeScriptParameter('params', new TypeScriptIdentifier('TParams'), isOptional: true), |
| 122 | + ], |
| 123 | + new TypeScriptString(), |
| 124 | + new TypeScriptRaw("let routes = JSON.parse('".json_encode($controllers->toJsObject(), flags: JSON_UNESCAPED_SLASHES). "')") |
| 125 | + ), |
| 126 | + null, |
| 127 | + 'route', |
| 128 | + true, |
| 129 | + [], |
| 130 | + ); |
| 131 | + |
| 132 | + return [$transformedRoutes, $actionParam, $transformedAction]; |
| 133 | + } |
| 134 | + |
| 135 | + private function resolveRoutes(): RouteControllerCollection |
| 136 | + { |
| 137 | + /** @var array<string, RouteController> $controllers */ |
| 138 | + $controllers = []; |
| 139 | + |
| 140 | + foreach (app(Router::class)->getRoutes()->getRoutes() as $route) { |
| 141 | + $controllerClass = $route->getControllerClass(); |
| 142 | + |
| 143 | + if ($controllerClass === null) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + |
| 147 | + $controllerClass = str_replace('\\', '.', $controllerClass); |
| 148 | + |
| 149 | + if ($route->getActionMethod() === $route->getControllerClass()) { |
| 150 | + $controllers[$controllerClass] = new InvokableRouteController( |
| 151 | + $this->resolveRouteParameters($route), |
| 152 | + $route->methods, |
| 153 | + $this->resolveUrl($route), |
| 154 | + ); |
| 155 | + |
| 156 | + continue; |
| 157 | + } |
| 158 | + |
| 159 | + if (! array_key_exists($controllerClass, $controllers)) { |
| 160 | + $controllers[$controllerClass] = new RouteController([]); |
| 161 | + } |
| 162 | + |
| 163 | + $controllers[$controllerClass]->actions[$route->getActionMethod()] = new RouteControllerAction( |
| 164 | + $route->getActionMethod(), |
| 165 | + $this->resolveRouteParameters($route), |
| 166 | + $route->methods, |
| 167 | + $this->resolveUrl($route), |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + return new RouteControllerCollection($controllers); |
| 172 | + } |
| 173 | + |
| 174 | + protected function resolveRouteParameters( |
| 175 | + Route $route |
| 176 | + ): RouteParameterCollection { |
| 177 | + preg_match_all('/\{(.*?)\}/', $route->getDomain().$route->uri, $matches); |
| 178 | + |
| 179 | + $parameters = array_map(fn (string $match) => new RouteParameter( |
| 180 | + trim($match, '?'), |
| 181 | + str_ends_with($match, '?') |
| 182 | + ), $matches[1]); |
| 183 | + |
| 184 | + return new RouteParameterCollection($parameters); |
| 185 | + } |
| 186 | + |
| 187 | + protected function resolveUrl(Route $route): string |
| 188 | + { |
| 189 | + return str_replace('?}', '}', $route->getDomain().$route->uri); |
| 190 | + } |
| 191 | +} |
0 commit comments