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