use php_http_array_hashkey_string* where applicable
[m6w6/ext-http] / php_http_property_proxy.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #ifndef PHP_HTTP_PPDBG
16 # define PHP_HTTP_PPDBG 0
17 #endif
18
19 PHP_HTTP_API php_http_property_proxy_t *php_http_property_proxy_init(php_http_property_proxy_t *proxy, zval *object, zval *member, zval *parent TSRMLS_DC)
20 {
21 if (!proxy) {
22 proxy = emalloc(sizeof(*proxy));
23 }
24 memset(proxy, 0, sizeof(*proxy));
25
26 MAKE_STD_ZVAL(proxy->myself);
27 ZVAL_OBJVAL(proxy->myself, php_http_property_proxy_object_new_ex(php_http_property_proxy_get_class_entry(), proxy, NULL TSRMLS_CC), 0);
28 Z_ADDREF_P(object);
29 proxy->object = object;
30 proxy->member = php_http_ztyp(IS_STRING, member);
31 if (parent) {
32 Z_ADDREF_P(parent);
33 proxy->parent = parent;
34 }
35
36 #if PHP_HTTP_PPDBG
37 fprintf(stderr, "proxy_init: %s\n", Z_STRVAL_P(proxy->member));
38 #endif
39 return proxy;
40 }
41
42 PHP_HTTP_API void php_http_property_proxy_dtor(php_http_property_proxy_t *proxy)
43 {
44 zval_ptr_dtor(&proxy->object);
45 zval_ptr_dtor(&proxy->member);
46 zval_ptr_dtor(&proxy->myself);
47 if (proxy->parent) {
48 zval_ptr_dtor(&proxy->parent);
49 }
50 }
51
52 PHP_HTTP_API void php_http_property_proxy_free(php_http_property_proxy_t **proxy)
53 {
54 if (*proxy) {
55 php_http_property_proxy_dtor(*proxy);
56 efree(*proxy);
57 *proxy = NULL;
58 }
59 }
60
61
62 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpPropertyProxy, method, 0, req_args)
63 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpPropertyProxy, method, 0)
64 #define PHP_HTTP_PP_ME(method, visibility) PHP_ME(HttpPropertyProxy, method, PHP_HTTP_ARGS(HttpPropertyProxy, method), visibility)
65
66 PHP_HTTP_EMPTY_ARGS(__construct);
67
68 static zend_class_entry *php_http_property_proxy_class_entry;
69
70 zend_class_entry *php_http_property_proxy_get_class_entry(void)
71 {
72 return php_http_property_proxy_class_entry;
73 }
74
75 static zend_function_entry php_http_property_proxy_method_entry[] = {
76 PHP_HTTP_PP_ME(__construct, ZEND_ACC_FINAL|ZEND_ACC_PRIVATE)
77 EMPTY_FUNCTION_ENTRY
78 };
79
80 static zend_object_handlers php_http_property_proxy_object_handlers;
81
82 zend_object_value php_http_property_proxy_object_new(zend_class_entry *ce TSRMLS_DC)
83 {
84 return php_http_property_proxy_object_new_ex(ce, NULL, NULL TSRMLS_CC);
85 }
86
87 zend_object_value php_http_property_proxy_object_new_ex(zend_class_entry *ce, php_http_property_proxy_t *proxy, php_http_property_proxy_object_t **ptr TSRMLS_DC)
88 {
89 zend_object_value ov;
90 php_http_property_proxy_object_t *o;
91
92 o = ecalloc(1, sizeof(*o));
93 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
94 object_properties_init((zend_object *) o, ce);
95
96 if (ptr) {
97 *ptr = o;
98 }
99 o->proxy = proxy;
100
101 ov.handle = zend_objects_store_put(o, NULL, php_http_property_proxy_object_free, NULL TSRMLS_CC);
102 ov.handlers = &php_http_property_proxy_object_handlers;
103
104 return ov;
105 }
106
107 void php_http_property_proxy_object_free(void *object TSRMLS_DC)
108 {
109 php_http_property_proxy_object_t *o = object;
110
111 if (o->proxy) {
112 php_http_property_proxy_free(&o->proxy);
113 }
114 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
115 efree(o);
116 }
117
118 static zval *php_http_property_proxy_object_get(zval *object TSRMLS_DC)
119 {
120 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
121
122 #if PHP_HTTP_PPDBG
123 fprintf(stderr, "proxy_get: %s\n", Z_STRVAL_P(obj->proxy->member));
124 #endif
125 if (Z_TYPE_P(obj->proxy->object) == IS_OBJECT) {
126 return zend_read_property(Z_OBJCE_P(obj->proxy->object), obj->proxy->object, Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member), 0 TSRMLS_CC);
127 } else {
128 zval **data = NULL;
129
130 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(obj->proxy->object), Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member)+1, (void *) &data)) {
131 Z_ADDREF_PP(data);
132 } else {
133 zval *unset;
134
135 MAKE_STD_ZVAL(unset);
136 ZVAL_NULL(unset);
137 zend_symtable_update(Z_ARRVAL_P(obj->proxy->object), Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member)+1, (void *) &unset, sizeof(zval *), (void *) &data);
138 }
139
140 return *data;
141 }
142
143 return NULL;
144 }
145
146 static void php_http_property_proxy_object_set(zval **object, zval *value TSRMLS_DC)
147 {
148 php_http_property_proxy_object_t *obj = zend_object_store_get_object(*object TSRMLS_CC);
149 zval *target = obj->proxy->parent ? php_http_property_proxy_object_get(obj->proxy->parent TSRMLS_CC) : obj->proxy->object;
150
151 #if PHP_HTTP_PPDBG
152 fprintf(stderr, "proxy_set: %s\n", Z_STRVAL_P(obj->proxy->member));
153 #endif
154 if (Z_TYPE_P(target) == IS_OBJECT) {
155 zend_update_property(Z_OBJCE_P(target), target, Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member), value TSRMLS_CC);
156 } else {
157 Z_ADDREF_P(value);
158 zend_symtable_update(Z_ARRVAL_P(target), Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member)+1, (void *) &value, sizeof(zval *), NULL);
159 }
160 /* propagate */
161 if (obj->proxy->parent) {
162 php_http_property_proxy_object_set(&obj->proxy->parent, target TSRMLS_CC);
163 zval_ptr_dtor(&target);
164 }
165 }
166
167 static STATUS php_http_property_proxy_object_cast(zval *object, zval *return_value, int type TSRMLS_DC)
168 {
169 zval *old_value, *new_value;
170
171 old_value = php_http_property_proxy_object_get(object TSRMLS_CC);
172 new_value = php_http_ztyp(type, old_value);
173
174 if (old_value != new_value) {
175 zval_ptr_dtor(&old_value);
176 }
177
178 RETVAL_ZVAL(new_value, 0, 0);
179
180 return SUCCESS;
181 }
182
183 static zval *php_http_property_proxy_object_read_dimension(zval *object, zval *offset, int type TSRMLS_DC)
184 {
185 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
186 zval *retval = NULL, *property;
187
188 #if PHP_HTTP_PPDBG
189 zval *ocpy = php_http_ztyp(IS_STRING, offset);
190 fprintf(stderr, "read_dimension: %s.%s (%d)\n", Z_STRVAL_P(obj->proxy->member), Z_STRVAL_P(ocpy), type);
191 zval_ptr_dtor(&ocpy);
192 #endif
193
194 property = php_http_property_proxy_object_get(object TSRMLS_CC);
195 if (type != BP_VAR_R) {
196 Z_ADDREF_P(property);
197 retval = php_http_property_proxy_init(NULL, property, offset, obj->proxy->myself TSRMLS_CC)->myself;
198 } else if (Z_TYPE_P(property) == IS_ARRAY) {
199 zval **data = NULL;
200
201 if (Z_TYPE_P(offset) == IS_LONG) {
202 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(property), Z_LVAL_P(offset), (void *) &data)) {
203 retval = *data;
204 }
205 } else {
206 offset = php_http_ztyp(IS_STRING, offset);
207 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(property), Z_STRVAL_P(offset), Z_STRLEN_P(offset), (void *) &data)) {
208 retval = *data;
209 }
210 zval_ptr_dtor(&offset);
211 }
212
213 if (data) {
214 Z_ADDREF_PP(data);
215 }
216 }
217 zval_ptr_dtor(&property);
218
219 return retval;
220 }
221
222 static void php_http_property_proxy_object_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC)
223 {
224 zval *property = php_http_property_proxy_object_get(object TSRMLS_CC);
225
226 #if PHP_HTTP_PPDBG
227 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
228 zval *ocpy = offset ? php_http_ztyp(IS_STRING, offset) : offset;
229 fprintf(stderr, "write_dimension: %s.%s (%d)\n", Z_STRVAL_P(obj->proxy->member), ocpy?Z_STRVAL_P(ocpy):"", Z_TYPE_P(property));
230 if(ocpy)zval_ptr_dtor(&ocpy);
231 #endif
232 switch (Z_TYPE_P(property)) {
233 case IS_NULL:
234 array_init(property);
235 /* no break */
236 case IS_ARRAY:
237 Z_ADDREF_P(value);
238 if (!offset) {
239 add_next_index_zval(property, value);
240 } else if (Z_TYPE_P(offset) == IS_LONG) {
241 add_index_zval(property, Z_LVAL_P(offset), value);
242 } else {
243 offset = php_http_ztyp(IS_STRING, offset);
244 add_assoc_zval_ex(property, Z_STRVAL_P(offset), Z_STRLEN_P(offset) + 1, value);
245 zval_ptr_dtor(&offset);
246 }
247 php_http_property_proxy_object_set(&object, property TSRMLS_CC);
248 zval_ptr_dtor(&property);
249 break;
250
251 default:
252 zval_ptr_dtor(&property);
253 break;
254 }
255 }
256
257 PHP_METHOD(HttpPropertyProxy, __construct)
258 {
259 }
260
261 PHP_MINIT_FUNCTION(http_property_proxy)
262 {
263 PHP_HTTP_REGISTER_CLASS(http\\Object, PropertyProxy, http_property_proxy, NULL, ZEND_ACC_FINAL);
264 php_http_property_proxy_class_entry->create_object = php_http_property_proxy_object_new;
265 memcpy(&php_http_property_proxy_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
266 php_http_property_proxy_object_handlers.set = php_http_property_proxy_object_set;
267 php_http_property_proxy_object_handlers.get = php_http_property_proxy_object_get;
268 php_http_property_proxy_object_handlers.cast_object = php_http_property_proxy_object_cast;
269 php_http_property_proxy_object_handlers.read_dimension = php_http_property_proxy_object_read_dimension;
270 php_http_property_proxy_object_handlers.write_dimension = php_http_property_proxy_object_write_dimension;
271
272 return SUCCESS;
273 }
274
275
276 /*
277 * Local variables:
278 * tab-width: 4
279 * c-basic-offset: 4
280 * End:
281 * vim600: noet sw=4 ts=4 fdm=marker
282 * vim<600: noet sw=4 ts=4
283 */
284