* The `namespace` and class imports of a file that declares routes: `use A\B;`, * `use A\B as C;`, a comma list `use A\B, C\D;` and a group * `use App\Http\Controllers\{Admin\UserController, X as Y};`. `use function` * and `use const` import no class. Only the header counts: a `use` inside a * c
(safe: string)
| 487 | * class body brings in a trait. |
| 488 | */ |
| 489 | function phpRouteScope(safe: string): PhpRouteScope { |
| 490 | const namespace = /^\s*namespace\s+([\w\\]+)\s*[;{]/m.exec(safe)?.[1] ?? ''; |
| 491 | const header = safe.slice(0, safe.search(/^\s*(?:(?:abstract|final|readonly)\s+)*(?:class|trait|interface|enum)\s/m) >>> 0 || safe.length); |
| 492 | const uses = new Map<string, string>(); |
| 493 | for (const m of header.matchAll(/^\s*use\s+(?!function\b|const\b)([^;]+);/gm)) { |
| 494 | const body = m[1]!.trim(); |
| 495 | const group = /^\\?([\w\\]+?)\\?\s*\{([^}]*)\}$/.exec(body); |
| 496 | for (const raw of (group ? group[2]! : body).split(',')) { |
| 497 | const item = raw.trim(); |
| 498 | if (/^(?:function|const)\s/i.test(item)) continue; |
| 499 | const named = /^\\?([A-Za-z_][\w\\]*)(?:\s+as\s+(\w+))?$/i.exec(item); |
| 500 | if (!named) continue; |
| 501 | const fqn = group ? `${group[1]!}\\${named[1]!}` : named[1]!; |
| 502 | uses.set((named[2] ?? fqn.split('\\').pop()!).toLowerCase(), fqn); |
| 503 | } |
| 504 | } |
| 505 | return { namespace, uses }; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * The class a routes file means by `X::class`, as PHP resolves the name there, |