-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.standard-paths.functions.php
More file actions
255 lines (226 loc) · 9.6 KB
/
Copy pathload.standard-paths.functions.php
File metadata and controls
255 lines (226 loc) · 9.6 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php declare(strict_types=1);
// @package webkernel/standard-paths/load.standard-paths.functions.php
use Webkernel\StdPaths\WebkernelComposer;
/**
* Real-ish paths without realpath()
* Normalize a file path by resolving "." and ".." segments,
* collapsing multiple slashes, and ensuring no trailing slash.
*
* @param string $filename Input path to resolve
* @return string Normalized path without trailing slash
*/
function resolveFilename(string $filename): string
{
$isAbsolute = str_starts_with($filename, '/');
$filename = preg_replace('#/+#', '/', $filename);
$parts = explode('/', $filename);
$out = [];
foreach ($parts as $part) {
if ($part === '' || $part === '.') { continue; }
if ($part === '..') { array_pop($out); continue; }
$out[] = $part;
}
$resolved = implode('/', $out);
if ($isAbsolute) {
$resolved = '/' . $resolved;
}
return rtrim($resolved, '/');
}
// =============================================================================
// Composer path helpers — thin wrappers over WebkernelComposer (loaded first)
// =============================================================================
if (!function_exists('project_root')) {
function project_root(?string $path = null): string
{
$root = WebkernelComposer::root();
return $path ? $root . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR) : $root;
}
}
if (!function_exists('webapp_path')) {
function webapp_path(?string $path = null): string
{
$root = WebkernelComposer::root();
return $path ? $root . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR) : $root;
}
}
if (!function_exists('vendor_dir')) {
function vendor_dir(?string $path = null): string
{
$dir = WebkernelComposer::vendorDir();
return $path ? $dir . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR) : $dir;
}
}
if (!function_exists('bin_dir')) {
function bin_dir(?string $path = null): string
{
$dir = WebkernelComposer::binDir();
return $path ? $dir . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR) : $dir;
}
}
if (!function_exists('autoload_file')) {
function autoload_file(): string
{
return WebkernelComposer::autoloadFile();
}
}
// =============================================================================
// webkernel_std_make_subpath
// Primitive filesystem helper. No dependency on any other Webkernel function.
// =============================================================================
if (!function_exists('webkernel_std_make_subpath')) {
/**
* Resolves a path under $basePath, creating it on demand when $makeOnMiss
* is true. Accepts an optional $onError callback for caller-controlled error
* handling; when omitted, throws RuntimeException.
*
* @param string $basePath Absolute base directory.
* @param string $subpath Relative path to append (file or dir).
* @param bool $makeOnMiss Create target automatically if missing.
* @param callable|null $onError fn(string $message): never — called
* instead of throwing RuntimeException.
* @return string Absolute resolved path.
* @throws RuntimeException When $onError is null and an error occurs.
*/
function webkernel_std_make_subpath(
string $basePath,
string $subpath,
bool $makeOnMiss = true,
?callable $onError = null
): string {
$raise = static function (string $message) use ($onError): never {
if ($onError !== null) {
($onError)($message);
// Guarantee never-return contract even if caller forgot it.
throw new \LogicException('$onError must not return: ' . $message);
}
throw new \RuntimeException($message);
};
$base = rtrim($basePath, DIRECTORY_SEPARATOR);
$sub = ltrim($subpath, DIRECTORY_SEPARATOR);
$target = $base . DIRECTORY_SEPARATOR . $sub;
if (file_exists($target)) {
$resolved = realpath($target);
if ($resolved === false) {
$raise(sprintf('Failed to resolve absolute path for: %s', $target));
}
return $resolved;
}
if (!$makeOnMiss) {
$raise(sprintf('Target path does not exist: %s', $target));
}
$isFile = pathinfo($target, PATHINFO_EXTENSION) !== '';
if ($isFile) {
$parent = dirname($target);
if (!is_dir($parent)) {
$prev = umask(0);
$created = @mkdir($parent, 0755, true);
umask($prev);
if (!$created && !is_dir($parent)) {
$raise(sprintf('Failed to create parent directory: %s', $parent));
}
}
if (@touch($target) === false) {
$raise(sprintf('Failed to create file: %s', $target));
}
} else {
$prev = umask(0);
$created = @mkdir($target, 0755, true);
umask($prev);
if (!$created && !is_dir($target)) {
$raise(sprintf('Failed to create directory: %s', $target));
}
}
$resolved = realpath($target);
if ($resolved === false) {
$raise(sprintf('Failed to resolve path after creation: %s', $target));
}
return $resolved;
}
}
// =============================================================================
// webkernel_package
// Resolves paths inside the /packages directory.
// Delegates creation to webkernel_std_make_subpath.
// Uses project_root() — never walks composer.json itself.
// =============================================================================
if (!function_exists('webkernel_package')) {
/**
* Resolves an absolute (or project-relative) path inside the packages
* directory, optionally scoped to a named package and sub-path.
*
* @param string|null $name Package directory name (e.g. 'stdloc').
* @param string|null $subpath Path relative to the package root.
* @param bool $relative Return path relative to project root.
* @param bool $makeOnMiss Create target automatically if missing.
* @param callable|null $onError Custom error handler — see webkernel_std_make_subpath.
* @return string Resolved absolute (or relative) path.
*/
function webkernel_package(
?string $name = null,
?string $subpath = null,
bool $relative = false,
bool $makeOnMiss = true,
?callable $onError = null
): string {
$root = project_root(); // single source of truth
$packagesDir = $root . DIRECTORY_SEPARATOR . 'packages';
// Build the target path from non-null segments.
$segments = array_filter(
[$packagesDir, $name, $subpath],
static fn (?string $v): bool => $v !== null && $v !== ''
);
$abs = implode(DIRECTORY_SEPARATOR, $segments);
// Delegate filesystem work entirely
// When $abs equals $packagesDir (bare call), the directory must already
// exist; creation is skipped when $makeOnMiss is false.
$resolved = webkernel_std_make_subpath(
dirname($abs),
basename($abs),
$makeOnMiss,
$onError
);
if (!$relative) {
return $resolved;
}
$prefix = rtrim($root, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
return str_starts_with($resolved, $prefix)
? substr($resolved, strlen($prefix))
: ltrim(str_replace($root, '', $resolved), DIRECTORY_SEPARATOR);
}
}
// =============================================================================
// webkernel_cache_path
// Thin scoped wrapper: always targets storage/framework/cache.
// =============================================================================
if (!function_exists('webkernel_cache_path')) {
/**
* Resolves or initializes a subpath within the framework cache directory.
*
* @param string $subpath Relative path inside the cache directory.
* @param bool $makeOnMiss Automatically create target if missing.
* @param callable|null $onError Custom error handler.
* @return string Absolute resolved path.
*/
function webkernel_cache_path(
string $subpath,
bool $makeOnMiss = true,
?callable $onError = null
): string {
$cacheBase = project_root() . DIRECTORY_SEPARATOR . 'storage/framework/cache';
return webkernel_std_make_subpath($cacheBase, $subpath, $makeOnMiss, $onError);
}
}
// =============================================================================
// COMPOSER_VENDOR_DIR Environment Variable Setup
// Sets the value of the environment variable. Otherwise, Laravel's
// PackageDiscoverCommand will create an empty array in
// bootstrap/cache/packages.php.
// =============================================================================
putenv('COMPOSER_VENDOR_DIR=' . vendor_dir());
// =============================================================================
// Webkernel Router
// Early dispatch for /__webkernel-app/* happens in public/index.php *after*
// the full autoloader (all "files" entries) so registrations from packages
// like standard-pix are guaranteed to have executed.
// See WebkernelRouter::dispatch().
// =============================================================================