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
*container
, zend_bool separated
)
366 fprintf(stderr
, ">> cleanup(%s) %p rc=%d\n",
367 separated
|| (Z_REFCOUNTED_P(container
) && !Z_REFCOUNT_P(container
)) ? "yes" : "no",
368 Z_REFCOUNTED_P(container
) ? Z_COUNTED_P(container
) : container
,
369 Z_REFCOUNTED_P(container
) ? Z_REFCOUNT_P(container
) : -1);
372 zval_ptr_dtor(container
);
376 static inline zend_bool
separate_container(zval
*container
)
379 fprintf(stderr
, ">> separate is_ref=%d rc=%d\n", Z_ISREF_P(container
),
380 Z_REFCOUNTED_P(container
) ? Z_REFCOUNT_P(container
) : -1);
383 switch (Z_TYPE_P(container
)) {
388 if (Z_REFCOUNT_P(container
) > 1) {
389 SEPARATE_ZVAL(container
);
391 fprintf(stderr
, ">> array_dup %p\n", Z_COUNTED_P(container
));
398 array_init(container
);
400 fprintf(stderr
, ">> created %p\n", Z_COUNTED_P(container
));
405 SEPARATE_ZVAL(container
);
406 Z_TRY_ADDREF_P(container
);
407 convert_to_array(container
);
409 fprintf(stderr
, ">> converted %p\n", Z_COUNTED_P(container
));
415 fprintf(stderr
, ">> nothing %p\n", Z_COUNTED_P(container
));
420 static inline zval
*set_container_value(zval
*container
, zend_string
*member
, zval
*value
)
422 ZVAL_DEREF(container
);
423 switch (Z_TYPE_P(container
)) {
425 zend_update_property(Z_OBJCE_P(container
), container
,
426 member
->val
, member
->len
, value
);
430 Z_TRY_ADDREF_P(value
);
432 value
= zend_symtable_update(Z_ARRVAL_P(container
), member
, value
);
434 value
= zend_hash_next_index_insert(Z_ARRVAL_P(container
), value
);
447 static zval
*get_proxied_value(zval
*object
, zval
*return_value
)
449 php_property_proxy_object_t
*obj
= get_propro(object
);
451 debug_propro(1, "get", obj
, NULL
, NULL
, NULL
);
454 zval tmp
, *container
;
457 container
= get_container(object
, &tmp
);
459 return_value
= get_container_value(container
, obj
->proxy
->member
, return_value
);
462 debug_propro(-1, "get", obj
, NULL
, NULL
, return_value
);
467 static void set_proxied_value(zval
*object
, zval
*value
)
469 php_property_proxy_object_t
*obj
= get_propro(object
);
471 debug_propro(1, "set", obj
, NULL
, NULL
, value
);
474 zval tmp
, *container
;
477 Z_TRY_ADDREF_P(value
);
480 container
= get_container(object
, &tmp
);
481 separated
= separate_container(container
);
482 set_container_value(container
, obj
->proxy
->member
, value
);
483 set_container(object
, container
);
484 cleanup_container(container
, separated
);
486 Z_TRY_DELREF_P(value
);
488 debug_propro(0, "set", obj
, NULL
, NULL
, value
);
491 debug_propro(-1, "set", obj
, NULL
, NULL
, value
);
494 static zval
*read_dimension(zval
*object
, zval
*offset
, int type
, zval
*return_value
)
497 zend_string
*member
= offset
? zval_get_string(offset
) : NULL
;
499 debug_propro(1, type
== BP_VAR_R
? "dim_r" : "dim_R",
500 get_propro(object
), NULL
, offset
, NULL
);
503 value
= get_proxied_value(object
, &tmp
);
505 if (type
== BP_VAR_R
|| type
== BP_VAR_IS
) {
508 if (!Z_ISUNDEF_P(value
)) {
512 RETVAL_ZVAL(get_container_value(value
, member
, &tmp
), 1, 0);
515 php_property_proxy_t
*proxy
;
516 php_property_proxy_object_t
*proxy_obj
;
518 if (Z_ISUNDEF_P(value
)) {
523 if (Z_TYPE_P(value
) == IS_ARRAY
) {
524 member
= zend_long_to_str(zend_hash_next_free_element(
526 } else if (Z_TYPE_P(value
) != IS_OBJECT
){
527 member
= zend_long_to_str(0);
531 proxy
= php_property_proxy_init(NULL
, member
);
532 proxy_obj
= php_property_proxy_object_new_ex(NULL
, proxy
);
533 ZVAL_COPY(&proxy_obj
->parent
, object
);
534 RETVAL_OBJ(&proxy_obj
->zo
);
536 debug_propro(0, "dim_R pp", get_propro(object
), NULL
, offset
, return_value
);
540 zend_string_release(member
);
543 debug_propro(-1, type
== BP_VAR_R
? "dim_r" : "dim_R",
544 get_propro(object
), NULL
, offset
, return_value
);
549 static int has_dimension(zval
*object
, zval
*offset
, int check_empty
)
554 debug_propro(1, "dim_e", get_propro(object
), NULL
, offset
, NULL
);
557 value
= get_proxied_value(object
, &tmp
);
559 if (!Z_ISUNDEF_P(value
)) {
560 zend_string
*zs
= zval_get_string(offset
);
563 if (Z_TYPE_P(value
) == IS_ARRAY
) {
564 zval
*zentry
= zend_symtable_find(Z_ARRVAL_P(value
), zs
);
568 exists
= !Z_ISNULL_P(zentry
);
575 zend_string_release(zs
);
578 debug_propro(-1, "dim_e", get_propro(object
), NULL
, offset
, NULL
);
583 static void write_dimension(zval
*object
, zval
*offset
, zval
*input_value
)
586 zend_string
*zs
= NULL
;
589 debug_propro(1, "dim_w", get_propro(object
), NULL
, offset
, input_value
);
592 zs
= zval_get_string(offset
);
596 array
= get_proxied_value(object
, &tmp
);
597 separated
= separate_container(array
);
598 set_container_value(array
, zs
, input_value
);
599 set_proxied_value(object
, array
);
600 cleanup_container(array
, separated
);
603 zend_string_release(zs
);
606 debug_propro(-1, "dim_w", get_propro(object
), NULL
, offset
, input_value
);
609 static void unset_dimension(zval
*object
, zval
*offset
)
611 zval
*array
, *value
, tmp
;
613 debug_propro(1, "dim_u", get_propro(object
), NULL
, offset
, NULL
);
616 value
= get_proxied_value(object
, &tmp
);
620 if (Z_TYPE_P(array
) == IS_ARRAY
) {
621 zend_string
*o
= zval_get_string(offset
);
623 SEPARATE_ARRAY(array
);
624 zend_symtable_del(Z_ARRVAL_P(array
), o
);
626 set_proxied_value(object
, value
);
628 zend_string_release(o
);
631 debug_propro(-1, "dim_u", get_propro(object
), NULL
, offset
, NULL
);
634 ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct
, 0, 0, 2)
635 ZEND_ARG_INFO(0, object
)
636 ZEND_ARG_INFO(0, member
)
637 ZEND_ARG_OBJ_INFO(0, parent
, php
\\PropertyProxy
, 1)
639 static PHP_METHOD(propro
, __construct
) {
640 zend_error_handling zeh
;
641 zval
*reference
, *parent
= NULL
;
644 zend_replace_error_handling(EH_THROW
, NULL
, &zeh
);
645 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "o!S|O!",
646 &reference
, &member
, &parent
,
647 php_property_proxy_class_entry
)) {
648 php_property_proxy_object_t
*obj
;
650 obj
= get_propro(getThis());
653 ZVAL_COPY(&obj
->parent
, parent
);
654 obj
->proxy
= php_property_proxy_init(NULL
, member
);
655 } else if (reference
) {
656 zval
*container
= reference
;
658 obj
->proxy
= php_property_proxy_init(container
, member
);
660 php_error(E_WARNING
, "Either object or parent must be set");
663 zend_restore_error_handling(&zeh
);
666 static const zend_function_entry php_property_proxy_method_entry
[] = {
667 PHP_ME(propro
, __construct
, ai_propro_construct
, ZEND_ACC_PUBLIC
)
671 static PHP_MINIT_FUNCTION(propro
)
673 zend_class_entry ce
= {0};
675 INIT_NS_CLASS_ENTRY(ce
, "php", "PropertyProxy",
676 php_property_proxy_method_entry
);
677 php_property_proxy_class_entry
= zend_register_internal_class(&ce
);
678 php_property_proxy_class_entry
->create_object
= php_property_proxy_object_new
;
679 php_property_proxy_class_entry
->ce_flags
|= ZEND_ACC_FINAL
;
681 memcpy(&php_property_proxy_object_handlers
, zend_get_std_object_handlers(),
682 sizeof(zend_object_handlers
));
683 php_property_proxy_object_handlers
.offset
= XtOffsetOf(php_property_proxy_object_t
, zo
);
684 php_property_proxy_object_handlers
.free_obj
= destroy_obj
;
685 php_property_proxy_object_handlers
.set
= set_obj
;
686 php_property_proxy_object_handlers
.get
= get_obj
;
687 php_property_proxy_object_handlers
.cast_object
= cast_obj
;
688 php_property_proxy_object_handlers
.read_dimension
= read_dimension
;
689 php_property_proxy_object_handlers
.write_dimension
= write_dimension
;
690 php_property_proxy_object_handlers
.has_dimension
= has_dimension
;
691 php_property_proxy_object_handlers
.unset_dimension
= unset_dimension
;
696 PHP_MINFO_FUNCTION(propro
)
698 php_info_print_table_start();
699 php_info_print_table_header(2, "Property proxy support", "enabled");
700 php_info_print_table_row(2, "Extension version", PHP_PROPRO_VERSION
);
701 php_info_print_table_end();
704 static const zend_function_entry propro_functions
[] = {
708 zend_module_entry propro_module_entry
= {
709 STANDARD_MODULE_HEADER
,
718 STANDARD_MODULE_PROPERTIES
721 #ifdef COMPILE_DL_PROPRO
722 ZEND_GET_MODULE(propro
)
731 * vim600: noet sw=4 ts=4 fdm=marker
732 * vim<600: noet sw=4 ts=4