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