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