-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
yaf_registry.c
245 lines (196 loc) · 6.9 KB
/
yaf_registry.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
+----------------------------------------------------------------------+
| Yet Another Framework |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinchen Hui <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_yaf.h"
#include "Zend/zend_interfaces.h" /* for zend_class_serialize_deny */
#include "yaf_namespace.h"
#if PHP_MAJOR_VERSION > 7
#include "yaf_registry_arginfo.h"
#else
#include "yaf_registry_legacy_arginfo.h"
#endif
#include "yaf_registry.h"
zend_class_entry *yaf_registry_ce;
static zend_object_handlers yaf_registry_obj_handlers;
static HashTable *yaf_registry_get_properties(yaf_object *obj) /* {{{ */ {
zval rv;
HashTable *ht;
yaf_registry_object *registry = php_yaf_registry_fetch_object(yaf_strip_obj(obj));
if (!registry->properties) {
ALLOC_HASHTABLE(registry->properties);
zend_hash_init(registry->properties, 8, NULL, ZVAL_PTR_DTOR, 0);
YAF_ALLOW_VIOLATION(registry->properties);
}
ht = registry->properties;
ZVAL_ARR(&rv, zend_array_dup(®istry->entries));
zend_hash_str_update(ht, "entries:protected", sizeof("entries:protected") - 1, &rv);
return ht;
}
/* }}} */
static void yaf_registry_object_free(zend_object *object) /* {{{ */ {
yaf_registry_object *registry = php_yaf_registry_fetch_object(object);
ZEND_ASSERT(Z_OBJ(YAF_G(registry)) == object);
zend_hash_destroy(®istry->entries);
if (registry->properties) {
if (GC_DELREF(registry->properties) == 0) {
GC_REMOVE_FROM_BUFFER(registry->properties);
zend_array_destroy(registry->properties);
}
}
zend_object_std_dtor(object);
}
/* }}} */
yaf_registry_object *yaf_registry_instance() /* {{{ */ {
yaf_registry_object *registry;
if (EXPECTED(Z_TYPE(YAF_G(registry)) == IS_OBJECT)) {
return Z_YAFREGISTRYOBJ(YAF_G(registry));
}
registry = emalloc(sizeof(yaf_registry_object) + zend_object_properties_size(yaf_registry_ce));
zend_object_std_init(®istry->std, yaf_registry_ce);
registry->std.handlers = &yaf_registry_obj_handlers;
zend_hash_init(®istry->entries, 8, NULL, ZVAL_PTR_DTOR, 0);
registry->properties = NULL;
ZVAL_OBJ(&YAF_G(registry), ®istry->std);
return Z_YAFREGISTRYOBJ(YAF_G(registry));
}
/* }}} */
static zval *yaf_registry_find(yaf_registry_object *registry, zend_string *name) /* {{{ */ {
return zend_hash_find(®istry->entries, name);
}
/* }}} */
static int yaf_registry_has(yaf_registry_object *registry, zend_string *name) /* {{{ */ {
return zend_hash_exists(®istry->entries, name);
}
/* }}} */
static zval *yaf_registry_update(yaf_registry_object *registry, zend_string *name, zval *value) /* {{{ */ {
Z_TRY_ADDREF_P(value);
return zend_hash_update(®istry->entries, name, value);
}
/* }}} */
static void yaf_registry_del(yaf_registry_object *registry, zend_string *name) /* {{{ */ {
zend_hash_del(®istry->entries, name);
}
/* }}} */
/** {{{ proto private Yaf_Registry::__construct(void)
*/
PHP_METHOD(yaf_registry, __construct) {
}
/* }}} */
/** {{{ proto public static Yaf_Registry::get($name)
*/
PHP_METHOD(yaf_registry, get) {
zval *value;
zend_string *name;
yaf_registry_object *registry = yaf_registry_instance();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
return;
}
if ((value = yaf_registry_find(registry, name))) {
RETURN_ZVAL(value, 1, 0);
}
RETURN_NULL();
}
/* }}} */
/** {{{ proto public static Yaf_Registry::set($name, $value)
*/
PHP_METHOD(yaf_registry, set) {
zval *value;
zend_string *name;
yaf_registry_object *registry = yaf_registry_instance();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &name, &value) == FAILURE) {
return;
}
if (yaf_registry_update(registry, name, value)) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public static Yaf_Registry::del($name)
*/
PHP_METHOD(yaf_registry, del) {
zend_string *name;
yaf_registry_object *registry = yaf_registry_instance();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
return;
}
yaf_registry_del(registry, name);
RETURN_TRUE;
}
/* }}} */
/** {{{ proto public Yaf_Registry::has($name)
*/
PHP_METHOD(yaf_registry, has) {
zend_string *name;
yaf_registry_object *registry = yaf_registry_instance();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
return;
}
RETURN_BOOL(yaf_registry_has(registry, name));
}
/* }}} */
/** {{{ proto public Yaf_Registry::getInstance(void)
*/
PHP_METHOD(yaf_registry, getInstance) {
ZVAL_OBJ(return_value, &(yaf_registry_instance())->std);
Z_ADDREF_P(return_value);
return;
}
/* }}} */
/** {{{ yaf_registry_methods
*/
zend_function_entry yaf_registry_methods[] = {
PHP_ME(yaf_registry, __construct, arginfo_class_Yaf_Registry___construct, ZEND_ACC_CTOR|ZEND_ACC_PRIVATE)
PHP_ME(yaf_registry, get, arginfo_class_Yaf_Registry_get, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(yaf_registry, has, arginfo_class_Yaf_Registry_has, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(yaf_registry, set, arginfo_class_Yaf_Registry_set, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(yaf_registry, del, arginfo_class_Yaf_Registry_del, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
{NULL, NULL, NULL}
};
/* }}} */
/** {{{ YAF_STARTUP_FUNCTION
*/
YAF_STARTUP_FUNCTION(registry) {
zend_class_entry ce;
YAF_INIT_CLASS_ENTRY(ce, "Yaf_Registry", "Yaf\\Registry", yaf_registry_methods);
yaf_registry_ce = zend_register_internal_class_ex(&ce, NULL);
#if PHP_VERSION_ID < 80100
yaf_registry_ce->ce_flags |= ZEND_ACC_FINAL;
yaf_registry_ce->serialize = zend_class_serialize_deny;
yaf_registry_ce->unserialize = zend_class_unserialize_deny;
#else
yaf_registry_ce->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NOT_SERIALIZABLE;
#endif
memcpy(&yaf_registry_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
yaf_registry_obj_handlers.offset = XtOffsetOf(yaf_registry_object, std);
yaf_registry_obj_handlers.clone_obj = NULL;
yaf_registry_obj_handlers.get_gc = yaf_fake_get_gc;
yaf_registry_obj_handlers.free_obj = yaf_registry_object_free;
yaf_registry_obj_handlers.get_properties = yaf_registry_get_properties;
return SUCCESS;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/