Line data Source code
1 : /*
2 : +----------------------------------------------------------------------+
3 : | LSParrot PHP LSP Extension |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) LSParrot GitHub Organization |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to the 0BSD license that is |
8 : | bundled with this package in the file LICENSE. |
9 : +----------------------------------------------------------------------+
10 : | Author: Go Kudo <zeriyoshi@gmail.com> |
11 : +----------------------------------------------------------------------+
12 : */
13 :
14 :
15 : #include "lsp_internal.h"
16 :
17 200 : static inline zend_string *lsp_composer_autoload_file(lsp_server *server, const char *name)
18 : {
19 : zend_string *vendor_dir, *composer_dir, *path;
20 :
21 200 : vendor_dir = lsp_composer_vendor_dir(server->root);
22 200 : composer_dir = lsp_join_path2(vendor_dir, "composer");
23 200 : path = lsp_join_path2(composer_dir, name);
24 200 : zend_string_release(composer_dir);
25 200 : zend_string_release(vendor_dir);
26 :
27 200 : return path;
28 : }
29 :
30 16 : static inline zend_string *lsp_fqcn_relative_path(const char *value, size_t length, bool psr0)
31 : {
32 : zend_string *relative;
33 : size_t i;
34 :
35 16 : relative = zend_string_alloc(length + sizeof(".php") - 1, 0);
36 :
37 176 : for (i = 0; i < length; i++) {
38 160 : ZSTR_VAL(relative)[i] = (value[i] == '\\' || (psr0 && value[i] == '_')) ? '/' : value[i];
39 : }
40 :
41 16 : memcpy(ZSTR_VAL(relative) + length, ".php", sizeof(".php") - 1);
42 16 : ZSTR_VAL(relative)[ZSTR_LEN(relative)] = '\0';
43 :
44 16 : return relative;
45 : }
46 :
47 16 : static inline zend_string *lsp_autoload_candidate_from_dir(zend_string *dir, zend_string *relative)
48 : {
49 : zend_string *candidate;
50 : bool slash;
51 :
52 16 : slash = lsp_path_has_trailing_separator(dir);
53 16 : candidate = strpprintf(0, "%s%s%s", ZSTR_VAL(dir), slash ? "" : "/", ZSTR_VAL(relative));
54 16 : if (lsp_is_regular_file(candidate)) {
55 16 : return candidate;
56 : }
57 :
58 0 : zend_string_release(candidate);
59 :
60 0 : return NULL;
61 : }
62 :
63 16 : static inline zend_string *lsp_autoload_candidate_from_dirs(zval *dirs_zv, zend_string *relative)
64 : {
65 : zend_string *candidate;
66 : zval *dir_zv;
67 :
68 16 : if (Z_TYPE_P(dirs_zv) == IS_STRING) {
69 0 : return lsp_autoload_candidate_from_dir(Z_STR_P(dirs_zv), relative);
70 : }
71 :
72 16 : if (Z_TYPE_P(dirs_zv) != IS_ARRAY) {
73 0 : return NULL;
74 : }
75 :
76 16 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(dirs_zv), dir_zv) {
77 16 : if (Z_TYPE_P(dir_zv) != IS_STRING) {
78 0 : continue;
79 : }
80 :
81 16 : candidate = lsp_autoload_candidate_from_dir(Z_STR_P(dir_zv), relative);
82 16 : if (candidate) {
83 16 : return candidate;
84 : }
85 : } ZEND_HASH_FOREACH_END();
86 :
87 0 : return NULL;
88 : }
89 :
90 72 : static inline zend_string *lsp_find_autoload_classmap_path(lsp_server *server, zend_string *fqcn)
91 : {
92 : zend_string *path, *classmap_file;
93 : zval classmap, *path_zv;
94 :
95 72 : classmap_file = lsp_composer_autoload_file(server, "autoload_classmap.php");
96 72 : if (!lsp_include_array_file(classmap_file, &classmap)) {
97 48 : zend_string_release(classmap_file);
98 :
99 48 : return NULL;
100 : }
101 :
102 24 : zend_string_release(classmap_file);
103 :
104 24 : path = NULL;
105 24 : path_zv = zend_hash_find(Z_ARRVAL(classmap), fqcn);
106 24 : if (path_zv && Z_TYPE_P(path_zv) == IS_STRING && lsp_is_regular_file(Z_STR_P(path_zv))) {
107 0 : path = zend_string_copy(Z_STR_P(path_zv));
108 : }
109 :
110 24 : zval_ptr_dtor(&classmap);
111 :
112 24 : return path;
113 : }
114 :
115 72 : static inline zend_string *lsp_find_autoload_psr4_path(lsp_server *server, zend_string *fqcn)
116 : {
117 : zend_string *psr4_file, *prefix, *relative, *candidate;
118 : zval psr4, *dirs_zv;
119 : size_t prefix_length;
120 :
121 72 : psr4_file = lsp_composer_autoload_file(server, "autoload_psr4.php");
122 72 : if (!lsp_include_array_file(psr4_file, &psr4)) {
123 48 : zend_string_release(psr4_file);
124 :
125 48 : return NULL;
126 : }
127 24 : zend_string_release(psr4_file);
128 :
129 24 : candidate = NULL;
130 40 : ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(psr4), prefix, dirs_zv) {
131 32 : if (!prefix || ZSTR_LEN(prefix) > ZSTR_LEN(fqcn) || strncasecmp(ZSTR_VAL(fqcn), ZSTR_VAL(prefix), ZSTR_LEN(prefix)) != 0) {
132 16 : continue;
133 : }
134 :
135 16 : prefix_length = ZSTR_LEN(prefix);
136 16 : relative = lsp_fqcn_relative_path(ZSTR_VAL(fqcn) + prefix_length, ZSTR_LEN(fqcn) - prefix_length, false);
137 16 : candidate = lsp_autoload_candidate_from_dirs(dirs_zv, relative);
138 :
139 16 : zend_string_release(relative);
140 :
141 16 : if (candidate) {
142 16 : break;
143 : }
144 : } ZEND_HASH_FOREACH_END();
145 :
146 24 : zval_ptr_dtor(&psr4);
147 :
148 24 : return candidate;
149 : }
150 :
151 56 : static inline zend_string *lsp_find_autoload_psr0_path(lsp_server *server, zend_string *fqcn)
152 : {
153 : zend_string *psr0_file, *prefix, *relative, *candidate;
154 : zval psr0, *dirs_zv;
155 : size_t prefix_length;
156 :
157 56 : psr0_file = lsp_composer_autoload_file(server, "autoload_namespaces.php");
158 56 : if (!lsp_include_array_file(psr0_file, &psr0)) {
159 48 : zend_string_release(psr0_file);
160 :
161 48 : return NULL;
162 : }
163 8 : zend_string_release(psr0_file);
164 :
165 8 : candidate = NULL;
166 8 : ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(psr0), prefix, dirs_zv) {
167 0 : if (!prefix || ZSTR_LEN(prefix) > ZSTR_LEN(fqcn) || strncasecmp(ZSTR_VAL(fqcn), ZSTR_VAL(prefix), ZSTR_LEN(prefix)) != 0) {
168 0 : continue;
169 : }
170 :
171 0 : prefix_length = ZSTR_LEN(prefix);
172 0 : relative = lsp_fqcn_relative_path(ZSTR_VAL(fqcn) + prefix_length, ZSTR_LEN(fqcn) - prefix_length, true);
173 0 : candidate = lsp_autoload_candidate_from_dirs(dirs_zv, relative);
174 0 : zend_string_release(relative);
175 0 : if (candidate) {
176 0 : break;
177 : }
178 : } ZEND_HASH_FOREACH_END();
179 :
180 8 : zval_ptr_dtor(&psr0);
181 :
182 8 : return candidate;
183 : }
184 :
185 72 : static inline zend_string *lsp_find_autoload_class_like_path(lsp_server *server, zend_string *fqcn)
186 : {
187 : zend_string *path;
188 :
189 72 : path = lsp_find_autoload_classmap_path(server, fqcn);
190 72 : if (path) {
191 0 : return path;
192 : }
193 :
194 72 : path = lsp_find_autoload_psr4_path(server, fqcn);
195 72 : if (path) {
196 16 : return path;
197 : }
198 :
199 56 : return lsp_find_autoload_psr0_path(server, fqcn);
200 : }
201 :
202 784 : extern zend_string *lsp_find_project_symbol_path(lsp_server *server, char expected_kind, zend_string *fqcn)
203 : {
204 : const char *path;
205 784 : size_t path_length = 0;
206 784 : char found_kind = '\0';
207 :
208 784 : lsp_index_join_worker(server);
209 :
210 : /* O(1) hash probe over the symbol-key map instead of the historical
211 : * linear arena scan. */
212 784 : path = lsp_symbol_index_lookup(&server->symbol_index, expected_kind, fqcn, &found_kind, NULL, &path_length);
213 784 : if (path && path_length > 0) {
214 712 : return zend_string_init(path, path_length, 0);
215 : }
216 :
217 72 : if (expected_kind == LSP_SYMBOL_CLASS) {
218 72 : return lsp_find_autoload_class_like_path(server, fqcn);
219 : }
220 :
221 0 : return NULL;
222 : }
|