Resolve a PHP class import `use A\B\C;` the way composer's PSR-4 autoloader * does: for every autoload.psr-4 prefix covering the name, longest first, the * class lives in exactly / .php with sub-namespaces as * subdirectories; the first existing file wins. When a prefix covers the name * but no such file exists the import is UNRESOLVED: it must never fall through * to the nam
| 2119 | * came first. `use function` / `use const` name namespace members, not class |
| 2120 | * files, so they are not applicable. */ |
| 2121 | static psr4_outcome_t resolve_php_psr4_class(const cbm_gbuf_t *gbuf, const char *source_file_qn, |
| 2122 | const CBMImport *imp, const cbm_gbuf_node_t **out) { |
| 2123 | *out = NULL; |
| 2124 | CBMHashTable *pkgmap = cbm_pipeline_get_pkgmap(); |
| 2125 | if (!pkgmap || imp->kind != CBM_IMPORT_KIND_DEFAULT) { |
| 2126 | return PSR4_NOT_APPLICABLE; |
| 2127 | } |
| 2128 | const char *name = imp->module_path; |
| 2129 | if (name[0] == '\\') { |
| 2130 | name++; /* fully qualified `use \App\X;` */ |
| 2131 | } |
| 2132 | char key[PKGMAP_PATH_BUF]; |
| 2133 | size_t name_len = strlen(name); |
| 2134 | if (name_len == 0 || name_len >= sizeof(key) || !strchr(name, '\\')) { |
| 2135 | return PSR4_NOT_APPLICABLE; |
| 2136 | } |
| 2137 | bool covered = false; |
| 2138 | /* Right to left: each '\' ends a candidate prefix, longest first. */ |
| 2139 | for (size_t cut = name_len - SKIP_ONE; cut > 0; cut--) { |
| 2140 | if (name[cut - SKIP_ONE] != '\\') { |
| 2141 | continue; |
| 2142 | } |
| 2143 | memcpy(key, name, cut); |
| 2144 | key[cut] = '\0'; |
| 2145 | const char *dir = (const char *)cbm_ht_get(pkgmap, key); |
| 2146 | if (!dir) { |
| 2147 | continue; |
| 2148 | } |
| 2149 | covered = true; |
| 2150 | char rel[PKGMAP_PATH_BUF]; |
| 2151 | int w = snprintf(rel, sizeof(rel), "%s%s%s.php", dir, dir[0] ? "/" : "", name + cut); |
| 2152 | if (w <= 0 || (size_t)w >= sizeof(rel)) { |
| 2153 | continue; |
| 2154 | } |
| 2155 | for (char *c = rel; *c; c++) { |
| 2156 | if (*c == '\\') { |
| 2157 | *c = '/'; |
| 2158 | } |
| 2159 | } |
| 2160 | const cbm_gbuf_node_t *file = psr4_file_node(gbuf, rel); |
| 2161 | if (file && (!source_file_qn || !file->qualified_name || |
| 2162 | strcmp(file->qualified_name, source_file_qn) != 0)) { |
| 2163 | *out = file; |
| 2164 | return PSR4_RESOLVED; |
| 2165 | } |
| 2166 | } |
| 2167 | return covered ? PSR4_UNRESOLVED : PSR4_NOT_APPLICABLE; |
| 2168 | } |
| 2169 | |
| 2170 | static bool psr4_class_label(const char *label) { |
| 2171 | return label && (strcmp(label, "Class") == 0 || strcmp(label, "Interface") == 0 || |
no test coverage detected