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

Function resolvePhpIncludePath

src/resolution/import-resolver.ts:2023–2055  ·  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

2021 * appended to `$_SERVER['DOCUMENT_ROOT']`, written behind PHP_DOCUMENT_ROOT.
2022 */
2023function resolvePhpIncludePath(
2024 includePath: string,
2025 fromFile: string,
2026 context: ResolutionContext
2027): string | null {
2028 const projectRoot = context.getProjectRoot();
2029 // The web server's root is in its own configuration, not the project's
2030 // code (#3298): a `codegraph.json` `webRoot` names it, or the project's own
2031 // includes show it clearly enough (php-web-root.ts). A guess at the project
2032 // root would link `public/index.php`'s include of
2033 // `$_SERVER['DOCUMENT_ROOT'] . '/config.php'` to the root's config.php,
2034 // not the public/config.php PHP loads, so with neither it links nothing.
2035 // Loaded exactly as written, like `./x`, and never outside the project.
2036 if (includePath.startsWith(`${PHP_DOCUMENT_ROOT}/`)) {
2037 const webRoot = context.getPhpWebRoot ? context.getPhpWebRoot(fromFile) : loadWebRoot(projectRoot);
2038 if (webRoot === null) return null;
2039 const target = underWebRoot(webRoot, includePath.slice(PHP_DOCUMENT_ROOT.length + 1));
2040 return target !== null && context.fileExists(target) ? target : null;
2041 }
2042 const fromDir = path.dirname(path.join(projectRoot, fromFile));
2043 const basePath = path.resolve(fromDir, includePath);
2044 const relativePath = path.relative(projectRoot, basePath).replace(/\\/g, '/');
2045 if (context.fileExists(relativePath)) return relativePath;
2046 // A path anchored to the file's own directory (`./x`, `../x`, as the
2047 // extractor writes `__DIR__ . '/x'`) is loaded exactly as written: PHP adds
2048 // no extension, so `__DIR__ . '/settings.local'` is never settings.local.php.
2049 if (includePath.startsWith('./') || includePath.startsWith('../')) return null;
2050 // The literal may omit the .php extension (e.g. include "config").
2051 for (const ext of EXTENSION_RESOLUTION.php ?? []) {
2052 if (context.fileExists(relativePath + ext)) return relativePath + ext;
2053 }
2054 return null;
2055}
2056
2057/**
2058 * 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