xref: /PHP-8.0/ext/spl/spl_functions.c (revision 7d6a0ba8)
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    | http://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_ini.h"
23 #include "ext/standard/info.h"
24 #include "php_spl.h"
25 
26 /* {{{ spl_register_interface */
spl_register_interface(zend_class_entry ** ppce,char * class_name,const zend_function_entry * functions)27 void spl_register_interface(zend_class_entry ** ppce, char * class_name, const zend_function_entry * functions)
28 {
29 	zend_class_entry ce;
30 
31 	INIT_CLASS_ENTRY_EX(ce, class_name, strlen(class_name), functions);
32 	*ppce = zend_register_internal_interface(&ce);
33 }
34 /* }}} */
35 
36 /* {{{ spl_register_std_class */
spl_register_std_class(zend_class_entry ** ppce,char * class_name,void * obj_ctor,const zend_function_entry * function_list)37 PHPAPI void spl_register_std_class(zend_class_entry ** ppce, char * class_name, void * obj_ctor, const zend_function_entry * function_list)
38 {
39 	zend_class_entry ce;
40 
41 	INIT_CLASS_ENTRY_EX(ce, class_name, strlen(class_name), function_list);
42 	*ppce = zend_register_internal_class(&ce);
43 
44 	/* entries changed by initialize */
45 	if (obj_ctor) {
46 		(*ppce)->create_object = obj_ctor;
47 	}
48 }
49 /* }}} */
50 
51 /* {{{ 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)52 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)
53 {
54 	zend_class_entry ce;
55 
56 	INIT_CLASS_ENTRY_EX(ce, class_name, strlen(class_name), function_list);
57 	*ppce = zend_register_internal_class_ex(&ce, parent_ce);
58 
59 	/* entries changed by initialize */
60 	if (obj_ctor) {
61 		(*ppce)->create_object = obj_ctor;
62 	} else {
63 		(*ppce)->create_object = parent_ce->create_object;
64 	}
65 }
66 /* }}} */
67 
68 /* {{{ spl_register_property */
spl_register_property(zend_class_entry * class_entry,char * prop_name,int prop_name_len,int prop_flags)69 void spl_register_property( zend_class_entry * class_entry, char *prop_name, int prop_name_len, int prop_flags)
70 {
71 	zend_declare_property_null(class_entry, prop_name, prop_name_len, prop_flags);
72 }
73 /* }}} */
74 
75 /* {{{ spl_add_class_name */
spl_add_class_name(zval * list,zend_class_entry * pce,int allow,int ce_flags)76 void spl_add_class_name(zval *list, zend_class_entry *pce, int allow, int ce_flags)
77 {
78 	if (!allow || (allow > 0 && (pce->ce_flags & ce_flags)) || (allow < 0 && !(pce->ce_flags & ce_flags))) {
79 		zval *tmp;
80 
81 		if ((tmp = zend_hash_find(Z_ARRVAL_P(list), pce->name)) == NULL) {
82 			zval t;
83 			ZVAL_STR_COPY(&t, pce->name);
84 			zend_hash_add(Z_ARRVAL_P(list), pce->name, &t);
85 		}
86 	}
87 }
88 /* }}} */
89 
90 /* {{{ spl_add_interfaces */
spl_add_interfaces(zval * list,zend_class_entry * pce,int allow,int ce_flags)91 void spl_add_interfaces(zval *list, zend_class_entry * pce, int allow, int ce_flags)
92 {
93 	uint32_t num_interfaces;
94 
95 	if (pce->num_interfaces) {
96 		ZEND_ASSERT(pce->ce_flags & ZEND_ACC_LINKED);
97 		for (num_interfaces = 0; num_interfaces < pce->num_interfaces; num_interfaces++) {
98 			spl_add_class_name(list, pce->interfaces[num_interfaces], allow, ce_flags);
99 		}
100 	}
101 }
102 /* }}} */
103 
104 /* {{{ spl_add_traits */
spl_add_traits(zval * list,zend_class_entry * pce,int allow,int ce_flags)105 void spl_add_traits(zval *list, zend_class_entry * pce, int allow, int ce_flags)
106 {
107 	uint32_t num_traits;
108 	zend_class_entry *trait;
109 
110 	for (num_traits = 0; num_traits < pce->num_traits; num_traits++) {
111 		trait = zend_fetch_class_by_name(pce->trait_names[num_traits].name,
112 			pce->trait_names[num_traits].lc_name, ZEND_FETCH_CLASS_TRAIT);
113 		ZEND_ASSERT(trait);
114 		spl_add_class_name(list, trait, allow, ce_flags);
115 	}
116 }
117 /* }}} */
118 
119 
120 /* {{{ spl_add_classes */
spl_add_classes(zend_class_entry * pce,zval * list,int sub,int allow,int ce_flags)121 int spl_add_classes(zend_class_entry *pce, zval *list, int sub, int allow, int ce_flags)
122 {
123 	if (!pce) {
124 		return 0;
125 	}
126 	spl_add_class_name(list, pce, allow, ce_flags);
127 	if (sub) {
128 		spl_add_interfaces(list, pce, allow, ce_flags);
129 		while (pce->parent) {
130 			pce = pce->parent;
131 			spl_add_classes(pce, list, sub, allow, ce_flags);
132 		}
133 	}
134 	return 0;
135 }
136 /* }}} */
137 
spl_gen_private_prop_name(zend_class_entry * ce,char * prop_name,int prop_len)138 zend_string * spl_gen_private_prop_name(zend_class_entry *ce, char *prop_name, int prop_len) /* {{{ */
139 {
140 	return zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), prop_name, prop_len, 0);
141 }
142 /* }}} */
143