Skip to content

Commit abe4314

Browse files
committed
Update GenerateCommand.php
1 parent b386630 commit abe4314

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

src/GenerateCommand.php

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Illuminate\Routing\UrlGenerator;
1010
use Illuminate\Support\Collection;
1111
use Illuminate\Support\Str;
12-
use Illuminate\Support\Stringable;
1312
use Illuminate\View\Compilers\BladeCompiler;
1413
use Illuminate\View\Factory;
1514
use ReflectionProperty;
@@ -43,7 +42,7 @@ public function __construct(
4342

4443
public function handle()
4544
{
46-
$this->view->addNamespace('wayfinder', __DIR__ . '/../resources');
45+
$this->view->addNamespace('wayfinder', __DIR__.'/../resources');
4746
$this->view->addExtension('blade.ts', 'blade');
4847
$this->bladeCompiler->directive('trimDeadspace', function () {
4948
return '<?php ob_start(); ?>';
@@ -64,43 +63,43 @@ public function handle()
6463
$this->urlDefaults[$middleware] ??= $this->getDefaultsForMiddleware($middleware);
6564

6665
return $this->urlDefaults[$middleware];
67-
})->flatMap(fn($r) => $r);
66+
})->flatMap(fn ($r) => $r);
6867

6968
return new Route($route, $defaults, $this->forcedScheme, $this->forcedRoot);
7069
});
7170

7271
$this->files->deleteDirectory($this->base());
7372

7473
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());
7675

7776
$controllers->undot()->each($this->writeBarrelFiles(...));
7877
$controllers->each($this->writeControllerFile(...));
7978

8079
$this->writeContent();
8180

82-
info('[Wayfinder] Generated actions in ' . $this->base());
81+
info('[Wayfinder] Generated actions in '.$this->base());
8382
}
8483

8584
$this->pathDirectory = 'routes';
8685

8786
$this->files->deleteDirectory($this->base());
8887

8988
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(), '.'));
9190

9291
$named->undot()->each($this->writeBarrelFiles(...));
9392
$named->each($this->writeNamedFile(...));
9493

9594
$this->writeContent();
9695

97-
info('[Wayfinder] Generated routes in ' . $this->base());
96+
info('[Wayfinder] Generated routes in '.$this->base());
9897
}
9998

10099
$this->pathDirectory = 'wayfinder';
101100

102101
$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'));
104103
}
105104

106105
private function appendContent($path, $content): void
@@ -123,28 +122,28 @@ private function writeContent(): void
123122

124123
private function writeControllerFile(Collection $routes, string $namespace): void
125124
{
126-
$path = join_paths($this->base(), ...explode('.', $namespace)) . '.ts';
125+
$path = join_paths($this->base(), ...explode('.', $namespace)).'.ts';
127126

128127
$this->appendCommonImports($routes, $path, $namespace);
129128

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) {
131130
if ($methodRoutes->count() === 1) {
132131
return $this->writeControllerMethodExport($methodRoutes->first(), $path);
133132
}
134133

135134
return $this->writeMultiRouteControllerMethodExport($methodRoutes, $path);
136135
});
137136

138-
[$invokable, $methods] = $routes->partition(fn(Route $route) => $route->hasInvokableController());
137+
[$invokable, $methods] = $routes->partition(fn (Route $route) => $route->hasInvokableController());
139138

140139
$defaultExport = $invokable->isNotEmpty() ? $invokable->first()->jsMethod() : last(explode('.', $namespace));
141140

142141
if ($invokable->isEmpty()) {
143142
$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(', ');
145144
$methodProps .= ' }';
146145
} 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);
148147
}
149148

150149
$this->appendContent($path, <<<JAVASCRIPT
@@ -164,8 +163,8 @@ private function writeMultiRouteControllerMethodExport(Collection $routes, strin
164163
'controller' => $routes->first()->controller(),
165164
'isInvokable' => $routes->first()->hasInvokableController(),
166165
'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()),
169168
'parameters' => $r->parameters(),
170169
'verbs' => $r->verbs(),
171170
'uri' => $r->uri(),
@@ -191,18 +190,18 @@ private function writeControllerMethodExport(Route $route, string $path): void
191190

192191
private function writeNamedFile(Collection $routes, string $namespace): void
193192
{
194-
$path = join_paths($this->base(), ...explode('.', $namespace)) . '.ts';
193+
$path = join_paths($this->base(), ...explode('.', $namespace)).'.ts';
195194

196195
$this->appendCommonImports($routes, $path, $namespace);
197196

198-
$routes->each(fn(Route $route) => $this->writeNamedMethodExport($route, $path));
197+
$routes->each(fn (Route $route) => $this->writeNamedMethodExport($route, $path));
199198

200-
$imports = $routes->map(fn(Route $route) => $route->namedMethod())->implode(', ');
199+
$imports = $routes->map(fn (Route $route) => $route->namedMethod())->implode(', ');
201200

202201
$basename = basename($path, '.ts');
203202
$base = Str::of($basename)->when(
204203
str_contains($basename, '-'),
205-
fn($s) => $s->camel()
204+
fn ($s) => $s->camel()
206205
)->toString();
207206

208207
if ($base !== $imports) {
@@ -218,13 +217,13 @@ private function appendCommonImports(Collection $routes, string $path, string $n
218217
{
219218
$imports = ['queryParams', 'type QueryParams'];
220219

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))) {
222221
$imports[] = 'validateParameters';
223222
}
224223

225224
$importBase = str_repeat('/..', substr_count($namespace, '.') + 1);
226225

227-
$this->appendContent($path, 'import { ' . implode(', ', $imports) . " } from '.{$importBase}/wayfinder'\n");
226+
$this->appendContent($path, 'import { '.implode(', ', $imports)." } from '.{$importBase}/wayfinder'\n");
228227
}
229228

230229
private function writeNamedMethodExport(Route $route, string $path): void
@@ -251,9 +250,7 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v
251250
return;
252251
}
253252

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;
257254

258255
$children->each(function ($grandkids, $child) use ($parent, $normalizeToCamelCase) {
259256
$grandkids = collect($grandkids);
@@ -264,11 +261,13 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v
264261

265262
$directory = join_paths($this->base(), $parent, $child);
266263

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);
268267

269268
$this->appendContent(join_paths($directory, 'index.ts'), $imports);
270269

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);
272271

273272
$varExport = $normalizeToCamelCase($child);
274273

@@ -321,7 +320,7 @@ private function getDefaultsForMiddleware(string $middleware)
321320
}
322321

323322
$methodContents = str($methodContents)->after('{')->beforeLast('}')->trim();
324-
$tokens = token_get_all('<?php ' . $methodContents);
323+
$tokens = token_get_all('<?php '.$methodContents);
325324
$foundUrlFacade = false;
326325
$defaults = [];
327326
$inArray = false;

0 commit comments

Comments
 (0)