/src/php-src/ext/spl/php_spl.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Marcus Boerger <helly@php.net> | |
12 | | +----------------------------------------------------------------------+ |
13 | | */ |
14 | | |
15 | | #ifdef HAVE_CONFIG_H |
16 | | #include <config.h> |
17 | | #endif |
18 | | |
19 | | #include "php.h" |
20 | | #include "php_main.h" |
21 | | #include "ext/standard/info.h" |
22 | | #include "php_spl.h" |
23 | | #include "php_spl_arginfo.h" |
24 | | #include "spl_functions.h" |
25 | | #include "spl_array.h" |
26 | | #include "spl_directory.h" |
27 | | #include "spl_iterators.h" |
28 | | #include "spl_exceptions.h" |
29 | | #include "spl_observer.h" |
30 | | #include "spl_dllist.h" |
31 | | #include "spl_fixedarray.h" |
32 | | #include "spl_heap.h" |
33 | | #include "zend_autoload.h" |
34 | | #include "zend_exceptions.h" |
35 | | #include "zend_interfaces.h" |
36 | | |
37 | | ZEND_TLS zend_string *spl_autoload_extensions; |
38 | | |
39 | 0 | #define SPL_DEFAULT_FILE_EXTENSIONS ".inc,.php" |
40 | | |
41 | | static zend_class_entry * spl_find_ce_by_name(zend_string *name, bool autoload) |
42 | 0 | { |
43 | 0 | zend_class_entry *ce = zend_lookup_class_ex(name, NULL, autoload ? 0 : ZEND_FETCH_CLASS_NO_AUTOLOAD); |
44 | 0 | if (ce == NULL) { |
45 | 0 | php_error_docref(NULL, E_WARNING, "Class %s does not exist%s", ZSTR_VAL(name), autoload ? " and could not be loaded" : ""); |
46 | 0 | return NULL; |
47 | 0 | } |
48 | | |
49 | 0 | return ce; |
50 | 0 | } |
51 | | |
52 | | static void spl_add_class_name(HashTable *list, zend_string *name) |
53 | 220 | { |
54 | 220 | zval t; |
55 | 220 | ZVAL_STR_COPY(&t, name); |
56 | 220 | zend_hash_add(list, name, &t); |
57 | 220 | } |
58 | | |
59 | | static void spl_add_interfaces(HashTable *list, const zend_class_entry *pce) |
60 | 0 | { |
61 | 0 | if (pce->num_interfaces) { |
62 | 0 | ZEND_ASSERT(pce->ce_flags & ZEND_ACC_LINKED); |
63 | 0 | for (uint32_t num_interfaces = 0; num_interfaces < pce->num_interfaces; num_interfaces++) { |
64 | 0 | spl_add_class_name(list, pce->interfaces[num_interfaces]->name); |
65 | 0 | } |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | | static void spl_add_traits(HashTable *list, const zend_class_entry *pce) |
70 | 0 | { |
71 | 0 | for (uint32_t num_traits = 0; num_traits < pce->num_traits; num_traits++) { |
72 | 0 | spl_add_class_name(list, pce->trait_names[num_traits].name); |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | | static void spl_add_classes(HashTable *list, const zend_class_entry *pce, bool only_classes, bool only_interfaces) |
77 | 440 | { |
78 | 440 | ZEND_ASSERT(pce); |
79 | 440 | ZEND_ASSERT(!(only_classes && only_interfaces) && "Cannot have both only classes and only interfaces be enabled"); |
80 | 440 | if ( |
81 | 440 | (only_classes && (pce->ce_flags & ZEND_ACC_INTERFACE) == ZEND_ACC_INTERFACE) |
82 | 420 | || (only_interfaces && (pce->ce_flags & ZEND_ACC_INTERFACE) == 0) |
83 | 440 | ) { |
84 | 220 | return; |
85 | 220 | } |
86 | 220 | spl_add_class_name(list, pce->name); |
87 | 220 | } |
88 | | |
89 | | /* {{{ Return an array containing the names of all parent classes */ |
90 | | PHP_FUNCTION(class_parents) |
91 | 0 | { |
92 | 0 | zval *obj; |
93 | 0 | zend_class_entry *ce; |
94 | 0 | bool autoload = true; |
95 | | |
96 | | /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */ |
97 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) { |
98 | 0 | RETURN_THROWS(); |
99 | 0 | } |
100 | | |
101 | 0 | if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) { |
102 | 0 | zend_wrong_parameter_type_error(1, Z_EXPECTED_OBJECT_OR_STRING, obj); |
103 | 0 | RETURN_THROWS(); |
104 | 0 | } |
105 | | |
106 | 0 | if (Z_TYPE_P(obj) == IS_STRING) { |
107 | 0 | if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) { |
108 | 0 | RETURN_FALSE; |
109 | 0 | } |
110 | 0 | } else { |
111 | 0 | ce = Z_OBJCE_P(obj); |
112 | 0 | } |
113 | | |
114 | 0 | array_init(return_value); |
115 | 0 | const zend_class_entry *parent_class = ce->parent; |
116 | 0 | while (parent_class) { |
117 | 0 | spl_add_class_name(Z_ARR_P(return_value), parent_class->name); |
118 | 0 | parent_class = parent_class->parent; |
119 | 0 | } |
120 | 0 | } |
121 | | /* }}} */ |
122 | | |
123 | | /* {{{ Return all classes and interfaces implemented by SPL */ |
124 | | PHP_FUNCTION(class_implements) |
125 | 0 | { |
126 | 0 | zval *obj; |
127 | 0 | bool autoload = true; |
128 | 0 | const zend_class_entry *ce; |
129 | | |
130 | | /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */ |
131 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) { |
132 | 0 | RETURN_THROWS(); |
133 | 0 | } |
134 | 0 | if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) { |
135 | 0 | zend_wrong_parameter_type_error(1, Z_EXPECTED_OBJECT_OR_STRING, obj); |
136 | 0 | RETURN_THROWS(); |
137 | 0 | } |
138 | | |
139 | 0 | if (Z_TYPE_P(obj) == IS_STRING) { |
140 | 0 | if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) { |
141 | 0 | RETURN_FALSE; |
142 | 0 | } |
143 | 0 | } else { |
144 | 0 | ce = Z_OBJCE_P(obj); |
145 | 0 | } |
146 | | |
147 | 0 | array_init(return_value); |
148 | 0 | spl_add_interfaces(Z_ARR_P(return_value), ce); |
149 | 0 | } |
150 | | /* }}} */ |
151 | | |
152 | | /* {{{ Return all traits used by a class. */ |
153 | | PHP_FUNCTION(class_uses) |
154 | 0 | { |
155 | 0 | zval *obj; |
156 | 0 | bool autoload = true; |
157 | 0 | const zend_class_entry *ce; |
158 | | |
159 | | /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */ |
160 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) { |
161 | 0 | RETURN_THROWS(); |
162 | 0 | } |
163 | 0 | if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) { |
164 | 0 | zend_wrong_parameter_type_error(1, Z_EXPECTED_OBJECT_OR_STRING, obj); |
165 | 0 | RETURN_THROWS(); |
166 | 0 | } |
167 | | |
168 | 0 | if (Z_TYPE_P(obj) == IS_STRING) { |
169 | 0 | if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) { |
170 | 0 | RETURN_FALSE; |
171 | 0 | } |
172 | 0 | } else { |
173 | 0 | ce = Z_OBJCE_P(obj); |
174 | 0 | } |
175 | | |
176 | 0 | array_init(return_value); |
177 | 0 | spl_add_traits(Z_ARR_P(return_value), ce); |
178 | 0 | } |
179 | | /* }}} */ |
180 | | |
181 | | #define SPL_ADD_CLASS(class_name, z_list, only_classes, only_interfaces) \ |
182 | 440 | spl_add_classes(Z_ARR_P(z_list), spl_ce_ ## class_name, only_classes, only_interfaces) |
183 | | |
184 | | #define SPL_LIST_CLASSES(z_list, only_classes, only_interfaces) \ |
185 | 8 | SPL_ADD_CLASS(AppendIterator, z_list, only_classes, only_interfaces); \ |
186 | 8 | SPL_ADD_CLASS(ArrayIterator, z_list, only_classes, only_interfaces); \ |
187 | 8 | SPL_ADD_CLASS(ArrayObject, z_list, only_classes, only_interfaces); \ |
188 | 8 | SPL_ADD_CLASS(BadFunctionCallException, z_list, only_classes, only_interfaces); \ |
189 | 8 | SPL_ADD_CLASS(BadMethodCallException, z_list, only_classes, only_interfaces); \ |
190 | 8 | SPL_ADD_CLASS(CachingIterator, z_list, only_classes, only_interfaces); \ |
191 | 8 | SPL_ADD_CLASS(CallbackFilterIterator, z_list, only_classes, only_interfaces); \ |
192 | 8 | SPL_ADD_CLASS(DirectoryIterator, z_list, only_classes, only_interfaces); \ |
193 | 8 | SPL_ADD_CLASS(DomainException, z_list, only_classes, only_interfaces); \ |
194 | 8 | SPL_ADD_CLASS(EmptyIterator, z_list, only_classes, only_interfaces); \ |
195 | 8 | SPL_ADD_CLASS(FilesystemIterator, z_list, only_classes, only_interfaces); \ |
196 | 8 | SPL_ADD_CLASS(FilterIterator, z_list, only_classes, only_interfaces); \ |
197 | 8 | SPL_ADD_CLASS(GlobIterator, z_list, only_classes, only_interfaces); \ |
198 | 8 | SPL_ADD_CLASS(InfiniteIterator, z_list, only_classes, only_interfaces); \ |
199 | 8 | SPL_ADD_CLASS(InvalidArgumentException, z_list, only_classes, only_interfaces); \ |
200 | 8 | SPL_ADD_CLASS(IteratorIterator, z_list, only_classes, only_interfaces); \ |
201 | 8 | SPL_ADD_CLASS(LengthException, z_list, only_classes, only_interfaces); \ |
202 | 8 | SPL_ADD_CLASS(LimitIterator, z_list, only_classes, only_interfaces); \ |
203 | 8 | SPL_ADD_CLASS(LogicException, z_list, only_classes, only_interfaces); \ |
204 | 8 | SPL_ADD_CLASS(MultipleIterator, z_list, only_classes, only_interfaces); \ |
205 | 8 | SPL_ADD_CLASS(NoRewindIterator, z_list, only_classes, only_interfaces); \ |
206 | 8 | SPL_ADD_CLASS(OuterIterator, z_list, only_classes, only_interfaces); \ |
207 | 8 | SPL_ADD_CLASS(OutOfBoundsException, z_list, only_classes, only_interfaces); \ |
208 | 8 | SPL_ADD_CLASS(OutOfRangeException, z_list, only_classes, only_interfaces); \ |
209 | 8 | SPL_ADD_CLASS(OverflowException, z_list, only_classes, only_interfaces); \ |
210 | 8 | SPL_ADD_CLASS(ParentIterator, z_list, only_classes, only_interfaces); \ |
211 | 8 | SPL_ADD_CLASS(RangeException, z_list, only_classes, only_interfaces); \ |
212 | 8 | SPL_ADD_CLASS(RecursiveArrayIterator, z_list, only_classes, only_interfaces); \ |
213 | 8 | SPL_ADD_CLASS(RecursiveCachingIterator, z_list, only_classes, only_interfaces); \ |
214 | 8 | SPL_ADD_CLASS(RecursiveCallbackFilterIterator, z_list, only_classes, only_interfaces); \ |
215 | 8 | SPL_ADD_CLASS(RecursiveDirectoryIterator, z_list, only_classes, only_interfaces); \ |
216 | 8 | SPL_ADD_CLASS(RecursiveFilterIterator, z_list, only_classes, only_interfaces); \ |
217 | 8 | SPL_ADD_CLASS(RecursiveIterator, z_list, only_classes, only_interfaces); \ |
218 | 8 | SPL_ADD_CLASS(RecursiveIteratorIterator, z_list, only_classes, only_interfaces); \ |
219 | 8 | SPL_ADD_CLASS(RecursiveRegexIterator, z_list, only_classes, only_interfaces); \ |
220 | 8 | SPL_ADD_CLASS(RecursiveTreeIterator, z_list, only_classes, only_interfaces); \ |
221 | 8 | SPL_ADD_CLASS(RegexIterator, z_list, only_classes, only_interfaces); \ |
222 | 8 | SPL_ADD_CLASS(RuntimeException, z_list, only_classes, only_interfaces); \ |
223 | 8 | SPL_ADD_CLASS(SeekableIterator, z_list, only_classes, only_interfaces); \ |
224 | 8 | SPL_ADD_CLASS(SplDoublyLinkedList, z_list, only_classes, only_interfaces); \ |
225 | 8 | SPL_ADD_CLASS(SplFileInfo, z_list, only_classes, only_interfaces); \ |
226 | 8 | SPL_ADD_CLASS(SplFileObject, z_list, only_classes, only_interfaces); \ |
227 | 8 | SPL_ADD_CLASS(SplFixedArray, z_list, only_classes, only_interfaces); \ |
228 | 8 | SPL_ADD_CLASS(SplHeap, z_list, only_classes, only_interfaces); \ |
229 | 8 | SPL_ADD_CLASS(SplMinHeap, z_list, only_classes, only_interfaces); \ |
230 | 8 | SPL_ADD_CLASS(SplMaxHeap, z_list, only_classes, only_interfaces); \ |
231 | 8 | SPL_ADD_CLASS(SplObjectStorage, z_list, only_classes, only_interfaces); \ |
232 | 8 | SPL_ADD_CLASS(SplObserver, z_list, only_classes, only_interfaces); \ |
233 | 8 | SPL_ADD_CLASS(SplPriorityQueue, z_list, only_classes, only_interfaces); \ |
234 | 8 | SPL_ADD_CLASS(SplQueue, z_list, only_classes, only_interfaces); \ |
235 | 8 | SPL_ADD_CLASS(SplStack, z_list, only_classes, only_interfaces); \ |
236 | 8 | SPL_ADD_CLASS(SplSubject, z_list, only_classes, only_interfaces); \ |
237 | 8 | SPL_ADD_CLASS(SplTempFileObject, z_list, only_classes, only_interfaces); \ |
238 | 8 | SPL_ADD_CLASS(UnderflowException, z_list, only_classes, only_interfaces); \ |
239 | 8 | SPL_ADD_CLASS(UnexpectedValueException, z_list, only_classes, only_interfaces); \ |
240 | | |
241 | | /* {{{ Return an array containing the names of all classes and interfaces defined in SPL */ |
242 | | PHP_FUNCTION(spl_classes) |
243 | 0 | { |
244 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
245 | | |
246 | 0 | array_init(return_value); |
247 | |
|
248 | 0 | SPL_LIST_CLASSES(return_value, false, false) |
249 | 0 | } |
250 | | /* }}} */ |
251 | | |
252 | | static bool spl_autoload(zend_string *lc_name, const char *ext, size_t ext_len) /* {{{ */ |
253 | 0 | { |
254 | 0 | zend_string *class_file; |
255 | 0 | zval dummy; |
256 | 0 | zend_file_handle file_handle; |
257 | 0 | zval result; |
258 | |
|
259 | 0 | class_file = zend_string_concat2(ZSTR_VAL(lc_name), ZSTR_LEN(lc_name), ext, ext_len); |
260 | |
|
261 | 0 | #if DEFAULT_SLASH != '\\' |
262 | 0 | { |
263 | 0 | char *ptr = ZSTR_VAL(class_file); |
264 | 0 | const char *end = ptr + ZSTR_LEN(class_file); |
265 | |
|
266 | 0 | while ((ptr = memchr(ptr, '\\', (end - ptr))) != NULL) { |
267 | 0 | *ptr = DEFAULT_SLASH; |
268 | 0 | } |
269 | 0 | } |
270 | 0 | #endif |
271 | |
|
272 | 0 | bool ret = false; |
273 | 0 | zend_stream_init_filename_ex(&file_handle, class_file); |
274 | 0 | if (php_stream_open_for_zend_ex(&file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE) == SUCCESS) { |
275 | 0 | zend_string *opened_path; |
276 | 0 | if (!file_handle.opened_path) { |
277 | 0 | file_handle.opened_path = zend_string_copy(class_file); |
278 | 0 | } |
279 | 0 | opened_path = zend_string_copy(file_handle.opened_path); |
280 | 0 | ZVAL_NULL(&dummy); |
281 | 0 | zend_op_array *new_op_array = NULL; |
282 | 0 | if (zend_hash_add(&EG(included_files), opened_path, &dummy)) { |
283 | 0 | new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE); |
284 | 0 | } |
285 | 0 | zend_string_release_ex(opened_path, false); |
286 | 0 | if (new_op_array) { |
287 | 0 | uint32_t orig_jit_trace_num = EG(jit_trace_num); |
288 | |
|
289 | 0 | ZVAL_UNDEF(&result); |
290 | 0 | zend_execute(new_op_array, &result); |
291 | 0 | EG(jit_trace_num) = orig_jit_trace_num; |
292 | |
|
293 | 0 | destroy_op_array(new_op_array); |
294 | 0 | efree(new_op_array); |
295 | 0 | zval_ptr_dtor(&result); |
296 | |
|
297 | 0 | ret = zend_hash_exists(EG(class_table), lc_name); |
298 | 0 | } |
299 | 0 | } |
300 | 0 | zend_destroy_file_handle(&file_handle); |
301 | 0 | zend_string_release(class_file); |
302 | 0 | return ret; |
303 | 0 | } /* }}} */ |
304 | | |
305 | | /* {{{ Default autoloader implementation */ |
306 | | PHP_FUNCTION(spl_autoload) |
307 | 0 | { |
308 | 0 | size_t pos_len, pos1_len; |
309 | 0 | char *pos, *pos1; |
310 | 0 | zend_string *class_name, *lc_name, *file_exts = NULL; |
311 | |
|
312 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!", &class_name, &file_exts) == FAILURE) { |
313 | 0 | RETURN_THROWS(); |
314 | 0 | } |
315 | | |
316 | 0 | if (!file_exts) { |
317 | 0 | file_exts = spl_autoload_extensions; |
318 | 0 | } |
319 | |
|
320 | 0 | if (file_exts == NULL) { /* autoload_extensions is not initialized, set to defaults */ |
321 | 0 | pos = SPL_DEFAULT_FILE_EXTENSIONS; |
322 | 0 | pos_len = sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1; |
323 | 0 | } else { |
324 | 0 | pos = ZSTR_VAL(file_exts); |
325 | 0 | pos_len = ZSTR_LEN(file_exts); |
326 | 0 | } |
327 | |
|
328 | 0 | if (ZSTR_VAL(class_name)[0] == '\\') { |
329 | 0 | lc_name = zend_string_alloc(ZSTR_LEN(class_name) - 1, 0); |
330 | 0 | zend_str_tolower_copy(ZSTR_VAL(lc_name), ZSTR_VAL(class_name) + 1, ZSTR_LEN(class_name) - 1); |
331 | 0 | } else { |
332 | 0 | lc_name = zend_string_tolower(class_name); |
333 | 0 | } |
334 | 0 | while (pos && *pos && !EG(exception)) { |
335 | 0 | pos1 = strchr(pos, ','); |
336 | 0 | if (pos1) { |
337 | 0 | pos1_len = (size_t)(pos1 - pos); |
338 | 0 | } else { |
339 | 0 | pos1_len = pos_len; |
340 | 0 | } |
341 | 0 | if (spl_autoload(lc_name, pos, pos1_len)) { |
342 | 0 | break; /* loaded */ |
343 | 0 | } |
344 | 0 | pos = pos1 ? pos1 + 1 : NULL; |
345 | 0 | pos_len = pos1? pos_len - pos1_len - 1 : 0; |
346 | 0 | } |
347 | 0 | zend_string_release(lc_name); |
348 | 0 | } /* }}} */ |
349 | | |
350 | | /* {{{ Register and return default file extensions for spl_autoload */ |
351 | | PHP_FUNCTION(spl_autoload_extensions) |
352 | 0 | { |
353 | 0 | zend_string *file_exts = NULL; |
354 | |
|
355 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &file_exts) == FAILURE) { |
356 | 0 | RETURN_THROWS(); |
357 | 0 | } |
358 | | |
359 | 0 | if (file_exts) { |
360 | 0 | if (spl_autoload_extensions) { |
361 | 0 | zend_string_release_ex(spl_autoload_extensions, 0); |
362 | 0 | } |
363 | 0 | spl_autoload_extensions = zend_string_copy(file_exts); |
364 | 0 | } |
365 | |
|
366 | 0 | if (spl_autoload_extensions == NULL) { |
367 | 0 | RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1); |
368 | 0 | } else { |
369 | 0 | zend_string_addref(spl_autoload_extensions); |
370 | 0 | RETURN_STR(spl_autoload_extensions); |
371 | 0 | } |
372 | 0 | } /* }}} */ |
373 | | |
374 | | /* {{{ Try all registered autoload function to load the requested class */ |
375 | | PHP_FUNCTION(spl_autoload_call) |
376 | 0 | { |
377 | 0 | zend_string *class_name; |
378 | |
|
379 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &class_name) == FAILURE) { |
380 | 0 | RETURN_THROWS(); |
381 | 0 | } |
382 | | |
383 | 0 | zend_string *lc_name = zend_string_tolower(class_name); |
384 | 0 | zend_perform_class_autoload(class_name, lc_name); |
385 | 0 | zend_string_release(lc_name); |
386 | 0 | } /* }}} */ |
387 | | |
388 | | /* {{{ Register given function as autoloader */ |
389 | | PHP_FUNCTION(spl_autoload_register) |
390 | 603 | { |
391 | 603 | bool do_throw = 1; |
392 | 603 | bool prepend = 0; |
393 | 603 | zend_fcall_info fci = {0}; |
394 | 603 | zend_fcall_info_cache fcc; |
395 | | |
396 | 1.80k | ZEND_PARSE_PARAMETERS_START(0, 3) |
397 | 1.80k | Z_PARAM_OPTIONAL |
398 | 2.41k | Z_PARAM_FUNC_OR_NULL(fci, fcc) |
399 | 1.83k | Z_PARAM_BOOL(do_throw) |
400 | 99 | Z_PARAM_BOOL(prepend) |
401 | 603 | ZEND_PARSE_PARAMETERS_END(); |
402 | | |
403 | 598 | if (!do_throw) { |
404 | 0 | php_error_docref(NULL, E_NOTICE, "Argument #2 ($do_throw) has been ignored, " |
405 | 0 | "spl_autoload_register() will always throw"); |
406 | 0 | } |
407 | | |
408 | | /* If first arg is not null */ |
409 | 598 | if (ZEND_FCI_INITIALIZED(fci)) { |
410 | 598 | if (!ZEND_FCC_INITIALIZED(fcc)) { |
411 | | /* Call trampoline has been cleared by zpp. Refetch it, because we want to deal |
412 | | * with it ourselves. It is important that it is not refetched on every call, |
413 | | * because calls may occur from different scopes. */ |
414 | 15 | zend_is_callable_ex(&fci.function_name, NULL, IS_CALLABLE_SUPPRESS_DEPRECATIONS, NULL, &fcc, NULL); |
415 | 15 | } |
416 | | |
417 | 598 | if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION && |
418 | 0 | fcc.function_handler->internal_function.handler == zif_spl_autoload_call) { |
419 | 0 | zend_argument_value_error(1, "must not be the spl_autoload_call() function"); |
420 | 0 | RETURN_THROWS(); |
421 | 0 | } |
422 | 598 | } else { |
423 | 0 | memset(&fcc, 0, sizeof(fcc)); |
424 | 0 | fcc.function_handler = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("spl_autoload")); |
425 | 0 | } |
426 | | |
427 | 598 | zend_autoload_register_class_loader(&fcc, prepend); |
428 | | |
429 | 598 | RETURN_TRUE; |
430 | 598 | } /* }}} */ |
431 | | |
432 | | /* {{{ Unregister given function as autoloader */ |
433 | | PHP_FUNCTION(spl_autoload_unregister) |
434 | 0 | { |
435 | 0 | zend_fcall_info fci; |
436 | 0 | zend_fcall_info_cache fcc; |
437 | |
|
438 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
439 | 0 | Z_PARAM_FUNC_NO_TRAMPOLINE_FREE(fci, fcc) |
440 | 0 | ZEND_PARSE_PARAMETERS_END(); |
441 | | |
442 | 0 | if (zend_string_equals_literal(fcc.function_handler->common.function_name, "spl_autoload_call")) { |
443 | | /* Release trampoline */ |
444 | 0 | zend_release_fcall_info_cache(&fcc); |
445 | 0 | php_error_docref(NULL, E_DEPRECATED, |
446 | 0 | "Using spl_autoload_call() as a callback for spl_autoload_unregister() is deprecated," |
447 | 0 | " to remove all registered autoloaders, call spl_autoload_unregister()" |
448 | 0 | " for all values returned from spl_autoload_functions()"); |
449 | 0 | if (UNEXPECTED(EG(exception))) { |
450 | 0 | RETURN_THROWS(); |
451 | 0 | } |
452 | 0 | zend_autoload_clean_class_loaders(); |
453 | 0 | RETURN_TRUE; |
454 | 0 | } |
455 | | |
456 | 0 | RETVAL_BOOL(zend_autoload_unregister_class_loader(&fcc)); |
457 | | /* Release trampoline */ |
458 | 0 | zend_release_fcall_info_cache(&fcc); |
459 | 0 | } /* }}} */ |
460 | | |
461 | | /* {{{ Return all registered autoloader functions */ |
462 | | PHP_FUNCTION(spl_autoload_functions) |
463 | 0 | { |
464 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
465 | | |
466 | 0 | zend_autoload_fcc_map_to_callable_zval_map(return_value); |
467 | 0 | } /* }}} */ |
468 | | |
469 | | /* {{{ Return hash id for given object */ |
470 | | PHP_FUNCTION(spl_object_hash) |
471 | 678 | { |
472 | 678 | zend_object *obj; |
473 | | |
474 | 2.03k | ZEND_PARSE_PARAMETERS_START(1, 1) |
475 | 2.71k | Z_PARAM_OBJ(obj) |
476 | 678 | ZEND_PARSE_PARAMETERS_END(); |
477 | | |
478 | 676 | RETURN_NEW_STR(php_spl_object_hash(obj)); |
479 | 676 | } |
480 | | /* }}} */ |
481 | | |
482 | | /* {{{ Returns the integer object handle for the given object */ |
483 | | PHP_FUNCTION(spl_object_id) |
484 | 376 | { |
485 | 376 | zend_object *obj; |
486 | | |
487 | 1.12k | ZEND_PARSE_PARAMETERS_START(1, 1) |
488 | 1.50k | Z_PARAM_OBJ(obj) |
489 | 376 | ZEND_PARSE_PARAMETERS_END(); |
490 | | |
491 | 371 | RETURN_LONG((zend_long)obj->handle); |
492 | 371 | } |
493 | | /* }}} */ |
494 | | |
495 | | PHPAPI zend_string *php_spl_object_hash(zend_object *obj) /* {{{*/ |
496 | 676 | { |
497 | 676 | return strpprintf(32, "%016zx0000000000000000", (intptr_t)obj->handle); |
498 | 676 | } |
499 | | /* }}} */ |
500 | | |
501 | | static void spl_build_class_list_string(zval *entry, char **list) /* {{{ */ |
502 | 220 | { |
503 | 220 | char *res; |
504 | | |
505 | 220 | spprintf(&res, 0, "%s, %s", *list, Z_STRVAL_P(entry)); |
506 | 220 | efree(*list); |
507 | 220 | *list = res; |
508 | 220 | } /* }}} */ |
509 | | |
510 | | /* {{{ PHP_MINFO(spl) */ |
511 | | PHP_MINFO_FUNCTION(spl) |
512 | 4 | { |
513 | 4 | zval list, *zv; |
514 | 4 | char *strg; |
515 | | |
516 | 4 | php_info_print_table_start(); |
517 | 4 | php_info_print_table_row(2, "SPL support", "enabled"); |
518 | | |
519 | 4 | array_init(&list); |
520 | 4 | SPL_LIST_CLASSES(&list, false, true) |
521 | 4 | strg = estrdup(""); |
522 | 48 | ZEND_HASH_MAP_FOREACH_VAL(Z_ARRVAL_P(&list), zv) { |
523 | 48 | spl_build_class_list_string(zv, &strg); |
524 | 48 | } ZEND_HASH_FOREACH_END(); |
525 | 4 | zend_array_destroy(Z_ARR(list)); |
526 | 4 | php_info_print_table_row(2, "Interfaces", strg + 2); |
527 | 4 | efree(strg); |
528 | | |
529 | 4 | array_init(&list); |
530 | 4 | SPL_LIST_CLASSES(&list, true, false) |
531 | 4 | strg = estrdup(""); |
532 | 408 | ZEND_HASH_MAP_FOREACH_VAL(Z_ARRVAL_P(&list), zv) { |
533 | 408 | spl_build_class_list_string(zv, &strg); |
534 | 408 | } ZEND_HASH_FOREACH_END(); |
535 | 4 | zend_array_destroy(Z_ARR(list)); |
536 | 4 | php_info_print_table_row(2, "Classes", strg + 2); |
537 | 4 | efree(strg); |
538 | | |
539 | 4 | php_info_print_table_end(); |
540 | 4 | } |
541 | | /* }}} */ |
542 | | |
543 | | /* {{{ PHP_MINIT_FUNCTION(spl) */ |
544 | | PHP_MINIT_FUNCTION(spl) |
545 | 16 | { |
546 | 16 | PHP_MINIT(spl_exceptions)(INIT_FUNC_ARGS_PASSTHRU); |
547 | 16 | PHP_MINIT(spl_iterators)(INIT_FUNC_ARGS_PASSTHRU); |
548 | 16 | PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU); |
549 | 16 | PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU); |
550 | 16 | PHP_MINIT(spl_dllist)(INIT_FUNC_ARGS_PASSTHRU); |
551 | 16 | PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU); |
552 | 16 | PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU); |
553 | 16 | PHP_MINIT(spl_observer)(INIT_FUNC_ARGS_PASSTHRU); |
554 | | |
555 | 16 | return SUCCESS; |
556 | 16 | } |
557 | | /* }}} */ |
558 | | |
559 | | PHP_RINIT_FUNCTION(spl) /* {{{ */ |
560 | 300k | { |
561 | 300k | spl_autoload_extensions = NULL; |
562 | 300k | spl_object_storage_reset_get_hash_depth(); |
563 | 300k | return SUCCESS; |
564 | 300k | } /* }}} */ |
565 | | |
566 | | PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */ |
567 | 300k | { |
568 | 300k | if (spl_autoload_extensions) { |
569 | 0 | zend_string_release_ex(spl_autoload_extensions, 0); |
570 | | spl_autoload_extensions = NULL; |
571 | 0 | } |
572 | 300k | return SUCCESS; |
573 | 300k | } /* }}} */ |
574 | | |
575 | | static const zend_module_dep spl_deps[] = { |
576 | | ZEND_MOD_REQUIRED("json") |
577 | | ZEND_MOD_END |
578 | | }; |
579 | | |
580 | | zend_module_entry spl_module_entry = { |
581 | | STANDARD_MODULE_HEADER_EX, NULL, |
582 | | spl_deps, |
583 | | "SPL", |
584 | | ext_functions, |
585 | | PHP_MINIT(spl), |
586 | | NULL, |
587 | | PHP_RINIT(spl), |
588 | | PHP_RSHUTDOWN(spl), |
589 | | PHP_MINFO(spl), |
590 | | PHP_SPL_VERSION, |
591 | | STANDARD_MODULE_PROPERTIES |
592 | | }; |