Skip to content

Commit 721b4b1

Browse files
committed
Add support for generating route while routes are cached
1 parent fe3e183 commit 721b4b1

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

build.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { execSync } from "node:child_process";
22
import path from "node:path";
33

4-
export default function setup() {
4+
const testbenchDir = path.join(__dirname, "vendor", "bin", "testbench");
5+
const artisan = (command: string): void => console.error(execSync(`${testbenchDir} ${command}`).toString('utf8'))
6+
7+
export function setup(): void {
58
try {
6-
const testbenchDir = path.join(__dirname, "vendor", "bin", "testbench");
9+
if (process.env.WAYFINDER_CACHE_ROUTES) {
10+
artisan('route:cache')
11+
} else {
12+
artisan('route:clear')
13+
}
714

8-
execSync(
9-
`${testbenchDir} wayfinder:generate --path=workbench/resources/js --with-form`,
10-
);
15+
artisan('wayfinder:generate --path=workbench/resources/js --with-form')
1116
} catch (error) {
12-
console.error(
13-
`Wayfinder build error.\n----------${error.output}\n----------`,
14-
);
17+
console.error(`Wayfinder build error\n----------${error.output}\n----------`);
18+
1519
process.exit(1);
1620
}
1721
}
22+
23+
export function teardown(): void {
24+
artisan('route:clear')
25+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"type": "module",
33
"scripts": {
4-
"test": "vitest run"
4+
"test": "vitest run && npm run test-routes-cached",
5+
"test-routes-cached": "WAYFINDER_CACHE_ROUTES=1 vitest run"
56
},
67
"devDependencies": {
78
"@types/node": "^22.7.5",

src/Route.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Laravel\Wayfinder;
44

5+
use Closure;
56
use Illuminate\Contracts\Routing\UrlRoutable;
67
use Illuminate\Routing\Route as BaseRoute;
8+
use Illuminate\Routing\RouteAction;
79
use Illuminate\Support\Collection;
810
use Illuminate\Support\Str;
911
use Laravel\SerializableClosure\Support\ReflectionClosure;
@@ -127,7 +129,7 @@ public function controllerPath(): string
127129
$controller = $this->controller();
128130

129131
if ($controller === '\\Closure') {
130-
return $this->relativePath((new ReflectionClosure($this->base->getAction()['uses']))->getFileName());
132+
return $this->relativePath((new ReflectionClosure($this->closure()))->getFileName());
131133
}
132134

133135
if (! class_exists($controller)) {
@@ -142,7 +144,7 @@ public function controllerMethodLineNumber(): int
142144
$controller = $this->controller();
143145

144146
if ($controller === '\\Closure') {
145-
return (new ReflectionClosure($this->base->getAction()['uses']))->getStartLine();
147+
return (new ReflectionClosure($this->closure()))->getStartLine();
146148
}
147149

148150
if (! class_exists($controller)) {
@@ -167,4 +169,11 @@ private function relativePath(string $path)
167169
{
168170
return ltrim(str_replace(base_path(), '', $path), DIRECTORY_SEPARATOR);
169171
}
172+
173+
private function closure(): Closure
174+
{
175+
return RouteAction::containsSerializedClosure($this->base->getAction())
176+
? unserialize($this->base->getAction('uses'))->getClosure()
177+
: $this->base->getAction('uses');
178+
}
170179
}

0 commit comments

Comments
 (0)