xref: /PHP-7.0/ext/spl/spl_functions.c (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 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 /* $Id$ */
20 
21 #ifdef HAVE_CONFIG_H
22 	#include "config.h"
23 #endif
24 
25 #include "php.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "php_spl.h"
29 
30 /* {{{ spl_register_interface */
spl_register_interface(zend_class_entry ** ppce,char * class_name,const zend_function_entry * functions)31 void spl_register_interface(zend_class_entry ** ppce, char * class_name, const zend_function_entry * functions)
32 {
33 	zend_class_entry ce;
34 
35 	INIT_CLASS_ENTRY_EX(ce, class_name, strlen(class_name), functions);
36 	*ppce = zend_register_internal_interface(&ce);
37 }
38 /* }}} */
39 
40 /* {{{ spl_register_std_class */
spl_register_std_class(zend_class_entry ** ppce,char * class_name,void * obj_ctor,const zend_function_entry * function_list)41 PHPAPI void spl_register_std_class(zend_class_entry ** ppce, char * class_name, void * obj_ctor, const zend_function_entry * function_list)
42 {
43 	zend_class_entry ce;
44 
45 	INIT_CLASS_ENTRY_EX(ce, class_name, strlen(class_name), function_list);
46 	*ppce = zend_register_internal_class(&ce);
47 
48 	/* entries changed by initialize */
49 	if (obj_ctor) {
50 		(*ppce)->create_object = obj_ctor;
51 	}
52 }
53 /* }}} */
54 
55 /* {{{ 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)56 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)
57 {
58 	zend_class_entry ce;
59 
60 	INIT_CLASS_ENTRY_EX(ce, class_name, strlen(class_name), function_list);
61 	*ppce = zend_register_internal_class_ex(&ce, parent_ce);
62 
63 	/* entries changed by initialize */
64 	if (obj_ctor) {
65 		(*ppce)->create_object = obj_ctor;
66 	} else {
67 		(*ppce)->create_object = parent_ce->create_object;
68 	}
69 }
70 /* }}} */
71 
72 /* {{{ spl_register_property */
spl_register_property(zend_class_entry * class_entry,char * prop_name,int prop_name_len,int prop_flags)73 void spl_register_property( zend_class_entry * class_entry, char *prop_name, int prop_name_len, int prop_flags)
74 {
75 	zend_declare_property_null(class_entry, prop_name, prop_name_len, prop_flags);
76 }
77 /* }}} */
78 
79 /* {{{ spl_add_class_name */
spl_add_class_name(zval * list,zend_class_entry * pce,int allow,int ce_flags)80 void spl_add_class_name(zval *list, zend_class_entry *pce, int allow, int ce_flags)
81 {
82 	if (!allow || (allow > 0 && pce->ce_flags & ce_flags) || (allow < 0 && !(pce->ce_flags & ce_flags))) {
83 		zval *tmp;
84 
85 		if ((tmp = zend_hash_find(Z_ARRVAL_P(list), pce->name)) == NULL) {
86 			zval t;
87 			ZVAL_STR_COPY(&t, pce->name);
88 			zend_hash_add(Z_ARRVAL_P(list), pce->name, &t);
89 		}
90 	}
91 }
92 /* }}} */
93 
94 /* {{{ spl_add_interfaces */
spl_add_interfaces(zval * list,zend_class_entry * pce,int allow,int ce_flags)95 void spl_add_interfaces(zval *list, zend_class_entry * pce, int allow, int ce_flags)
96 {
97 	uint32_t num_interfaces;
98 
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 /* {{{ spl_add_traits */
spl_add_traits(zval * list,zend_class_entry * pce,int allow,int ce_flags)106 void spl_add_traits(zval *list, zend_class_entry * pce, int allow, int ce_flags)
107 {
108 	uint32_t num_traits;
109 
110 	for (num_traits = 0; num_traits < pce->num_traits; num_traits++) {
111 		spl_add_class_name(list, pce->traits[num_traits], allow, ce_flags);
112 	}
113 }
114 /* }}} */
115 
116 
117 /* {{{ spl_add_classes */
spl_add_classes(zend_class_entry * pce,zval * list,int sub,int allow,int ce_flags)118 int spl_add_classes(zend_class_entry *pce, zval *list, int sub, int allow, int ce_flags)
119 {
120 	if (!pce) {
121 		return 0;
122 	}
123 	spl_add_class_name(list, pce, allow, ce_flags);
124 	if (sub) {
125 		spl_add_interfaces(list, pce, allow, ce_flags);
126 		while (pce->parent) {
127 			pce = pce->parent;
128 			spl_add_classes(pce, list, sub, allow, ce_flags);
129 		}
130 	}
131 	return 0;
132 }
133 /* }}} */
134 
spl_gen_private_prop_name(zend_class_entry * ce,char * prop_name,int prop_len)135 zend_string * spl_gen_private_prop_name(zend_class_entry *ce, char *prop_name, int prop_len) /* {{{ */
136 {
137 	return zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), prop_name, prop_len, 0);
138 }
139 /* }}} */
140 
141 /*
142  * Local variables:
143  * tab-width: 4
144  * c-basic-offset: 4
145  * End:
146  * vim600: fdm=marker
147  * vim: noet sw=4 ts=4
148  */
149