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

Function resolvePhpIncludePath

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

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