-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLocalValetDriver.php
More file actions
136 lines (121 loc) · 4.86 KB
/
Copy pathLocalValetDriver.php
File metadata and controls
136 lines (121 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* Custom Valet Driver for VoxelSite.
*
* Laravel Herd uses Valet under the hood. This driver tells Valet
* how to route VoxelSite's URLs:
*
* - /_studio/api/* → _studio/api/router.php
* - /_studio/install.php → serve directly
* - /_studio/ui/* → serve static files
* - /_studio/* → _studio/index.php (SPA)
* - /* → serve static files or index.php (clean URLs)
*
* This file is development-only. On production (Apache shared hosting),
* the .htaccess files handle the same routing.
*/
class LocalValetDriver extends \Valet\Drivers\BasicValetDriver
{
/**
* Determine if this driver should serve the request.
*/
public function serves(string $sitePath, string $siteName, string $uri): bool
{
// This driver serves any site that has a _studio directory
return is_dir($sitePath . '/_studio');
}
/**
* Determine if the incoming request is for a static file.
*/
public function isStaticFile(string $sitePath, string $siteName, string $uri): string|false
{
// Serve static UI assets directly (studio CSS, JS, fonts)
if (preg_match('#^/_studio/ui/#', $uri)) {
$filePath = $sitePath . $uri;
if (is_file($filePath)) {
return $filePath;
}
}
// Serve preview static files (non-PHP assets referenced by preview pages)
// PHP files go through the preview endpoint for rendering + hot-reload injection
if (preg_match('#^/_studio/preview/.+\.(css|js|json|svg|png|jpg|jpeg|gif|webp|avif|ico|woff|woff2|ttf|otf)$#i', $uri)) {
$filePath = $sitePath . $uri;
if (is_file($filePath)) {
return $filePath;
}
}
// Serve root static files (CSS, JS, images for the generated website)
if (preg_match('#^/assets/#', $uri)) {
$filePath = $sitePath . $uri;
if (is_file($filePath)) {
return $filePath;
}
}
// Serve root static files (not PHP, not in _studio/ or assets/)
if (!str_starts_with($uri, '/_studio/') && !str_starts_with($uri, '/assets/')) {
$filePath = $sitePath . $uri;
if (is_file($filePath)) {
return $filePath;
}
}
return false;
}
/**
* Get the fully resolved path to the application's front controller.
*/
public function frontControllerPath(string $sitePath, string $siteName, string $uri): ?string
{
// Ensure DOCUMENT_ROOT is set — Valet/Herd may leave it empty,
// which breaks AI-generated code using $_SERVER['DOCUMENT_ROOT'].
$_SERVER['DOCUMENT_ROOT'] = $sitePath;
// API routes → router.php
if (preg_match('#^/_studio/api/#', $uri)) {
// Set REQUEST_URI so the router can parse the path
$_SERVER['REQUEST_URI'] = $uri;
return $sitePath . '/_studio/api/router.php';
}
// Install page
if ($uri === '/_studio/install.php' || $uri === '/_studio/install') {
return $sitePath . '/_studio/install.php';
}
// Studio SPA (any /_studio/* route that isn't a static file or API)
if (str_starts_with($uri, '/_studio/') || $uri === '/_studio') {
return $sitePath . '/_studio/index.php';
}
// Root website: / → index.php or default placeholder
if ($uri === '/' || $uri === '') {
if (is_file($sitePath . '/index.php')) {
return $sitePath . '/index.php';
}
// No published site yet — show the pre-install placeholder
return $sitePath . '/_studio/data/default-index.php';
}
// Shipped PHP endpoints — serve directly when requested by exact path
// (submit.php for forms, mcp.php for AI tool integration)
if ($uri === '/submit.php') {
return $sitePath . '/submit.php';
}
if ($uri === '/mcp.php') {
return $sitePath . '/mcp.php';
}
// Clean URLs: /about → about.php or about.html
$cleanPath = ltrim($uri, '/');
if ($cleanPath) {
// First try the exact path (handles URIs that already include .php)
if (is_file($sitePath . '/' . $cleanPath)) {
return $sitePath . '/' . $cleanPath;
}
if (is_file($sitePath . '/' . $cleanPath . '.php')) {
return $sitePath . '/' . $cleanPath . '.php';
}
if (is_file($sitePath . '/' . $cleanPath . '.html')) {
return $sitePath . '/' . $cleanPath . '.html';
}
}
// Fallback — serve homepage or pre-install placeholder
if (is_file($sitePath . '/index.php')) {
return $sitePath . '/index.php';
}
return $sitePath . '/_studio/data/default-index.php';
}
}