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

Function resolvePhpIncludePath

src/resolution/import-resolver.ts:2269–2301  ·  view source on GitHub ↗

* Resolve a PHP include/require path to a project-relative file path. * * PHP resolves includes relative to the including file's directory (the * common case for procedural codebases); php.ini `include_path` is not * modeled. Callers pass an already-extracted static path: a literal, or one * ap

(
  includePath: string,
  fromFile: string,
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

2267 * appended to `$_SERVER['DOCUMENT_ROOT']`, written behind PHP_DOCUMENT_ROOT.
2268 */
2269function resolvePhpIncludePath(
2270 includePath: string,
2271 fromFile: string,
2272 context: ResolutionContext
2273): string | null {
2274 const projectRoot = context.getProjectRoot();
2275 // The web server's root is in its own configuration, not the project's
2276 // code (#3298): a `codegraph.json` `webRoot` names it, or the project's own
2277 // includes show it clearly enough (php-web-root.ts). A guess at the project
2278 // root would link `public/index.php`'s include of
2279 // `$_SERVER['DOCUMENT_ROOT'] . '/config.php'` to the root's config.php,
2280 // not the public/config.php PHP loads, so with neither it links nothing.
2281 // Loaded exactly as written, like `./x`, and never outside the project.
2282 if (includePath.startsWith(`${PHP_DOCUMENT_ROOT}/`)) {
2283 const webRoot = context.getPhpWebRoot ? context.getPhpWebRoot(fromFile) : loadWebRoot(projectRoot);
2284 if (webRoot === null) return null;
2285 const target = underWebRoot(webRoot, includePath.slice(PHP_DOCUMENT_ROOT.length + 1));
2286 return target !== null && context.fileExists(target) ? target : null;
2287 }
2288 const fromDir = path.dirname(path.join(projectRoot, fromFile));
2289 const basePath = path.resolve(fromDir, includePath);
2290 const relativePath = path.relative(projectRoot, basePath).replace(/\\/g, '/');
2291 if (context.fileExists(relativePath)) return relativePath;
2292 // A path anchored to the file's own directory (`./x`, `../x`, as the
2293 // extractor writes `__DIR__ . '/x'`) is loaded exactly as written: PHP adds
2294 // no extension, so `__DIR__ . '/settings.local'` is never settings.local.php.
2295 if (includePath.startsWith('./') || includePath.startsWith('../')) return null;
2296 // The literal may omit the .php extension (e.g. include "config").
2297 for (const ext of EXTENSION_RESOLUTION.php ?? []) {
2298 if (context.fileExists(relativePath + ext)) return relativePath + ext;
2299 }
2300 return null;
2301}
2302
2303/**
2304 * Extract import mappings from a file

Callers 1

resolveViaImportFunction · 0.85

Calls 7

loadWebRootFunction · 0.90
underWebRootFunction · 0.90
getPhpWebRootMethod · 0.80
fileExistsMethod · 0.80
getProjectRootMethod · 0.65
resolveMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected