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 #define DEBUG_PROPRO 0
25 static inline php_property_proxy_object_t
*get_propro(zval
*object
);
26 static inline zval
*get_proxied_value(zval
*object
, zval
*return_value
);
27 static inline void set_proxied_value(zval
*object
, zval
*value
);
29 static zval
*read_dimension(zval
*object
, zval
*offset
, int type
, zval
*return_value
);
30 static ZEND_RESULT_CODE
cast_obj(zval
*object
, zval
*return_value
, int type
);
31 static void write_dimension(zval
*object
, zval
*offset
, zval
*input_value
);
34 /* we do not really care about TS when debugging */
36 static const char *inoutstr
[] = {"< return","= "," > enter "};
37 static const char *types
[] = {
58 static const char *_type(zval
*zv
)
60 switch (Z_TYPE_P(zv
)) {
62 if (zv
->value
.obj
->ce
== php_property_proxy_get_class_entry()) {
67 return types
[Z_TYPE_P(zv
)];
71 static int _walk(php_property_proxy_object_t
*obj
)
76 if (!Z_ISUNDEF(obj
->parent
)) {
77 p
+= _walk(get_propro(&obj
->parent
));
80 p
+= fprintf(stderr
, ".%s", obj
->proxy
->member
->val
);
87 static inline zval
*get_container(zval
*object
, zval
*tmp
);
88 static inline zval
*get_container_value(zval
*container
, zend_string
*member
, zval
*return_value
);
90 static void debug_propro(int inout
, const char *f
,
91 php_property_proxy_object_t
*obj
,
92 php_property_proxy_t
*proxy
,
93 zval
*offset
, zval
*value
)
96 zval tmp
, *container
= &tmp
;
102 fprintf(stderr
, "#PP %14p %*c %s %s\t", proxy
, level
, ' ', inoutstr
[inout
+ 1], f
);
109 if (Z_ISUNDEF(obj
->parent
)) {
110 container
= &obj
->proxy
->container
;
112 zval parent_tmp
, *parent_container
;
113 php_property_proxy_object_t
*parent_obj
= get_propro(&obj
->parent
);
115 ZVAL_UNDEF(&parent_tmp
);
116 parent_container
= get_container(&obj
->parent
, &parent_tmp
);
117 container
= get_container_value(parent_container
, parent_obj
->proxy
->member
, &tmp
);
120 fprintf(stderr
, " container= %-14p < %-10s rc=%-11d ",
121 Z_REFCOUNTED_P(container
) ? Z_COUNTED_P(container
) : NULL
,
123 Z_REFCOUNTED_P(container
) ? Z_REFCOUNT_P(container
) : 0);
124 if (Z_ISREF_P(container
)) {
125 zval
*ref
= Z_REFVAL_P(container
);
126 fprintf(stderr
, " %-12s %p rc=% 2d",
129 Z_REFCOUNTED_P(ref
) ? Z_REFCOUNT_P(ref
) : -1);
131 fprintf(stderr
, "> ");
140 char *offset_str
= "[]";
144 convert_to_string_ex(o
);
145 offset_str
= Z_STRVAL_P(o
);
148 width
+= fprintf(stderr
, ".%s", offset_str
);
150 if (o
&& o
!= offset
) {
155 fprintf(stderr
, "%.*s", 32-width
, " ");
156 if (Z_ISUNDEF_P(value
)) {
157 fprintf(stderr
, " = UNDEF");
159 fprintf(stderr
, " = %-14p < %-10s rc=%-11d %3s> ",
160 Z_REFCOUNTED_P(value
) ? Z_COUNTED_P(value
) : NULL
,
162 Z_REFCOUNTED_P(value
) ? Z_REFCOUNT_P(value
) : 0,
163 Z_ISREF_P(value
) ? "ref" : "");
164 if (!Z_ISUNDEF_P(value
) && Z_TYPE_P(value
) != IS_INDIRECT
) {
165 zend_print_flat_zval_r(value TSRMLS_CC
);
170 fprintf(stderr
, "\n");
173 #define debug_propro(l, f, obj, proxy, off, val)
176 php_property_proxy_t
*php_property_proxy_init(zval
*container
, zend_string
*member
)
178 php_property_proxy_t
*proxy
= ecalloc(1, sizeof(*proxy
));
182 ZVAL_STR_COPY(&offset
, member
);
185 debug_propro(1, "init", NULL
, proxy
, &offset
, NULL
);
188 ZVAL_COPY(&proxy
->container
, container
);
190 proxy
->member
= zend_string_copy(member
);
192 debug_propro(-1, "init", NULL
, proxy
, &offset
, NULL
);
201 void php_property_proxy_free(php_property_proxy_t
**proxy
)
206 ZVAL_STR_COPY(&offset
, (*proxy
)->member
);
207 debug_propro(1, "dtor", NULL
, *proxy
, &offset
, NULL
);
211 zval_ptr_dtor(&(*proxy
)->container
);
213 ZVAL_UNDEF(&(*proxy
)->container
);
214 zend_string_release((*proxy
)->member
);
215 (*proxy
)->member
= NULL
;
221 debug_propro(-1, "dtor", NULL
, NULL
, &offset
, NULL
);
227 static zend_class_entry
*php_property_proxy_class_entry
;
228 static zend_object_handlers php_property_proxy_object_handlers
;
230 zend_class_entry
*php_property_proxy_get_class_entry(void)
232 return php_property_proxy_class_entry
;
235 php_property_proxy_object_t
*php_property_proxy_object_new_ex(
236 zend_class_entry
*ce
, php_property_proxy_t
*proxy
)
238 php_property_proxy_object_t
*o
;
241 ce
= php_property_proxy_class_entry
;
244 o
= ecalloc(1, sizeof(*o
) + sizeof(zval
) * (ce
->default_properties_count
- 1));
245 zend_object_std_init(&o
->zo
, ce
);
246 object_properties_init(&o
->zo
, ce
);
249 o
->zo
.handlers
= &php_property_proxy_object_handlers
;
254 zend_object
*php_property_proxy_object_new(zend_class_entry
*ce
)
256 return &php_property_proxy_object_new_ex(ce
, NULL
)->zo
;
259 static void destroy_obj(zend_object
*object
)
261 php_property_proxy_object_t
*o
= PHP_PROPRO_PTR(object
);
264 php_property_proxy_free(&o
->proxy
);
266 if (!Z_ISUNDEF(o
->parent
)) {
267 zval_ptr_dtor(&o
->parent
);
268 ZVAL_UNDEF(&o
->parent
);
270 zend_object_std_dtor(object
);
273 static ZEND_RESULT_CODE
cast_obj(zval
*object
, zval
*return_value
, int type
)
278 RETVAL_ZVAL(get_proxied_value(object
, &tmp
), 1, 0);
280 debug_propro(0, "cast", get_propro(object
), NULL
, NULL
, return_value
);
282 if (!Z_ISUNDEF_P(return_value
)) {
283 ZVAL_DEREF(return_value
);
284 convert_to_explicit_type_ex(return_value
, type
);
291 static zval
*get_obj(zval
*object
, zval
*return_value
)
296 RETVAL_ZVAL(get_proxied_value(object
, &tmp
), 1, 0);
300 static void set_obj(zval
*object
, zval
*value
) {
301 set_proxied_value(object
, value
);
304 static inline php_property_proxy_object_t
*get_propro(zval
*object
)
306 ZEND_ASSERT(Z_TYPE_P(object
) == IS_OBJECT
);
307 return PHP_PROPRO_PTR(Z_OBJ_P(object
));
310 static inline zval
*get_container(zval
*object
, zval
*tmp
)
312 php_property_proxy_object_t
*obj
= get_propro(object
);
315 if (Z_ISUNDEF(obj
->parent
)) {
316 container
= &obj
->proxy
->container
;
318 container
= get_proxied_value(&obj
->parent
, tmp
);
324 static inline void set_container(zval
*object
, zval
*container
)
326 php_property_proxy_object_t
*obj
= get_propro(object
);
328 if (Z_ISUNDEF(obj
->parent
)) {
331 ZVAL_COPY_VALUE(&tmp
, &obj
->proxy
->container
);
332 ZVAL_COPY(&obj
->proxy
->container
, container
);
335 set_proxied_value(&obj
->parent
, container
);
339 static inline zval
*get_container_value(zval
*container
, zend_string
*member
, zval
*return_value
)
341 zval
*found_value
= NULL
, prop_tmp
;
343 ZVAL_DEREF(container
);
344 switch (Z_TYPE_P(container
)) {
346 found_value
= zend_read_property(Z_OBJCE_P(container
), container
,
347 member
->val
, member
->len
, 0, &prop_tmp
);
352 found_value
= zend_symtable_find(Z_ARRVAL_P(container
), member
);
357 RETVAL_ZVAL(found_value
, 0, 0);
363 static inline void cleanup_container(zval
*object
, zval
*container
, zend_bool separated
)
366 zval_ptr_dtor(container
);
370 static inline zend_bool
separate_container(zval
*object
, zval
*container
)
372 switch (Z_TYPE_P(container
)) {
377 /* always duplicate for PHP-7.0 and 7.1 on travis */
378 ZVAL_ARR(container
, zend_array_dup(Z_ARRVAL_P(container
)));
382 array_init(container
);
386 SEPARATE_ZVAL(container
);
387 Z_TRY_ADDREF_P(container
);
388 convert_to_array(container
);
395 static inline zval
*set_container_value(zval
*container
, zend_string
*member
, zval
*value
)
397 ZVAL_DEREF(container
);
398 switch (Z_TYPE_P(container
)) {
400 zend_update_property(Z_OBJCE_P(container
), container
,
401 member
->val
, member
->len
, value
);
405 Z_TRY_ADDREF_P(value
);
407 value
= zend_symtable_update(Z_ARRVAL_P(container
), member
, value
);
409 value
= zend_hash_next_index_insert(Z_ARRVAL_P(container
), value
);
422 static zval
*get_proxied_value(zval
*object
, zval
*return_value
)
424 php_property_proxy_object_t
*obj
= get_propro(object
);
426 debug_propro(1, "get", obj
, NULL
, NULL
, NULL
);
429 zval tmp
, *container
;
432 container
= get_container(object
, &tmp
);
434 return_value
= get_container_value(container
, obj
->proxy
->member
, return_value
);
437 debug_propro(-1, "get", obj
, NULL
, NULL
, return_value
);
442 static void set_proxied_value(zval
*object
, zval
*value
)
444 php_property_proxy_object_t
*obj
= get_propro(object
);
446 debug_propro(1, "set", obj
, NULL
, NULL
, value
);
449 zval tmp
, *container
;
452 Z_TRY_ADDREF_P(value
);
455 container
= get_container(object
, &tmp
);
456 separated
= separate_container(object
, container
);
457 set_container_value(container
, obj
->proxy
->member
, value
);
458 set_container(object
, container
);
459 cleanup_container(object
, container
, separated
);
461 Z_TRY_DELREF_P(value
);
463 debug_propro(0, "set", obj
, NULL
, NULL
, value
);
466 debug_propro(-1, "set", obj
, NULL
, NULL
, value
);
469 static zval
*read_dimension(zval
*object
, zval
*offset
, int type
, zval
*return_value
)
472 zend_string
*member
= offset
? zval_get_string(offset
) : NULL
;
474 debug_propro(1, type
== BP_VAR_R
? "dim_r" : "dim_R",
475 get_propro(object
), NULL
, offset
, NULL
);
478 value
= get_proxied_value(object
, &tmp
);
480 if (type
== BP_VAR_R
|| type
== BP_VAR_IS
) {
483 if (!Z_ISUNDEF_P(value
)) {
487 RETVAL_ZVAL(get_container_value(value
, member
, &tmp
), 1, 0);
490 php_property_proxy_t
*proxy
;
491 php_property_proxy_object_t
*proxy_obj
;
493 if (Z_ISUNDEF_P(value
)) {
498 if (Z_TYPE_P(value
) == IS_ARRAY
) {
499 member
= zend_long_to_str(zend_hash_next_free_element(
501 } else if (Z_TYPE_P(value
) != IS_OBJECT
){
502 member
= zend_long_to_str(0);
506 proxy
= php_property_proxy_init(NULL
, member
);
507 proxy_obj
= php_property_proxy_object_new_ex(NULL
, proxy
);
508 ZVAL_COPY(&proxy_obj
->parent
, object
);
509 RETVAL_OBJ(&proxy_obj
->zo
);
511 debug_propro(0, "dim_R pp", get_propro(object
), NULL
, offset
, return_value
);
515 zend_string_release(member
);
518 debug_propro(-1, type
== BP_VAR_R
? "dim_r" : "dim_R",
519 get_propro(object
), NULL
, offset
, return_value
);
524 static int has_dimension(zval
*object
, zval
*offset
, int check_empty
)
529 debug_propro(1, "dim_e", get_propro(object
), NULL
, offset
, NULL
);
532 value
= get_proxied_value(object
, &tmp
);
534 if (!Z_ISUNDEF_P(value
)) {
535 zend_string
*zs
= zval_get_string(offset
);
538 if (Z_TYPE_P(value
) == IS_ARRAY
) {
539 zval
*zentry
= zend_symtable_find(Z_ARRVAL_P(value
), zs
);
543 exists
= !Z_ISNULL_P(zentry
);
550 zend_string_release(zs
);
553 debug_propro(-1, "dim_e", get_propro(object
), NULL
, offset
, NULL
);
558 static void write_dimension(zval
*object
, zval
*offset
, zval
*input_value
)
561 zend_string
*zs
= NULL
;
564 debug_propro(1, "dim_w", get_propro(object
), NULL
, offset
, input_value
);
567 zs
= zval_get_string(offset
);
571 array
= get_proxied_value(object
, &tmp
);
572 separated
= separate_container(object
, array
);
573 set_container_value(array
, zs
, input_value
);
574 set_proxied_value(object
, array
);
575 cleanup_container(object
, array
, separated
);
578 zend_string_release(zs
);
581 debug_propro(-1, "dim_w", get_propro(object
), NULL
, offset
, input_value
);
584 static void unset_dimension(zval
*object
, zval
*offset
)
586 zval
*array
, *value
, tmp
;
588 debug_propro(1, "dim_u", get_propro(object
), NULL
, offset
, NULL
);
591 value
= get_proxied_value(object
, &tmp
);
595 if (Z_TYPE_P(array
) == IS_ARRAY
) {
596 zend_string
*o
= zval_get_string(offset
);
598 SEPARATE_ARRAY(array
);
599 zend_symtable_del(Z_ARRVAL_P(array
), o
);
601 set_proxied_value(object
, value
);
603 zend_string_release(o
);
606 debug_propro(-1, "dim_u", get_propro(object
), NULL
, offset
, NULL
);
609 ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct
, 0, 0, 2)
610 ZEND_ARG_INFO(0, object
)
611 ZEND_ARG_INFO(0, member
)
612 ZEND_ARG_OBJ_INFO(0, parent
, php
\\PropertyProxy
, 1)
614 static PHP_METHOD(propro
, __construct
) {
615 zend_error_handling zeh
;
616 zval
*reference
, *parent
= NULL
;
619 zend_replace_error_handling(EH_THROW
, NULL
, &zeh
);
620 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "o!S|O!",
621 &reference
, &member
, &parent
,
622 php_property_proxy_class_entry
)) {
623 php_property_proxy_object_t
*obj
;
625 obj
= get_propro(getThis());
628 ZVAL_COPY(&obj
->parent
, parent
);
629 obj
->proxy
= php_property_proxy_init(NULL
, member
);
630 } else if (reference
) {
631 zval
*container
= reference
;
633 obj
->proxy
= php_property_proxy_init(container
, member
);
635 php_error(E_WARNING
, "Either object or parent must be set");
638 zend_restore_error_handling(&zeh
);
641 static const zend_function_entry php_property_proxy_method_entry
[] = {
642 PHP_ME(propro
, __construct
, ai_propro_construct
, ZEND_ACC_PUBLIC
)
646 static PHP_MINIT_FUNCTION(propro
)
648 zend_class_entry ce
= {0};
650 INIT_NS_CLASS_ENTRY(ce
, "php", "PropertyProxy",
651 php_property_proxy_method_entry
);
652 php_property_proxy_class_entry
= zend_register_internal_class(&ce
);
653 php_property_proxy_class_entry
->create_object
= php_property_proxy_object_new
;
654 php_property_proxy_class_entry
->ce_flags
|= ZEND_ACC_FINAL
;
656 memcpy(&php_property_proxy_object_handlers
, zend_get_std_object_handlers(),
657 sizeof(zend_object_handlers
));
658 php_property_proxy_object_handlers
.offset
= XtOffsetOf(php_property_proxy_object_t
, zo
);
659 php_property_proxy_object_handlers
.free_obj
= destroy_obj
;
660 php_property_proxy_object_handlers
.set
= set_obj
;
661 php_property_proxy_object_handlers
.get
= get_obj
;
662 php_property_proxy_object_handlers
.cast_object
= cast_obj
;
663 php_property_proxy_object_handlers
.read_dimension
= read_dimension
;
664 php_property_proxy_object_handlers
.write_dimension
= write_dimension
;
665 php_property_proxy_object_handlers
.has_dimension
= has_dimension
;
666 php_property_proxy_object_handlers
.unset_dimension
= unset_dimension
;
671 PHP_MINFO_FUNCTION(propro
)
673 php_info_print_table_start();
674 php_info_print_table_header(2, "Property proxy support", "enabled");
675 php_info_print_table_row(2, "Extension version", PHP_PROPRO_VERSION
);
676 php_info_print_table_end();
679 static const zend_function_entry propro_functions
[] = {
683 zend_module_entry propro_module_entry
= {
684 STANDARD_MODULE_HEADER
,
693 STANDARD_MODULE_PROPERTIES
696 #ifdef COMPILE_DL_PROPRO
697 ZEND_GET_MODULE(propro
)
706 * vim600: noet sw=4 ts=4 fdm=marker
707 * vim<600: noet sw=4 ts=4