xref: /PHP-7.4/ext/spl/spl_functions.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Marcus Boerger <helly@php.net>                              |
16    +----------------------------------------------------------------------+
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 	#include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "php_spl.h"
27 
28 /* {{{ spl_register_interface */
spl_register_interface(zend_class_entry ** ppce,char * class_name,const zend_function_entry * functions)29 void spl_register_interface(zend_class_entry ** ppce, char * class_name, const zend_function_entry * functions)
30 {
31 	zend_class_entry ce;
32 
33 	INIT_CLASS_ENTRY_EX(ce, class_name, strlen(class_name), functions);
34 	*ppce = zend_register_internal_interface(&ce);
35 }
36 /* }}} */
37 
38 /* {{{ spl_register_std_class */
spl_register_std_class(zend_class_entry ** ppce,char * class_name,void * obj_ctor,const zend_function_entry * function_list)39 PHPAPI void spl_register_std_class(zend_class_entry ** ppce, char * class_name, void * obj_ctor, const zend_function_entry * function_list)
40 {
41 	zend_class_entry ce;
42 
43 	INIT_CLASS_ENTRY_EX(ce, class_name, strlen(class_name), function_list);
44 	*ppce = zend_register_internal_class(&ce);
45 
46 	/* entries changed by initialize */
47 	if (obj_ctor) {
48 		(*ppce)->create_object = obj_ctor;
49 	}
50 }
51 /* }}} */
52 
53 /* {{{ spl_register_sub_class */
spl_register_sub_class(zend_class_entry ** ppce,zend_class_entry * parent_ce,char * class_name,void * obj_ctor,const zend_function_entry * function_list)54 PHPAPI void spl_register_sub_class(zend_class_entry ** ppce, zend_class_entry * parent_ce, char * class_name, void *obj_ctor, const zend_function_entry * function_list)
55 {
56 	zend_class_entry ce;
57 
58 	INIT_CLASS_ENTRY_EX(ce, class_name, strlen(class_name), function_list);
59 	*ppce = zend_register_internal_class_ex(&ce, parent_ce);
60 
61 	/* entries changed by initialize */
62 	if (obj_ctor) {
63 		(*ppce)->create_object = obj_ctor;
64 	} else {
65 		(*ppce)->create_object = parent_ce->create_object;
66 	}
67 }
68 /* }}} */
69 
70 /* {{{ spl_register_property */
spl_register_property(zend_class_entry * class_entry,char * prop_name,int prop_name_len,int prop_flags)71 void spl_register_property( zend_class_entry * class_entry, char *prop_name, int prop_name_len, int prop_flags)
72 {
73 	zend_declare_property_null(class_entry, prop_name, prop_name_len, prop_flags);
74 }
75 /* }}} */
76 
77 /* {{{ spl_add_class_name */
spl_add_class_name(zval * list,zend_class_entry * pce,int allow,int ce_flags)78 void spl_add_class_name(zval *list, zend_class_entry *pce, int allow, int ce_flags)
79 {
80 	if (!allow || (allow > 0 && pce->ce_flags & ce_flags) || (allow < 0 && !(pce->ce_flags & ce_flags))) {
81 		zval *tmp;
82 
83 		if ((tmp = zend_hash_find(Z_ARRVAL_P(list), pce->name)) == NULL) {
84 			zval t;
85 			ZVAL_STR_COPY(&t, pce->name);
86 			zend_hash_add(Z_ARRVAL_P(list), pce->name, &t);
87 		}
88 	}
89 }
90 /* }}} */
91 
92 /* {{{ spl_add_interfaces */
spl_add_interfaces(zval * list,zend_class_entry * pce,int allow,int ce_flags)93 void spl_add_interfaces(zval *list, zend_class_entry * pce, int allow, int ce_flags)
94 {
95 	uint32_t num_interfaces;
96 
97 	if (pce->num_interfaces) {
98 		ZEND_ASSERT(pce->ce_flags & ZEND_ACC_LINKED);
99 		for (num_interfaces = 0; num_interfaces < pce->num_interfaces; num_interfaces++) {
100 			spl_add_class_name(list, pce->interfaces[num_interfaces], allow, ce_flags);
101 		}
102 	}
103 }
104 /* }}} */
105 
106 /* {{{ spl_add_traits */
spl_add_traits(zval * list,zend_class_entry * pce,int allow,int ce_flags)107 void spl_add_traits(zval *list, zend_class_entry * pce, int allow, int ce_flags)
108 {
109 	uint32_t num_traits;
110 	zend_class_entry *trait;
111 
112 	for (num_traits = 0; num_traits < pce->num_traits; num_traits++) {
113 		trait = zend_fetch_class_by_name(pce->trait_names[num_traits].name,
114 			pce->trait_names[num_traits].lc_name, ZEND_FETCH_CLASS_TRAIT);
115 		ZEND_ASSERT(trait);
116 		spl_add_class_name(list, trait, allow, ce_flags);
117 	}
118 }
119 /* }}} */
120 
121 
122 /* {{{ spl_add_classes */
spl_add_classes(zend_class_entry * pce,zval * list,int sub,int allow,int ce_flags)123 int spl_add_classes(zend_class_entry *pce, zval *list, int sub, int allow, int ce_flags)
124 {
125 	if (!pce) {
126 		return 0;
127 	}
128 	spl_add_class_name(list, pce, allow, ce_flags);
129 	if (sub) {
130 		spl_add_interfaces(list, pce, allow, ce_flags);
131 		while (pce->parent) {
132 			pce = pce->parent;
133 			spl_add_classes(pce, list, sub, allow, ce_flags);
134 		}
135 	}
136 	return 0;
137 }
138 /* }}} */
139 
spl_gen_private_prop_name(zend_class_entry * ce,char * prop_name,int prop_len)140 zend_string * spl_gen_private_prop_name(zend_class_entry *ce, char *prop_name, int prop_len) /* {{{ */
141 {
142 	return zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), prop_name, prop_len, 0);
143 }
144 /* }}} */
145