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.h"
23 #if PHP_VERSION_ID < 50500
30 #define DEBUG_PROPRO 0
32 #if PHP_VERSION_ID < 50400
33 # define object_properties_init(o, ce) \
34 zend_hash_copy(((zend_object *) o)->properties, &(ce->default_properties), \
35 (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*))
38 php_property_proxy_t
*php_property_proxy_init(zval
*container
,
39 const char *member_str
, size_t member_len TSRMLS_DC
)
41 php_property_proxy_t
*proxy
= ecalloc(1, sizeof(*proxy
));
43 Z_ADDREF_P(container
);
44 proxy
->container
= container
;
45 proxy
->member_str
= estrndup(member_str
, member_len
);
46 proxy
->member_len
= member_len
;
51 void php_property_proxy_free(php_property_proxy_t
**proxy
)
54 zval_ptr_dtor(&(*proxy
)->container
);
55 efree((*proxy
)->member_str
);
61 static zend_class_entry
*php_property_proxy_class_entry
;
62 static zend_object_handlers php_property_proxy_object_handlers
;
64 zend_class_entry
*php_property_proxy_get_class_entry(void)
66 return php_property_proxy_class_entry
;
69 zend_object_value
php_property_proxy_object_new(zend_class_entry
*ce TSRMLS_DC
)
71 return php_property_proxy_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
74 static void php_property_proxy_object_free(void *object TSRMLS_DC
)
76 php_property_proxy_object_t
*o
= object
;
79 fprintf(stderr
, "#PP %p free\n", o
);
83 php_property_proxy_free(&o
->proxy
);
86 zend_objects_store_del_ref_by_handle_ex(o
->parent
->zv
.handle
,
87 o
->parent
->zv
.handlers TSRMLS_CC
);
90 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
94 zend_object_value
php_property_proxy_object_new_ex(zend_class_entry
*ce
,
95 php_property_proxy_t
*proxy
, php_property_proxy_object_t
**ptr TSRMLS_DC
)
97 php_property_proxy_object_t
*o
;
99 o
= ecalloc(1, sizeof(*o
));
100 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
101 object_properties_init((zend_object
*) o
, ce
);
108 o
->zv
.handle
= zend_objects_store_put(o
, NULL
,
109 php_property_proxy_object_free
, NULL TSRMLS_CC
);
110 o
->zv
.handlers
= &php_property_proxy_object_handlers
;
113 fprintf(stderr
, "#PP %p init\n", o
);
120 /* we do not really care about TS when debugging */
121 static int level
= 1;
122 static const char space
[] = " ";
123 static const char *inoutstr
[] = {"< return",""," > enter"};
124 static void _walk(php_property_proxy_object_t
*obj TSRMLS_DC
)
127 _walk(obj
->parent TSRMLS_CC
);
128 fprintf(stderr
, ".%s", obj
->proxy
->member_str
);
132 static void debug_propro(int inout
, const char *f
, zval
*object
, zval
*offset
,
133 zval
*value TSRMLS_DC
)
135 php_property_proxy_object_t
*obj
;
137 obj
= zend_object_store_get_object(object TSRMLS_CC
);
138 fprintf(stderr
, "#PP %p %s %s %s ", obj
, &space
[sizeof(space
)-level
],
139 inoutstr
[inout
+1], f
);
143 _walk(obj TSRMLS_CC
);
149 char *offset_str
= "[]";
153 convert_to_string_ex(&o
);
154 offset_str
= Z_STRVAL_P(o
);
157 fprintf(stderr
, ".%s", offset_str
);
159 if (o
&& o
!= offset
) {
177 fprintf(stderr
, " = (%s) ", t
[Z_TYPE_P(value
)&0xf]);
178 zend_print_flat_zval_r(value TSRMLS_CC
);
181 fprintf(stderr
, "\n");
184 #define debug_propro(l, f, obj, off, val)
187 static zval
*get_parent_proxied_value(zval
*object TSRMLS_DC
);
188 static zval
*get_proxied_value(zval
*object TSRMLS_DC
);
189 static zval
*read_dimension(zval
*object
, zval
*offset
, int type TSRMLS_DC
);
190 static ZEND_RESULT_CODE
cast_proxied_value(zval
*object
, zval
*return_value
,
192 static void write_dimension(zval
*object
, zval
*offset
, zval
*value TSRMLS_DC
);
193 static void set_proxied_value(zval
**object
, zval
*value TSRMLS_DC
);
195 static zval
*get_parent_proxied_value(zval
*object TSRMLS_DC
)
198 php_property_proxy_object_t
*obj
;
200 obj
= zend_object_store_get_object(object TSRMLS_CC
);
205 MAKE_STD_ZVAL(parent
);
206 parent
->type
= IS_OBJECT
;
207 parent
->value
.obj
= obj
->parent
->zv
;
208 zend_objects_store_add_ref_by_handle(
209 obj
->parent
->zv
.handle TSRMLS_CC
);
210 value
= get_proxied_value(parent TSRMLS_CC
);
211 zval_ptr_dtor(&parent
);
218 static zval
*get_proxied_value(zval
*object TSRMLS_DC
)
220 zval
**hash_value
, *value
= NULL
;
221 php_property_proxy_object_t
*obj
;
224 obj
= zend_object_store_get_object(object TSRMLS_CC
);
225 debug_propro(1, "get", object
, NULL
, NULL TSRMLS_CC
);
229 zval
*parent_value
= get_parent_proxied_value(object TSRMLS_CC
);
231 if (parent_value
&& parent_value
!= obj
->proxy
->container
) {
232 Z_ADDREF_P(parent_value
);
233 zval_ptr_dtor(&obj
->proxy
->container
);
234 obj
->proxy
->container
= parent_value
;
237 switch (Z_TYPE_P(obj
->proxy
->container
)) {
239 value
= zend_read_property(Z_OBJCE_P(obj
->proxy
->container
),
240 obj
->proxy
->container
, obj
->proxy
->member_str
,
241 obj
->proxy
->member_len
, 0 TSRMLS_CC
);
245 rv
= zend_symtable_find(Z_ARRVAL_P(obj
->proxy
->container
),
246 obj
->proxy
->member_str
, obj
->proxy
->member_len
+ 1,
247 (void *) &hash_value
);
256 debug_propro(-1, "get", object
, NULL
, value TSRMLS_CC
);
261 static ZEND_RESULT_CODE
cast_proxied_value(zval
*object
, zval
*return_value
,
266 if ((proxied_value
= get_proxied_value(object TSRMLS_CC
))) {
267 RETVAL_ZVAL(proxied_value
, 1, 0);
268 if (Z_TYPE_P(proxied_value
) != type
) {
269 convert_to_explicit_type(return_value
, type
);
277 static void set_proxied_value(zval
**object
, zval
*value TSRMLS_DC
)
279 php_property_proxy_object_t
*obj
;
281 obj
= zend_object_store_get_object(*object TSRMLS_CC
);
282 debug_propro(1, "set", *object
, NULL
, value TSRMLS_CC
);
286 zval
*parent_value
= get_parent_proxied_value(*object TSRMLS_CC
);
288 if (parent_value
&& parent_value
!= obj
->proxy
->container
) {
289 Z_ADDREF_P(parent_value
);
290 zval_ptr_dtor(&obj
->proxy
->container
);
291 obj
->proxy
->container
= parent_value
;
295 switch (Z_TYPE_P(obj
->proxy
->container
)) {
297 zend_update_property(Z_OBJCE_P(obj
->proxy
->container
),
298 obj
->proxy
->container
, obj
->proxy
->member_str
,
299 obj
->proxy
->member_len
, value TSRMLS_CC
);
304 zend_symtable_update(Z_ARRVAL_P(obj
->proxy
->container
),
305 obj
->proxy
->member_str
, obj
->proxy
->member_len
+ 1,
306 (void *) &value
, sizeof(zval
*), NULL
);
312 MAKE_STD_ZVAL(zparent
);
313 zparent
->type
= IS_OBJECT
;
314 zparent
->value
.obj
= obj
->parent
->zv
;
315 zend_objects_store_add_ref_by_handle(
316 obj
->parent
->zv
.handle TSRMLS_CC
);
317 set_proxied_value(&zparent
, obj
->proxy
->container TSRMLS_CC
);
318 zval_ptr_dtor(&zparent
);
322 debug_propro(-1, "set", *object
, NULL
, NULL TSRMLS_CC
);
325 static zval
*read_dimension(zval
*object
, zval
*offset
, int type TSRMLS_DC
)
331 debug_propro(1, type
== BP_VAR_R
? "dim_read" : "dim_read_ref", object
,
332 offset
, NULL TSRMLS_CC
);
334 proxied_value
= get_proxied_value(object TSRMLS_CC
);
335 convert_to_string_ex(&o
);
337 if (BP_VAR_R
== type
&& proxied_value
) {
338 if (Z_TYPE_P(proxied_value
) == IS_ARRAY
) {
340 ZEND_RESULT_CODE rv
= zend_symtable_find(Z_ARRVAL_P(proxied_value
),
341 Z_STRVAL_P(o
), Z_STRLEN_P(o
), (void *) &hash_value
);
344 Z_ADDREF_PP(hash_value
);
349 php_property_proxy_t
*proxy
;
350 php_property_proxy_object_t
*proxy_obj
;
353 convert_to_array(proxied_value
);
354 Z_ADDREF_P(proxied_value
);
356 MAKE_STD_ZVAL(proxied_value
);
357 array_init(proxied_value
);
358 set_proxied_value(&object
, proxied_value TSRMLS_CC
);
361 proxy
= php_property_proxy_init(proxied_value
, Z_STRVAL_P(o
),
362 Z_STRLEN_P(o
) TSRMLS_CC
);
363 zval_ptr_dtor(&proxied_value
);
364 MAKE_STD_ZVAL(value
);
365 Z_SET_REFCOUNT_P(value
, 0);
366 value
->type
= IS_OBJECT
;
367 value
->value
.obj
= php_property_proxy_object_new_ex(
368 php_property_proxy_get_class_entry(), proxy
,
369 &proxy_obj TSRMLS_CC
);
370 proxy_obj
->parent
= zend_object_store_get_object(object TSRMLS_CC
);
371 zend_objects_store_add_ref_by_handle(
372 proxy_obj
->parent
->zv
.handle TSRMLS_CC
);
374 if (o
&& o
!= offset
) {
378 debug_propro(-1, type
== BP_VAR_R
? "dim_read" : "dim_read_ref", object
,
379 offset
, value TSRMLS_CC
);
384 static int has_dimension(zval
*object
, zval
*offset
, int check_empty TSRMLS_DC
)
389 debug_propro(1, "dim_exists", object
, offset
, NULL TSRMLS_CC
);
391 proxied_value
= get_proxied_value(object TSRMLS_CC
);
392 if (!proxied_value
) {
397 convert_to_string_ex(&o
);
399 if (Z_TYPE_P(proxied_value
) == IS_ARRAY
) {
401 ZEND_RESULT_CODE rv
= zend_symtable_find(Z_ARRVAL_P(proxied_value
), Z_STRVAL_P(o
), Z_STRLEN_P(o
) + 1, (void *) &zentry
);
407 exists
= Z_TYPE_PP(zentry
) != IS_NULL
;
419 debug_propro(-1, "dim_exists", object
, offset
, NULL TSRMLS_CC
);
424 static void write_dimension(zval
*object
, zval
*offset
, zval
*value TSRMLS_DC
)
426 zval
*proxied_value
, *o
= offset
;
428 debug_propro(1, "dim_write", object
, offset
, value TSRMLS_CC
);
430 proxied_value
= get_proxied_value(object TSRMLS_CC
);
433 convert_to_array(proxied_value
);
434 Z_ADDREF_P(proxied_value
);
436 MAKE_STD_ZVAL(proxied_value
);
437 array_init(proxied_value
);
440 if (Z_REFCOUNT_P(value
) > 1) {
441 SEPARATE_ZVAL(&value
);
446 convert_to_string_ex(&o
);
447 zend_symtable_update(Z_ARRVAL_P(proxied_value
), Z_STRVAL_P(o
),
448 Z_STRLEN_P(o
) + 1, (void *) &value
, sizeof(zval
*), NULL
);
450 zend_hash_next_index_insert(Z_ARRVAL_P(proxied_value
), (void *) &value
,
451 sizeof(zval
*), NULL
);
454 if (o
&& o
!= offset
) {
458 set_proxied_value(&object
, proxied_value TSRMLS_CC
);
460 debug_propro(-1, "dim_write", object
, offset
, proxied_value TSRMLS_CC
);
462 zval_ptr_dtor(&proxied_value
);
465 static void unset_dimension(zval
*object
, zval
*offset TSRMLS_DC
)
469 debug_propro(1, "dim_unset", object
, offset
, NULL TSRMLS_CC
);
471 proxied_value
= get_proxied_value(object TSRMLS_CC
);
473 if (proxied_value
&& Z_TYPE_P(proxied_value
) == IS_ARRAY
) {
477 convert_to_string_ex(&o
);
478 rv
= zend_symtable_del(Z_ARRVAL_P(proxied_value
), Z_STRVAL_P(o
),
482 set_proxied_value(&object
, proxied_value TSRMLS_CC
);
490 debug_propro(-1, "dim_unset", object
, offset
, proxied_value TSRMLS_CC
);
493 ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct
, 0, 0, 2)
494 ZEND_ARG_INFO(1, object
)
495 ZEND_ARG_INFO(0, member
)
496 ZEND_ARG_OBJ_INFO(0, parent
, php
\\PropertyProxy
, 1)
498 static PHP_METHOD(propro
, __construct
) {
499 zend_error_handling zeh
;
500 zval
*container
, *parent
= NULL
;
504 zend_replace_error_handling(EH_THROW
, NULL
, &zeh TSRMLS_CC
);
505 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "zs|O!",
506 &container
, &member_str
, &member_len
, &parent
,
507 php_property_proxy_class_entry
)) {
508 php_property_proxy_object_t
*obj
;
510 obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
511 obj
->proxy
= php_property_proxy_init(container
, member_str
,
512 member_len TSRMLS_CC
);
514 zend_objects_store_add_ref(parent TSRMLS_CC
);
515 obj
->parent
= zend_object_store_get_object(parent TSRMLS_CC
);
518 zend_restore_error_handling(&zeh TSRMLS_CC
);
521 static const zend_function_entry php_property_proxy_method_entry
[] = {
522 PHP_ME(propro
, __construct
, ai_propro_construct
, ZEND_ACC_PUBLIC
)
526 static PHP_MINIT_FUNCTION(propro
)
528 zend_class_entry ce
= {0};
530 INIT_NS_CLASS_ENTRY(ce
, "php", "PropertyProxy",
531 php_property_proxy_method_entry
);
532 php_property_proxy_class_entry
= zend_register_internal_class_ex(&ce
, NULL
,
534 php_property_proxy_class_entry
->create_object
=
535 php_property_proxy_object_new
;
536 php_property_proxy_class_entry
->ce_flags
|= ZEND_ACC_FINAL_CLASS
;
538 memcpy(&php_property_proxy_object_handlers
, zend_get_std_object_handlers(),
539 sizeof(zend_object_handlers
));
540 php_property_proxy_object_handlers
.set
= set_proxied_value
;
541 php_property_proxy_object_handlers
.get
= get_proxied_value
;
542 php_property_proxy_object_handlers
.cast_object
= cast_proxied_value
;
543 php_property_proxy_object_handlers
.read_dimension
= read_dimension
;
544 php_property_proxy_object_handlers
.write_dimension
= write_dimension
;
545 php_property_proxy_object_handlers
.has_dimension
= has_dimension
;
546 php_property_proxy_object_handlers
.unset_dimension
= unset_dimension
;
551 PHP_MINFO_FUNCTION(propro
)
553 php_info_print_table_start();
554 php_info_print_table_header(2, "Property proxy support", "enabled");
555 php_info_print_table_row(2, "Extension version", PHP_PROPRO_VERSION
);
556 php_info_print_table_end();
559 static const zend_function_entry propro_functions
[] = {
563 zend_module_entry propro_module_entry
= {
564 STANDARD_MODULE_HEADER
,
573 STANDARD_MODULE_PROPERTIES
576 #ifdef COMPILE_DL_PROPRO
577 ZEND_GET_MODULE(propro
)
586 * vim600: noet sw=4 ts=4 fdm=marker
587 * vim<600: noet sw=4 ts=4