9
9
use Illuminate \Routing \UrlGenerator ;
10
10
use Illuminate \Support \Collection ;
11
11
use Illuminate \Support \Str ;
12
- use Illuminate \Support \Stringable ;
13
12
use Illuminate \View \Compilers \BladeCompiler ;
14
13
use Illuminate \View \Factory ;
15
14
use ReflectionProperty ;
@@ -43,7 +42,7 @@ public function __construct(
43
42
44
43
public function handle ()
45
44
{
46
- $ this ->view ->addNamespace ('wayfinder ' , __DIR__ . '/../resources ' );
45
+ $ this ->view ->addNamespace ('wayfinder ' , __DIR__ . '/../resources ' );
47
46
$ this ->view ->addExtension ('blade.ts ' , 'blade ' );
48
47
$ this ->bladeCompiler ->directive ('trimDeadspace ' , function () {
49
48
return '<?php ob_start(); ?> ' ;
@@ -64,43 +63,43 @@ public function handle()
64
63
$ this ->urlDefaults [$ middleware ] ??= $ this ->getDefaultsForMiddleware ($ middleware );
65
64
66
65
return $ this ->urlDefaults [$ middleware ];
67
- })->flatMap (fn ($ r ) => $ r );
66
+ })->flatMap (fn ($ r ) => $ r );
68
67
69
68
return new Route ($ route , $ defaults , $ this ->forcedScheme , $ this ->forcedRoot );
70
69
});
71
70
72
71
$ this ->files ->deleteDirectory ($ this ->base ());
73
72
74
73
if (! $ this ->option ('skip-actions ' )) {
75
- $ controllers = $ routes ->filter (fn (Route $ route ) => $ route ->hasController ())->groupBy (fn (Route $ route ) => $ route ->dotNamespace ());
74
+ $ controllers = $ routes ->filter (fn (Route $ route ) => $ route ->hasController ())->groupBy (fn (Route $ route ) => $ route ->dotNamespace ());
76
75
77
76
$ controllers ->undot ()->each ($ this ->writeBarrelFiles (...));
78
77
$ controllers ->each ($ this ->writeControllerFile (...));
79
78
80
79
$ this ->writeContent ();
81
80
82
- info ('[Wayfinder] Generated actions in ' . $ this ->base ());
81
+ info ('[Wayfinder] Generated actions in ' . $ this ->base ());
83
82
}
84
83
85
84
$ this ->pathDirectory = 'routes ' ;
86
85
87
86
$ this ->files ->deleteDirectory ($ this ->base ());
88
87
89
88
if (! $ this ->option ('skip-routes ' )) {
90
- $ named = $ routes ->filter (fn (Route $ route ) => $ route ->name () && ! Str::endsWith ($ route ->name (), '. ' ))->groupBy (fn (Route $ route ) => Str::beforeLast ($ route ->name (), '. ' ));
89
+ $ named = $ routes ->filter (fn (Route $ route ) => $ route ->name () && ! Str::endsWith ($ route ->name (), '. ' ))->groupBy (fn (Route $ route ) => Str::beforeLast ($ route ->name (), '. ' ));
91
90
92
91
$ named ->undot ()->each ($ this ->writeBarrelFiles (...));
93
92
$ named ->each ($ this ->writeNamedFile (...));
94
93
95
94
$ this ->writeContent ();
96
95
97
- info ('[Wayfinder] Generated routes in ' . $ this ->base ());
96
+ info ('[Wayfinder] Generated routes in ' . $ this ->base ());
98
97
}
99
98
100
99
$ this ->pathDirectory = 'wayfinder ' ;
101
100
102
101
$ this ->files ->ensureDirectoryExists ($ this ->base ());
103
- $ this ->files ->copy (__DIR__ . '/../resources/js/wayfinder.ts ' , join_paths ($ this ->base (), 'index.ts ' ));
102
+ $ this ->files ->copy (__DIR__ . '/../resources/js/wayfinder.ts ' , join_paths ($ this ->base (), 'index.ts ' ));
104
103
}
105
104
106
105
private function appendContent ($ path , $ content ): void
@@ -123,28 +122,28 @@ private function writeContent(): void
123
122
124
123
private function writeControllerFile (Collection $ routes , string $ namespace ): void
125
124
{
126
- $ path = join_paths ($ this ->base (), ...explode ('. ' , $ namespace )) . '.ts ' ;
125
+ $ path = join_paths ($ this ->base (), ...explode ('. ' , $ namespace )). '.ts ' ;
127
126
128
127
$ this ->appendCommonImports ($ routes , $ path , $ namespace );
129
128
130
- $ routes ->groupBy (fn (Route $ route ) => $ route ->method ())->each (function ($ methodRoutes ) use ($ path ) {
129
+ $ routes ->groupBy (fn (Route $ route ) => $ route ->method ())->each (function ($ methodRoutes ) use ($ path ) {
131
130
if ($ methodRoutes ->count () === 1 ) {
132
131
return $ this ->writeControllerMethodExport ($ methodRoutes ->first (), $ path );
133
132
}
134
133
135
134
return $ this ->writeMultiRouteControllerMethodExport ($ methodRoutes , $ path );
136
135
});
137
136
138
- [$ invokable , $ methods ] = $ routes ->partition (fn (Route $ route ) => $ route ->hasInvokableController ());
137
+ [$ invokable , $ methods ] = $ routes ->partition (fn (Route $ route ) => $ route ->hasInvokableController ());
139
138
140
139
$ defaultExport = $ invokable ->isNotEmpty () ? $ invokable ->first ()->jsMethod () : last (explode ('. ' , $ namespace ));
141
140
142
141
if ($ invokable ->isEmpty ()) {
143
142
$ methodProps = "const {$ defaultExport } = { " ;
144
- $ methodProps .= $ methods ->map (fn (Route $ route ) => $ route ->jsMethod ())->unique ()->implode (', ' );
143
+ $ methodProps .= $ methods ->map (fn (Route $ route ) => $ route ->jsMethod ())->unique ()->implode (', ' );
145
144
$ methodProps .= ' } ' ;
146
145
} else {
147
- $ methodProps = $ methods ->map (fn (Route $ route ) => $ defaultExport . '. ' . $ route ->jsMethod () . ' = ' . $ route ->jsMethod ())->unique ()->implode (PHP_EOL );
146
+ $ methodProps = $ methods ->map (fn (Route $ route ) => $ defaultExport. '. ' . $ route ->jsMethod (). ' = ' . $ route ->jsMethod ())->unique ()->implode (PHP_EOL );
148
147
}
149
148
150
149
$ this ->appendContent ($ path , <<<JAVASCRIPT
@@ -164,8 +163,8 @@ private function writeMultiRouteControllerMethodExport(Collection $routes, strin
164
163
'controller ' => $ routes ->first ()->controller (),
165
164
'isInvokable ' => $ routes ->first ()->hasInvokableController (),
166
165
'withForm ' => $ this ->option ('with-form ' ) ?? false ,
167
- 'routes ' => $ routes ->map (fn ($ r ) => [
168
- 'tempMethod ' => $ r ->jsMethod () . md5 ($ r ->uri ()),
166
+ 'routes ' => $ routes ->map (fn ($ r ) => [
167
+ 'tempMethod ' => $ r ->jsMethod (). md5 ($ r ->uri ()),
169
168
'parameters ' => $ r ->parameters (),
170
169
'verbs ' => $ r ->verbs (),
171
170
'uri ' => $ r ->uri (),
@@ -191,18 +190,18 @@ private function writeControllerMethodExport(Route $route, string $path): void
191
190
192
191
private function writeNamedFile (Collection $ routes , string $ namespace ): void
193
192
{
194
- $ path = join_paths ($ this ->base (), ...explode ('. ' , $ namespace )) . '.ts ' ;
193
+ $ path = join_paths ($ this ->base (), ...explode ('. ' , $ namespace )). '.ts ' ;
195
194
196
195
$ this ->appendCommonImports ($ routes , $ path , $ namespace );
197
196
198
- $ routes ->each (fn (Route $ route ) => $ this ->writeNamedMethodExport ($ route , $ path ));
197
+ $ routes ->each (fn (Route $ route ) => $ this ->writeNamedMethodExport ($ route , $ path ));
199
198
200
- $ imports = $ routes ->map (fn (Route $ route ) => $ route ->namedMethod ())->implode (', ' );
199
+ $ imports = $ routes ->map (fn (Route $ route ) => $ route ->namedMethod ())->implode (', ' );
201
200
202
201
$ basename = basename ($ path , '.ts ' );
203
202
$ base = Str::of ($ basename )->when (
204
203
str_contains ($ basename , '- ' ),
205
- fn ($ s ) => $ s ->camel ()
204
+ fn ($ s ) => $ s ->camel ()
206
205
)->toString ();
207
206
208
207
if ($ base !== $ imports ) {
@@ -218,13 +217,13 @@ private function appendCommonImports(Collection $routes, string $path, string $n
218
217
{
219
218
$ imports = ['queryParams ' , 'type QueryParams ' ];
220
219
221
- if ($ routes ->contains (fn (Route $ route ) => $ route ->parameters ()->contains (fn (Parameter $ parameter ) => $ parameter ->optional ))) {
220
+ if ($ routes ->contains (fn (Route $ route ) => $ route ->parameters ()->contains (fn (Parameter $ parameter ) => $ parameter ->optional ))) {
222
221
$ imports [] = 'validateParameters ' ;
223
222
}
224
223
225
224
$ importBase = str_repeat ('/.. ' , substr_count ($ namespace , '. ' ) + 1 );
226
225
227
- $ this ->appendContent ($ path , 'import { ' . implode (', ' , $ imports ) . " } from '. {$ importBase }/wayfinder' \n" );
226
+ $ this ->appendContent ($ path , 'import { ' . implode (', ' , $ imports ). " } from '. {$ importBase }/wayfinder' \n" );
228
227
}
229
228
230
229
private function writeNamedMethodExport (Route $ route , string $ path ): void
@@ -251,9 +250,7 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v
251
250
return ;
252
251
}
253
252
254
- $ normalizeToCamelCase = fn ($ value ) => Str::of ($ value )
255
- ->whenContains (['- ' , '_ ' ], fn (Stringable $ string ) => $ string ->camel ())
256
- ->toString ();
253
+ $ normalizeToCamelCase = fn ($ value ) => str_contains ($ value , '- ' ) ? Str::camel ($ value ) : $ value ;
257
254
258
255
$ children ->each (function ($ grandkids , $ child ) use ($ parent , $ normalizeToCamelCase ) {
259
256
$ grandkids = collect ($ grandkids );
@@ -264,11 +261,13 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v
264
261
265
262
$ directory = join_paths ($ this ->base (), $ parent , $ child );
266
263
267
- $ imports = $ grandkids ->keys ()->map (fn ($ grandkid ) => "import * as {$ normalizeToCamelCase ($ grandkid )} from './ {$ grandkid }' " )->implode (PHP_EOL );
264
+ $ grandKidKeys = $ grandkids ->keys ()->mapWithKeys (fn ($ grandkid ) => [$ normalizeToCamelCase ($ grandkid ) => $ grandkid ]);
265
+
266
+ $ imports = $ grandKidKeys ->map (fn ($ grandkid , $ key ) => "import * as {$ key } from './ {$ grandkid }' " )->implode (PHP_EOL );
268
267
269
268
$ this ->appendContent (join_paths ($ directory , 'index.ts ' ), $ imports );
270
269
271
- $ keys = $ grandkids ->keys ()->map (fn ( $ k ) => str_repeat (' ' , 4 ) . $ normalizeToCamelCase ( $ k )) ->implode (', ' . PHP_EOL );
270
+ $ keys = $ grandKidKeys ->keys ()->map (fn ( $ key ) => str_repeat (' ' , 4 ). $ key ) ->implode (', ' . PHP_EOL );
272
271
273
272
$ varExport = $ normalizeToCamelCase ($ child );
274
273
@@ -321,7 +320,7 @@ private function getDefaultsForMiddleware(string $middleware)
321
320
}
322
321
323
322
$ methodContents = str ($ methodContents )->after ('{ ' )->beforeLast ('} ' )->trim ();
324
- $ tokens = token_get_all ('<?php ' . $ methodContents );
323
+ $ tokens = token_get_all ('<?php ' . $ methodContents );
325
324
$ foundUrlFacade = false ;
326
325
$ defaults = [];
327
326
$ inArray = false ;
0 commit comments