2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 #ifndef PHP_HTTP_PPDBG
16 # define PHP_HTTP_PPDBG 0
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
)
22 proxy
= emalloc(sizeof(*proxy
));
24 memset(proxy
, 0, sizeof(*proxy
));
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);
29 proxy
->object
= object
;
30 proxy
->member
= php_http_ztyp(IS_STRING
, member
);
33 proxy
->parent
= parent
;
37 fprintf(stderr
, "proxy_init: %s\n", Z_STRVAL_P(proxy
->member
));
42 void php_http_property_proxy_dtor(php_http_property_proxy_t
*proxy
)
44 zval_ptr_dtor(&proxy
->object
);
45 zval_ptr_dtor(&proxy
->member
);
46 zval_ptr_dtor(&proxy
->myself
);
48 zval_ptr_dtor(&proxy
->parent
);
52 void php_http_property_proxy_free(php_http_property_proxy_t
**proxy
)
55 php_http_property_proxy_dtor(*proxy
);
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)
66 PHP_HTTP_EMPTY_ARGS(__construct
);
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
)
73 static zend_object_handlers php_http_property_proxy_object_handlers
;
75 zend_object_value
php_http_property_proxy_object_new(zend_class_entry
*ce TSRMLS_DC
)
77 return php_http_property_proxy_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
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
)
83 php_http_property_proxy_object_t
*o
;
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
);
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
;
100 void php_http_property_proxy_object_free(void *object TSRMLS_DC
)
102 php_http_property_proxy_object_t
*o
= object
;
105 php_http_property_proxy_free(&o
->proxy
);
107 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
111 static zval
*php_http_property_proxy_object_get(zval
*object TSRMLS_DC
)
113 php_http_property_proxy_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
116 fprintf(stderr
, "proxy_get: %s\n", Z_STRVAL_P(obj
->proxy
->member
));
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
);
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
)) {
128 MAKE_STD_ZVAL(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
);
139 static void php_http_property_proxy_object_set(zval
**object
, zval
*value TSRMLS_DC
)
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
;
145 fprintf(stderr
, "proxy_set: %s\n", Z_STRVAL_P(obj
->proxy
->member
));
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
);
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
);
154 if (obj
->proxy
->parent
) {
155 php_http_property_proxy_object_set(&obj
->proxy
->parent
, target TSRMLS_CC
);
156 zval_ptr_dtor(&target
);
160 static STATUS
php_http_property_proxy_object_cast(zval
*object
, zval
*return_value
, int type TSRMLS_DC
)
162 zval
*old_value
, *new_value
;
164 old_value
= php_http_property_proxy_object_get(object TSRMLS_CC
);
165 new_value
= php_http_ztyp(type
, old_value
);
167 if (old_value
!= new_value
) {
168 zval_ptr_dtor(&old_value
);
171 RETVAL_ZVAL(new_value
, 0, 0);
176 static zval
*php_http_property_proxy_object_read_dimension(zval
*object
, zval
*offset
, int type TSRMLS_DC
)
178 php_http_property_proxy_object_t
*obj
= zend_object_store_get_object(object TSRMLS_CC
);
179 zval
*retval
= NULL
, *property
;
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
);
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
) {
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
)) {
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
)) {
203 zval_ptr_dtor(&offset
);
210 zval_ptr_dtor(&property
);
215 static void php_http_property_proxy_object_write_dimension(zval
*object
, zval
*offset
, zval
*value TSRMLS_DC
)
217 zval
*property
= php_http_property_proxy_object_get(object TSRMLS_CC
);
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
);
225 switch (Z_TYPE_P(property
)) {
227 array_init(property
);
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
);
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
);
240 php_http_property_proxy_object_set(&object
, property TSRMLS_CC
);
241 zval_ptr_dtor(&property
);
245 zval_ptr_dtor(&property
);
250 PHP_METHOD(HttpPropertyProxy
, __construct
)
254 PHP_MINIT_FUNCTION(http_property_proxy
)
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
;
274 * vim600: noet sw=4 ts=4 fdm=marker
275 * vim<600: noet sw=4 ts=4