MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / routeFileMounts

Function routeFileMounts

src/resolution/frameworks/laravel.ts:355–402  ·  view source on GitHub ↗

* The routes files this file mounts, each with the prefix a request carries * into it: a group whose argument is the file, a `require` / `include` / * `loadRoutesFrom` inside a group, and Laravel 11's `withRouting(api:, web:)`. * `customLoader` is set when `withRouting(using:)` loads routes by co

(
  safe: string,
  file: string,
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

353 * pass cannot read.
354 */
355function routeFileMounts(
356 safe: string,
357 file: string,
358 context: ResolutionContext
359): { mounts: Array<{ source: string; target: string; prefix: string }>; customLoader: boolean } {
360 const mounts: Array<{ source: string; target: string; prefix: string }> = [];
361 const groups = routeGroups(safe);
362 const add = (expr: string, prefix: string) => {
363 const target = resolvePhpPath(expr, file, context);
364 if (target) mounts.push({ source: file, target, prefix });
365 };
366
367 for (const g of groups) {
368 // ->group(base_path('routes/v1.php')), Route::group([...], base_path(…))
369 const body = g.args[g.args.length - 1] ?? '';
370 if (!/^\s*(?:static\s+)?(?:function|fn)\b/.test(body)) add(body, prefixAt(groups, g.open + 1));
371 }
372 const load = /\b(?:require|include)(?:_once)?\b([^;]*);|\bloadRoutesFrom\s*\(([^;]*)\)\s*;/g;
373 let m: RegExpExecArray | null;
374 while ((m = load.exec(safe)) !== null) {
375 let expr = (m[1] ?? m[2] ?? '').trim();
376 // `require(__DIR__.'/x.php')` — drop parens only when they wrap the whole expression.
377 if (expr.startsWith('(') && matchParen(expr, 0) === expr.length - 1) expr = expr.slice(1, -1);
378 add(expr, prefixAt(groups, m.index));
379 }
380
381 let customLoader = false;
382 const routing = /\bwithRouting\s*\(/g;
383 while ((m = routing.exec(safe)) !== null) {
384 const open = m.index + m[0].length - 1;
385 const close = matchParen(safe, open);
386 if (close < 0) continue;
387 const named = new Map<string, string>();
388 for (const arg of splitArgs(safe.slice(open + 1, close))) {
389 const kv = /^\s*(\w+)\s*:\s*([\s\S]*)$/.exec(arg);
390 if (kv) named.set(kv[1]!, kv[2]!);
391 }
392 if (named.has('using')) customLoader = true;
393 const apiPrefix = named.has('apiPrefix') ? literalOrPlaceholder(named.get('apiPrefix')!) : 'api';
394 for (const [key, prefix] of [['api', apiPrefix], ['web', '']] as const) {
395 const value = named.get(key);
396 if (!value) continue;
397 const items = /^\s*\[/.test(value) ? splitArgs(value.trim().slice(1, -1)) : [value];
398 for (const item of items) add(item, prefix);
399 }
400 }
401 return { mounts, customLoader };
402}
403
404/**
405 * A PHP path expression naming a file in the project: `base_path('routes/x.php')`,

Callers 1

postExtractFunction · 0.85

Calls 10

routeGroupsFunction · 0.85
prefixAtFunction · 0.85
literalOrPlaceholderFunction · 0.85
hasMethod · 0.80
addFunction · 0.70
matchParenFunction · 0.70
splitArgsFunction · 0.70
execMethod · 0.65
getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected