build and file maintenance
[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.h"
14
15 php_http_property_proxy_t *php_http_property_proxy_init(php_http_property_proxy_t *proxy, zval *object, zval *member TSRMLS_DC)
16 {
17 if (!proxy) {
18 proxy = emalloc(sizeof(*proxy));
19 }
20 memset(proxy, 0, sizeof(*proxy));
21
22 MAKE_STD_ZVAL(proxy->myself);
23 ZVAL_OBJVAL(proxy->myself, php_http_property_proxy_object_new_ex(php_http_property_proxy_class_entry, proxy, NULL TSRMLS_CC), 0);
24 Z_ADDREF_P(object);
25 proxy->object = object;
26 proxy->member = php_http_ztyp(IS_STRING, member);
27
28 return proxy;
29 }
30
31 void php_http_property_proxy_dtor(php_http_property_proxy_t *proxy)
32 {
33 zval_ptr_dtor(&proxy->object);
34 zval_ptr_dtor(&proxy->member);
35 zval_ptr_dtor(&proxy->myself);
36 }
37
38 void php_http_property_proxy_free(php_http_property_proxy_t **proxy)
39 {
40 if (*proxy) {
41 php_http_property_proxy_dtor(*proxy);
42 efree(*proxy);
43 *proxy = NULL;
44 }
45 }
46
47
48 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpPropertyProxy, method, 0, req_args)
49 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpPropertyProxy, method, 0)
50 #define PHP_HTTP_PP_ME(method, visibility) PHP_ME(HttpPropertyProxy, method, PHP_HTTP_ARGS(HttpPropertyProxy, method), visibility)
51
52 PHP_HTTP_EMPTY_ARGS(__construct);
53
54 zend_class_entry *php_http_property_proxy_class_entry;
55 zend_function_entry php_http_property_proxy_method_entry[] = {
56 PHP_HTTP_PP_ME(__construct, ZEND_ACC_FINAL|ZEND_ACC_PRIVATE)
57 EMPTY_FUNCTION_ENTRY
58 };
59 static zend_object_handlers php_http_property_proxy_object_handlers;
60
61 zend_object_value php_http_property_proxy_object_new(zend_class_entry *ce TSRMLS_DC)
62 {
63 return php_http_property_proxy_object_new_ex(ce, NULL, NULL TSRMLS_CC);
64 }
65
66 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)
67 {
68 zend_object_value ov;
69 php_http_property_proxy_object_t *o;
70
71 o = ecalloc(1, sizeof(*o));
72 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
73 object_properties_init((zend_object *) o, ce);
74
75 if (ptr) {
76 *ptr = o;
77 }
78 o->proxy = proxy;
79
80 ov.handle = zend_objects_store_put(o, NULL, php_http_property_proxy_object_free, NULL TSRMLS_CC);
81 ov.handlers = &php_http_property_proxy_object_handlers;
82
83 return ov;
84 }
85
86 void php_http_property_proxy_object_free(void *object TSRMLS_DC)
87 {
88 php_http_property_proxy_object_t *o = object;
89
90 if (o->proxy) {
91 php_http_property_proxy_free(&o->proxy);
92 }
93 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
94 efree(o);
95 }
96
97 static void php_http_property_proxy_object_set(zval **object, zval *value TSRMLS_DC)
98 {
99 php_http_property_proxy_object_t *obj = zend_object_store_get_object(*object TSRMLS_CC);
100
101 zend_update_property(Z_OBJCE_P(obj->proxy->object), obj->proxy->object, Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member), value TSRMLS_CC);
102 }
103
104 static zval *php_http_property_proxy_object_get(zval *object TSRMLS_DC)
105 {
106 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
107
108 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);
109 }
110
111 static STATUS php_http_property_proxy_object_cast(zval *object, zval *return_value, int type TSRMLS_DC)
112 {
113 zval *old_value, *new_value;
114
115 old_value = php_http_property_proxy_object_get(object TSRMLS_CC);
116 new_value = php_http_ztyp(type, old_value);
117
118 if (old_value != new_value) {
119 zval_ptr_dtor(&old_value);
120 }
121
122 RETVAL_ZVAL(new_value, 0, 0);
123
124 return SUCCESS;
125 }
126
127 static zval *php_http_property_proxy_object_read_dimension(zval *object, zval *offset, int type TSRMLS_DC)
128 {
129 zval *retval = NULL, *property = php_http_property_proxy_object_get(object TSRMLS_CC);
130
131 if (Z_TYPE_P(property) == IS_ARRAY) {
132 zval **data = NULL;
133
134 if (Z_TYPE_P(offset) == IS_LONG) {
135 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(property), Z_LVAL_P(offset), (void *) &data)) {
136 retval = *data;
137 }
138 } else {
139 offset = php_http_ztyp(IS_STRING, offset);
140 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(property), Z_STRVAL_P(offset), Z_STRLEN_P(offset), (void *) &data)) {
141 retval = *data;
142 }
143 zval_ptr_dtor(&offset);
144 }
145
146 if (data) {
147 Z_ADDREF_PP(data);
148 }
149 }
150 zval_ptr_dtor(&property);
151
152 return retval;
153 }
154
155 static void php_http_property_proxy_object_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC)
156 {
157 zval *property = php_http_property_proxy_object_get(object TSRMLS_CC);
158
159 switch (Z_TYPE_P(property)) {
160 case IS_NULL:
161 array_init(property);
162 /* fallthrough */
163 case IS_ARRAY:
164 Z_ADDREF_P(value);
165 if (!offset) {
166 add_next_index_zval(property, value);
167 } else if (Z_TYPE_P(offset) == IS_LONG) {
168 add_index_zval(property, Z_LVAL_P(offset), value);
169 } else {
170 offset = php_http_ztyp(IS_STRING, offset);
171 add_assoc_zval_ex(property, Z_STRVAL_P(offset), Z_STRLEN_P(offset) + 1, value);
172 zval_ptr_dtor(&offset);
173 }
174 php_http_property_proxy_object_set(&object, property TSRMLS_CC);
175 break;
176
177 default:
178 zval_ptr_dtor(&property);
179 break;
180 }
181 }
182
183 PHP_METHOD(HttpPropertyProxy, __construct)
184 {
185 }
186
187 PHP_MINIT_FUNCTION(http_property_proxy)
188 {
189 PHP_HTTP_REGISTER_CLASS(http\\Object, PropertyProxy, http_property_proxy, NULL, ZEND_ACC_FINAL);
190 php_http_property_proxy_class_entry->create_object = php_http_property_proxy_object_new;
191 memcpy(&php_http_property_proxy_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
192 php_http_property_proxy_object_handlers.set = php_http_property_proxy_object_set;
193 php_http_property_proxy_object_handlers.get = php_http_property_proxy_object_get;
194 php_http_property_proxy_object_handlers.cast_object = php_http_property_proxy_object_cast;
195 php_http_property_proxy_object_handlers.read_dimension = php_http_property_proxy_object_read_dimension;
196 php_http_property_proxy_object_handlers.write_dimension = php_http_property_proxy_object_write_dimension;
197
198 return SUCCESS;
199 }
200
201
202 /*
203 * Local variables:
204 * tab-width: 4
205 * c-basic-offset: 4
206 * End:
207 * vim600: noet sw=4 ts=4 fdm=marker
208 * vim<600: noet sw=4 ts=4
209 */
210