let the ctor honor the options array too
[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_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_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 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 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 zend_class_entry *php_http_property_proxy_class_entry;
69 zend_function_entry php_http_property_proxy_method_entry[] = {
70 PHP_HTTP_PP_ME(__construct, ZEND_ACC_FINAL|ZEND_ACC_PRIVATE)
71 EMPTY_FUNCTION_ENTRY
72 };
73 static zend_object_handlers php_http_property_proxy_object_handlers;
74
75 zend_object_value php_http_property_proxy_object_new(zend_class_entry *ce TSRMLS_DC)
76 {
77 return php_http_property_proxy_object_new_ex(ce, NULL, NULL TSRMLS_CC);
78 }
79
80 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)
81 {
82 zend_object_value ov;
83 php_http_property_proxy_object_t *o;
84
85 o = ecalloc(1, sizeof(*o));
86 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
87 object_properties_init((zend_object *) o, ce);
88
89 if (ptr) {
90 *ptr = o;
91 }
92 o->proxy = proxy;
93
94 ov.handle = zend_objects_store_put(o, NULL, php_http_property_proxy_object_free, NULL TSRMLS_CC);
95 ov.handlers = &php_http_property_proxy_object_handlers;
96
97 return ov;
98 }
99
100 void php_http_property_proxy_object_free(void *object TSRMLS_DC)
101 {
102 php_http_property_proxy_object_t *o = object;
103
104 if (o->proxy) {
105 php_http_property_proxy_free(&o->proxy);
106 }
107 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
108 efree(o);
109 }
110
111 static zval *php_http_property_proxy_object_get(zval *object TSRMLS_DC)
112 {
113 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
114
115 #if PHP_HTTP_PPDBG
116 fprintf(stderr, "proxy_get: %s\n", Z_STRVAL_P(obj->proxy->member));
117 #endif
118 if (Z_TYPE_P(obj->proxy->object) == IS_OBJECT) {
119 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);
120 } else {
121 zval **data = NULL;
122
123 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)) {
124 Z_ADDREF_PP(data);
125 } else {
126 zval *unset;
127
128 MAKE_STD_ZVAL(unset);
129 ZVAL_NULL(unset);
130 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);
131 }
132
133 return *data;
134 }
135
136 return NULL;
137 }
138
139 static void php_http_property_proxy_object_set(zval **object, zval *value TSRMLS_DC)
140 {
141 php_http_property_proxy_object_t *obj = zend_object_store_get_object(*object TSRMLS_CC);
142 zval *target = obj->proxy->parent ? php_http_property_proxy_object_get(obj->proxy->parent TSRMLS_CC) : obj->proxy->object;
143
144 #if PHP_HTTP_PPDBG
145 fprintf(stderr, "proxy_set: %s\n", Z_STRVAL_P(obj->proxy->member));
146 #endif
147 if (Z_TYPE_P(target) == IS_OBJECT) {
148 zend_update_property(Z_OBJCE_P(target), target, Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member), value TSRMLS_CC);
149 } else {
150 Z_ADDREF_P(value);
151 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);
152 }
153 /* propagate */
154 if (obj->proxy->parent) {
155 php_http_property_proxy_object_set(&obj->proxy->parent, target TSRMLS_CC);
156 zval_ptr_dtor(&target);
157 }
158 }
159
160 static STATUS php_http_property_proxy_object_cast(zval *object, zval *return_value, int type TSRMLS_DC)
161 {
162 zval *old_value, *new_value;
163
164 old_value = php_http_property_proxy_object_get(object TSRMLS_CC);
165 new_value = php_http_ztyp(type, old_value);
166
167 if (old_value != new_value) {
168 zval_ptr_dtor(&old_value);
169 }
170
171 RETVAL_ZVAL(new_value, 0, 0);
172
173 return SUCCESS;
174 }
175
176 static zval *php_http_property_proxy_object_read_dimension(zval *object, zval *offset, int type TSRMLS_DC)
177 {
178 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
179 zval *retval = NULL, *property;
180
181 #if PHP_HTTP_PPDBG
182 zval *ocpy = php_http_ztyp(IS_STRING, offset);
183 fprintf(stderr, "read_dimension: %s.%s (%d)\n", Z_STRVAL_P(obj->proxy->member), Z_STRVAL_P(ocpy), type);
184 zval_ptr_dtor(&ocpy);
185 #endif
186
187 property = php_http_property_proxy_object_get(object TSRMLS_CC);
188 if (type != BP_VAR_R) {
189 Z_ADDREF_P(property);
190 retval = php_http_property_proxy_init(NULL, property, offset, obj->proxy->myself TSRMLS_CC)->myself;
191 } else if (Z_TYPE_P(property) == IS_ARRAY) {
192 zval **data = NULL;
193
194 if (Z_TYPE_P(offset) == IS_LONG) {
195 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(property), Z_LVAL_P(offset), (void *) &data)) {
196 retval = *data;
197 }
198 } else {
199 offset = php_http_ztyp(IS_STRING, offset);
200 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(property), Z_STRVAL_P(offset), Z_STRLEN_P(offset), (void *) &data)) {
201 retval = *data;
202 }
203 zval_ptr_dtor(&offset);
204 }
205
206 if (data) {
207 Z_ADDREF_PP(data);
208 }
209 }
210 zval_ptr_dtor(&property);
211
212 return retval;
213 }
214
215 static void php_http_property_proxy_object_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC)
216 {
217 zval *property = php_http_property_proxy_object_get(object TSRMLS_CC);
218
219 #if PHP_HTTP_PPDBG
220 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
221 zval *ocpy = offset ? php_http_ztyp(IS_STRING, offset) : offset;
222 fprintf(stderr, "write_dimension: %s.%s (%d)\n", Z_STRVAL_P(obj->proxy->member), ocpy?Z_STRVAL_P(ocpy):"", Z_TYPE_P(property));
223 if(ocpy)zval_ptr_dtor(&ocpy);
224 #endif
225 switch (Z_TYPE_P(property)) {
226 case IS_NULL:
227 array_init(property);
228 /* no break */
229 case IS_ARRAY:
230 Z_ADDREF_P(value);
231 if (!offset) {
232 add_next_index_zval(property, value);
233 } else if (Z_TYPE_P(offset) == IS_LONG) {
234 add_index_zval(property, Z_LVAL_P(offset), value);
235 } else {
236 offset = php_http_ztyp(IS_STRING, offset);
237 add_assoc_zval_ex(property, Z_STRVAL_P(offset), Z_STRLEN_P(offset) + 1, value);
238 zval_ptr_dtor(&offset);
239 }
240 php_http_property_proxy_object_set(&object, property TSRMLS_CC);
241 zval_ptr_dtor(&property);
242 break;
243
244 default:
245 zval_ptr_dtor(&property);
246 break;
247 }
248 }
249
250 PHP_METHOD(HttpPropertyProxy, __construct)
251 {
252 }
253
254 PHP_MINIT_FUNCTION(http_property_proxy)
255 {
256 PHP_HTTP_REGISTER_CLASS(http\\Object, PropertyProxy, http_property_proxy, NULL, ZEND_ACC_FINAL);
257 php_http_property_proxy_class_entry->create_object = php_http_property_proxy_object_new;
258 memcpy(&php_http_property_proxy_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
259 php_http_property_proxy_object_handlers.set = php_http_property_proxy_object_set;
260 php_http_property_proxy_object_handlers.get = php_http_property_proxy_object_get;
261 php_http_property_proxy_object_handlers.cast_object = php_http_property_proxy_object_cast;
262 php_http_property_proxy_object_handlers.read_dimension = php_http_property_proxy_object_read_dimension;
263 php_http_property_proxy_object_handlers.write_dimension = php_http_property_proxy_object_write_dimension;
264
265 return SUCCESS;
266 }
267
268
269 /*
270 * Local variables:
271 * tab-width: 4
272 * c-basic-offset: 4
273 * End:
274 * vim600: noet sw=4 ts=4 fdm=marker
275 * vim<600: noet sw=4 ts=4
276 */
277