xref: /PHP-8.3/ext/spl/spl_functions.c (revision cfc38a60)
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_spl.h"
23 
24 /* {{{ spl_add_class_name */
spl_add_class_name(zval * list,zend_class_entry * pce,int allow,int ce_flags)25 void spl_add_class_name(zval *list, zend_class_entry *pce, int allow, int ce_flags)
26 {
27 	if (!allow || (allow > 0 && (pce->ce_flags & ce_flags)) || (allow < 0 && !(pce->ce_flags & ce_flags))) {
28 		zval *tmp;
29 
30 		if ((tmp = zend_hash_find(Z_ARRVAL_P(list), pce->name)) == NULL) {
31 			zval t;
32 			ZVAL_STR_COPY(&t, pce->name);
33 			zend_hash_add(Z_ARRVAL_P(list), pce->name, &t);
34 		}
35 	}
36 }
37 /* }}} */
38 
39 /* {{{ spl_add_interfaces */
spl_add_interfaces(zval * list,zend_class_entry * pce,int allow,int ce_flags)40 void spl_add_interfaces(zval *list, zend_class_entry * pce, int allow, int ce_flags)
41 {
42 	if (pce->num_interfaces) {
43 		ZEND_ASSERT(pce->ce_flags & ZEND_ACC_LINKED);
44 		for (uint32_t num_interfaces = 0; num_interfaces < pce->num_interfaces; num_interfaces++) {
45 			spl_add_class_name(list, pce->interfaces[num_interfaces], allow, ce_flags);
46 		}
47 	}
48 }
49 /* }}} */
50 
51 /* {{{ spl_add_traits */
spl_add_traits(zval * list,zend_class_entry * pce,int allow,int ce_flags)52 void spl_add_traits(zval *list, zend_class_entry * pce, int allow, int ce_flags)
53 {
54 	zend_class_entry *trait;
55 
56 	for (uint32_t num_traits = 0; num_traits < pce->num_traits; num_traits++) {
57 		trait = zend_fetch_class_by_name(pce->trait_names[num_traits].name,
58 			pce->trait_names[num_traits].lc_name, ZEND_FETCH_CLASS_TRAIT);
59 		ZEND_ASSERT(trait);
60 		spl_add_class_name(list, trait, allow, ce_flags);
61 	}
62 }
63 /* }}} */
64 
65 
66 /* {{{ spl_add_classes */
spl_add_classes(zend_class_entry * pce,zval * list,bool sub,int allow,int ce_flags)67 void spl_add_classes(zend_class_entry *pce, zval *list, bool sub, int allow, int ce_flags)
68 {
69 	ZEND_ASSERT(pce);
70 	spl_add_class_name(list, pce, allow, ce_flags);
71 	if (sub) {
72 		spl_add_interfaces(list, pce, allow, ce_flags);
73 		while (pce->parent) {
74 			pce = pce->parent;
75 			spl_add_classes(pce, list, sub, allow, ce_flags);
76 		}
77 	}
78 }
79 /* }}} */
80 
spl_gen_private_prop_name(zend_class_entry * ce,char * prop_name,size_t prop_len)81 zend_string * spl_gen_private_prop_name(zend_class_entry *ce, char *prop_name, size_t prop_len) /* {{{ */
82 {
83 	return zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), prop_name, prop_len, 0);
84 }
85 /* }}} */
86