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) 2013 Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #include <ext/standard/info.h>
21 #include "php_propro_api.h"
23 #if PHP_VERSION_ID < 50500
32 #define DEBUG_PROPRO 0
34 #if PHP_VERSION_ID < 50400
35 # define object_properties_init(o, ce) \
36 zend_hash_copy(((zend_object *) o)->properties, &(ce->default_properties), \
37 (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*))
40 php_property_proxy_t
*php_property_proxy_init(zval
*container
,
41 const char *member_str
, size_t member_len TSRMLS_DC
)
43 php_property_proxy_t
*proxy
= ecalloc(1, sizeof(*proxy
));
45 Z_ADDREF_P(container
);
46 proxy
->container
= container
;
47 proxy
->member_str
= estrndup(member_str
, member_len
);
48 proxy
->member_len
= member_len
;
53 void php_property_proxy_free(php_property_proxy_t
**proxy
)
56 zval_ptr_dtor(&(*proxy
)->container
);
57 efree((*proxy
)->member_str
);
63 static zend_class_entry
*php_property_proxy_class_entry
;
64 static zend_object_handlers php_property_proxy_object_handlers
;
66 zend_class_entry
*php_property_proxy_get_class_entry(void)
68 return php_property_proxy_class_entry
;
71 zend_object_value
php_property_proxy_object_new(zend_class_entry
*ce TSRMLS_DC
)
73 return php_property_proxy_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
76 static void php_property_proxy_object_free(void *object TSRMLS_DC
)
78 php_property_proxy_object_t
*o
= object
;
81 fprintf(stderr
, "#PP %p free\n", o
);
85 php_property_proxy_free(&o
->proxy
);
88 zend_objects_store_del_ref_by_handle_ex(o
->parent
->zv
.handle
,
89 o
->parent
->zv
.handlers TSRMLS_CC
);
92 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
96 zend_object_value
php_property_proxy_object_new_ex(zend_class_entry
*ce
,
97 php_property_proxy_t
*proxy
, php_property_proxy_object_t
**ptr TSRMLS_DC
)
99 php_property_proxy_object_t
*o
;
101 o
= ecalloc(1, sizeof(*o
));
102 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
103 object_properties_init((zend_object
*) o
, ce
);
110 o
->zv
.handle
= zend_objects_store_put(o
, NULL
,
111 php_property_proxy_object_free
, NULL TSRMLS_CC
);
112 o
->zv
.handlers
= &php_property_proxy_object_handlers
;
115 fprintf(stderr
, "#PP %p init\n", o
);
122 /* we do not really care about TS when debugging */
123 static int level
= 1;
124 static const char space
[] = " ";
125 static const char *inoutstr
[] = {"< return",""," > enter"};
126 static void _walk(php_property_proxy_object_t
*obj TSRMLS_DC
)
129 _walk(obj
->parent TSRMLS_CC
);
130 fprintf(stderr
, ".%s", obj
->proxy
->member_str
);
134 static void debug_propro(int inout
, const char *f
, zval
*object
, zval
*offset
,
135 zval
*value TSRMLS_DC
)
137 php_property_proxy_object_t
*obj
;
139 obj
= zend_object_store_get_object(object TSRMLS_CC
);
140 fprintf(stderr
, "#PP %p %s %s %s ", obj
, &space
[sizeof(space
)-level
],
141 inoutstr
[inout
+1], f
);
145 _walk(obj TSRMLS_CC
);
151 char *offset_str
= "[]";
155 convert_to_string_ex(&o
);
156 offset_str
= Z_STRVAL_P(o
);
159 fprintf(stderr
, ".%s", offset_str
);
161 if (o
&& o
!= offset
) {
179 fprintf(stderr
, " = (%s) ", t
[Z_TYPE_P(value
)&0xf]);
180 zend_print_flat_zval_r(value TSRMLS_CC
);
183 fprintf(stderr
, "\n");
186 #define debug_propro(l, f, obj, off, val)
189 static zval
*get_parent_proxied_value(zval
*object TSRMLS_DC
);
190 static zval
*get_proxied_value(zval
*object TSRMLS_DC
);
191 static zval
*read_dimension(zval
*object
, zval
*offset
, int type TSRMLS_DC
);
192 static ZEND_RESULT_CODE
cast_proxied_value(zval
*object
, zval
*return_value
,
194 static void write_dimension(zval
*object
, zval
*offset
, zval
*value TSRMLS_DC
);
195 static void set_proxied_value(zval
**object
, zval
*value TSRMLS_DC
);
197 static zval
*get_parent_proxied_value(zval
*object TSRMLS_DC
)
200 php_property_proxy_object_t
*obj
;
202 obj
= zend_object_store_get_object(object TSRMLS_CC
);
207 MAKE_STD_ZVAL(parent
);
208 parent
->type
= IS_OBJECT
;
209 parent
->value
.obj
= obj
->parent
->zv
;
210 zend_objects_store_add_ref_by_handle(
211 obj
->parent
->zv
.handle TSRMLS_CC
);
212 value
= get_proxied_value(parent TSRMLS_CC
);
213 zval_ptr_dtor(&parent
);
220 static zval
*get_proxied_value(zval
*object TSRMLS_DC
)
222 zval
**hash_value
, *value
= NULL
;
223 php_property_proxy_object_t
*obj
;
226 obj
= zend_object_store_get_object(object TSRMLS_CC
);
227 debug_propro(1, "get", object
, NULL
, NULL TSRMLS_CC
);
231 zval
*parent_value
= get_parent_proxied_value(object TSRMLS_CC
);
233 if (parent_value
&& parent_value
!= obj
->proxy
->container
) {
234 Z_ADDREF_P(parent_value
);
235 zval_ptr_dtor(&obj
->proxy
->container
);
236 obj
->proxy
->container
= parent_value
;
239 switch (Z_TYPE_P(obj
->proxy
->container
)) {
241 value
= zend_read_property(Z_OBJCE_P(obj
->proxy
->container
),
242 obj
->proxy
->container
, obj
->proxy
->member_str
,
243 obj
->proxy
->member_len
, 0 TSRMLS_CC
);
247 rv
= zend_symtable_find(Z_ARRVAL_P(obj
->proxy
->container
),
248 obj
->proxy
->member_str
, obj
->proxy
->member_len
+ 1,
249 (void *) &hash_value
);
258 debug_propro(-1, "get", object
, NULL
, value TSRMLS_CC
);
263 static ZEND_RESULT_CODE
cast_proxied_value(zval
*object
, zval
*return_value
,
268 if ((proxied_value
= get_proxied_value(object TSRMLS_CC
))) {
269 RETVAL_ZVAL(proxied_value
, 1, 0);
270 if (Z_TYPE_P(proxied_value
) != type
) {
271 convert_to_explicit_type(return_value
, type
);
279 static void set_proxied_value(zval
**object
, zval
*value TSRMLS_DC
)
281 php_property_proxy_object_t
*obj
;
283 obj
= zend_object_store_get_object(*object TSRMLS_CC
);
284 debug_propro(1, "set", *object
, NULL
, value TSRMLS_CC
);
288 zval
*parent_value
= get_parent_proxied_value(*object TSRMLS_CC
);
290 if (parent_value
&& parent_value
!= obj
->proxy
->container
) {
291 Z_ADDREF_P(parent_value
);
292 zval_ptr_dtor(&obj
->proxy
->container
);
293 obj
->proxy
->container
= parent_value
;
297 switch (Z_TYPE_P(obj
->proxy
->container
)) {
299 zend_update_property(Z_OBJCE_P(obj
->proxy
->container
),
300 obj
->proxy
->container
, obj
->proxy
->member_str
,
301 obj
->proxy
->member_len
, value TSRMLS_CC
);
306 zend_symtable_update(Z_ARRVAL_P(obj
->proxy
->container
),
307 obj
->proxy
->member_str
, obj
->proxy
->member_len
+ 1,
308 (void *) &value
, sizeof(zval
*), NULL
);
314 MAKE_STD_ZVAL(zparent
);
315 zparent
->type
= IS_OBJECT
;
316 zparent
->value
.obj
= obj
->parent
->zv
;
317 zend_objects_store_add_ref_by_handle(
318 obj
->parent
->zv
.handle TSRMLS_CC
);
319 set_proxied_value(&zparent
, obj
->proxy
->container TSRMLS_CC
);
320 zval_ptr_dtor(&zparent
);
324 debug_propro(-1, "set", *object
, NULL
, NULL TSRMLS_CC
);
327 static zval
*read_dimension(zval
*object
, zval
*offset
, int type TSRMLS_DC
)
333 debug_propro(1, type
== BP_VAR_R
? "dim_read" : "dim_read_ref", object
,
334 offset
, NULL TSRMLS_CC
);
336 proxied_value
= get_proxied_value(object TSRMLS_CC
);
337 convert_to_string_ex(&o
);
339 if (BP_VAR_R
== type
&& proxied_value
) {
340 if (Z_TYPE_P(proxied_value
) == IS_ARRAY
) {
342 ZEND_RESULT_CODE rv
= zend_symtable_find(Z_ARRVAL_P(proxied_value
),
343 Z_STRVAL_P(o
), Z_STRLEN_P(o
), (void *) &hash_value
);
346 Z_ADDREF_PP(hash_value
);
351 php_property_proxy_t
*proxy
;
352 php_property_proxy_object_t
*proxy_obj
;
355 convert_to_array(proxied_value
);
356 Z_ADDREF_P(proxied_value
);
358 MAKE_STD_ZVAL(proxied_value
);
359 array_init(proxied_value
);
360 set_proxied_value(&object
, proxied_value TSRMLS_CC
);
363 proxy
= php_property_proxy_init(proxied_value
, Z_STRVAL_P(o
),
364 Z_STRLEN_P(o
) TSRMLS_CC
);
365 zval_ptr_dtor(&proxied_value
);
366 MAKE_STD_ZVAL(value
);
367 Z_SET_REFCOUNT_P(value
, 0);
368 value
->type
= IS_OBJECT
;
369 value
->value
.obj
= php_property_proxy_object_new_ex(
370 php_property_proxy_get_class_entry(), proxy
,
371 &proxy_obj TSRMLS_CC
);
372 proxy_obj
->parent
= zend_object_store_get_object(object TSRMLS_CC
);
373 zend_objects_store_add_ref_by_handle(
374 proxy_obj
->parent
->zv
.handle TSRMLS_CC
);
376 if (o
&& o
!= offset
) {
380 debug_propro(-1, type
== BP_VAR_R
? "dim_read" : "dim_read_ref", object
,
381 offset
, value TSRMLS_CC
);
386 static int has_dimension(zval
*object
, zval
*offset
, int check_empty TSRMLS_DC
)
391 debug_propro(1, "dim_exists", object
, offset
, NULL TSRMLS_CC
);
393 proxied_value
= get_proxied_value(object TSRMLS_CC
);
394 if (!proxied_value
) {
399 convert_to_string_ex(&o
);
401 if (Z_TYPE_P(proxied_value
) == IS_ARRAY
) {
403 ZEND_RESULT_CODE rv
= zend_symtable_find(Z_ARRVAL_P(proxied_value
), Z_STRVAL_P(o
), Z_STRLEN_P(o
) + 1, (void *) &zentry
);
409 exists
= Z_TYPE_PP(zentry
) != IS_NULL
;
421 debug_propro(-1, "dim_exists", object
, offset
, NULL TSRMLS_CC
);
426 static void write_dimension(zval
*object
, zval
*offset
, zval
*value TSRMLS_DC
)
428 zval
*proxied_value
, *o
= offset
;
430 debug_propro(1, "dim_write", object
, offset
, value TSRMLS_CC
);
432 proxied_value
= get_proxied_value(object TSRMLS_CC
);
435 convert_to_array(proxied_value
);
436 Z_ADDREF_P(proxied_value
);
438 MAKE_STD_ZVAL(proxied_value
);
439 array_init(proxied_value
);
442 if (Z_REFCOUNT_P(value
) > 1) {
443 SEPARATE_ZVAL(&value
);
448 convert_to_string_ex(&o
);
449 zend_symtable_update(Z_ARRVAL_P(proxied_value
), Z_STRVAL_P(o
),
450 Z_STRLEN_P(o
) + 1, (void *) &value
, sizeof(zval
*), NULL
);
452 zend_hash_next_index_insert(Z_ARRVAL_P(proxied_value
), (void *) &value
,
453 sizeof(zval
*), NULL
);
456 if (o
&& o
!= offset
) {
460 set_proxied_value(&object
, proxied_value TSRMLS_CC
);
462 debug_propro(-1, "dim_write", object
, offset
, proxied_value TSRMLS_CC
);
464 zval_ptr_dtor(&proxied_value
);
467 static void unset_dimension(zval
*object
, zval
*offset TSRMLS_DC
)
471 debug_propro(1, "dim_unset", object
, offset
, NULL TSRMLS_CC
);
473 proxied_value
= get_proxied_value(object TSRMLS_CC
);
475 if (proxied_value
&& Z_TYPE_P(proxied_value
) == IS_ARRAY
) {
479 convert_to_string_ex(&o
);
480 rv
= zend_symtable_del(Z_ARRVAL_P(proxied_value
), Z_STRVAL_P(o
),
484 set_proxied_value(&object
, proxied_value TSRMLS_CC
);
492 debug_propro(-1, "dim_unset", object
, offset
, proxied_value TSRMLS_CC
);
495 ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct
, 0, 0, 2)
496 ZEND_ARG_INFO(1, object
)
497 ZEND_ARG_INFO(0, member
)
498 ZEND_ARG_OBJ_INFO(0, parent
, php
\\PropertyProxy
, 1)
500 static PHP_METHOD(propro
, __construct
) {
501 zend_error_handling zeh
;
502 zval
*container
, *parent
= NULL
;
506 zend_replace_error_handling(EH_THROW
, NULL
, &zeh TSRMLS_CC
);
507 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "zs|O!",
508 &container
, &member_str
, &member_len
, &parent
,
509 php_property_proxy_class_entry
)) {
510 php_property_proxy_object_t
*obj
;
512 obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
513 obj
->proxy
= php_property_proxy_init(container
, member_str
,
514 member_len TSRMLS_CC
);
516 zend_objects_store_add_ref(parent TSRMLS_CC
);
517 obj
->parent
= zend_object_store_get_object(parent TSRMLS_CC
);
520 zend_restore_error_handling(&zeh TSRMLS_CC
);
523 static const zend_function_entry php_property_proxy_method_entry
[] = {
524 PHP_ME(propro
, __construct
, ai_propro_construct
, ZEND_ACC_PUBLIC
)
528 static PHP_MINIT_FUNCTION(propro
)
530 zend_class_entry ce
= {0};
532 INIT_NS_CLASS_ENTRY(ce
, "php", "PropertyProxy",
533 php_property_proxy_method_entry
);
534 php_property_proxy_class_entry
= zend_register_internal_class_ex(&ce
, NULL
,
536 php_property_proxy_class_entry
->create_object
=
537 php_property_proxy_object_new
;
538 php_property_proxy_class_entry
->ce_flags
|= ZEND_ACC_FINAL_CLASS
;
540 memcpy(&php_property_proxy_object_handlers
, zend_get_std_object_handlers(),
541 sizeof(zend_object_handlers
));
542 php_property_proxy_object_handlers
.set
= set_proxied_value
;
543 php_property_proxy_object_handlers
.get
= get_proxied_value
;
544 php_property_proxy_object_handlers
.cast_object
= cast_proxied_value
;
545 php_property_proxy_object_handlers
.read_dimension
= read_dimension
;
546 php_property_proxy_object_handlers
.write_dimension
= write_dimension
;
547 php_property_proxy_object_handlers
.has_dimension
= has_dimension
;
548 php_property_proxy_object_handlers
.unset_dimension
= unset_dimension
;
553 PHP_MINFO_FUNCTION(propro
)
555 php_info_print_table_start();
556 php_info_print_table_header(2, "Property proxy support", "enabled");
557 php_info_print_table_row(2, "Extension version", PHP_PROPRO_VERSION
);
558 php_info_print_table_end();
561 static const zend_function_entry propro_functions
[] = {
565 zend_module_entry propro_module_entry
= {
566 STANDARD_MODULE_HEADER
,
575 STANDARD_MODULE_PROPERTIES
578 #ifdef COMPILE_DL_PROPRO
579 ZEND_GET_MODULE(propro
)
588 * vim600: noet sw=4 ts=4 fdm=marker
589 * vim<600: noet sw=4 ts=4