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 int _walk(php_property_proxy_object_t
*obj
)
63 if (!Z_ISUNDEF(obj
->parent
)) {
64 p
+= _walk(get_propro(&obj
->parent
));
67 p
+= fprintf(stderr
, ".%s", obj
->proxy
->member
->val
);
74 static void debug_propro(int inout
, const char *f
,
75 php_property_proxy_object_t
*obj
,
76 php_property_proxy_t
*proxy
,
77 zval
*offset
, zval
*value
)
85 fprintf(stderr
, "#PP %14p %*c %s %s\t", proxy
, level
, ' ', inoutstr
[inout
+ 1], f
);
90 fprintf(stderr
, " container= %-10p <%12s rc=%d ",
91 Z_REFCOUNTED(proxy
->container
) ? Z_COUNTED(proxy
->container
) : NULL
,
92 types
[Z_TYPE(proxy
->container
)],
93 Z_REFCOUNTED(proxy
->container
) ? Z_REFCOUNT(proxy
->container
) : 0);
94 if (Z_ISREF(proxy
->container
)) {
95 zval
*ref
= Z_REFVAL(proxy
->container
);
96 fprintf(stderr
, " %-12s %p rc=% 2d",
99 Z_REFCOUNTED_P(ref
) ? Z_REFCOUNT_P(ref
) : -1);
101 fprintf(stderr
, "> ");
110 char *offset_str
= "[]";
114 convert_to_string_ex(o
);
115 offset_str
= Z_STRVAL_P(o
);
118 width
+= fprintf(stderr
, ".%s", offset_str
);
120 if (o
&& o
!= offset
) {
125 fprintf(stderr
, "%.*s", 32-width
, " ");
126 if (Z_ISUNDEF_P(value
)) {
127 fprintf(stderr
, " =UNDEF ");
129 fprintf(stderr
, " = %-10p <%12s rc=%d %3s> ",
130 Z_REFCOUNTED_P(value
) ? Z_COUNTED_P(value
) : NULL
,
131 types
[Z_TYPE_P(value
)&0xf],
132 Z_REFCOUNTED_P(value
) ? Z_REFCOUNT_P(value
) : 0,
133 Z_ISREF_P(value
) ? "ref" : "");
134 if (!Z_ISUNDEF_P(value
) && Z_TYPE_P(value
) != IS_INDIRECT
) {
135 zend_print_flat_zval_r(value TSRMLS_CC
);
140 fprintf(stderr
, "\n");
143 #define debug_propro(l, f, obj, proxy, off, val)
146 php_property_proxy_t
*php_property_proxy_init(zval
*container
, zend_string
*member
)
148 php_property_proxy_t
*proxy
= ecalloc(1, sizeof(*proxy
));
152 ZVAL_STR_COPY(&offset
, member
);
155 debug_propro(1, "init", NULL
, proxy
, &offset
, NULL
);
157 ZVAL_COPY(&proxy
->container
, container
);
158 proxy
->member
= zend_string_copy(member
);
160 debug_propro(-1, "init", NULL
, proxy
, &offset
, NULL
);
169 void php_property_proxy_free(php_property_proxy_t
**proxy
)
174 ZVAL_STR_COPY(&offset
, (*proxy
)->member
);
175 debug_propro(1, "dtor", NULL
, *proxy
, &offset
, NULL
);
179 zval_ptr_dtor(&(*proxy
)->container
);
181 ZVAL_UNDEF(&(*proxy
)->container
);
182 zend_string_release((*proxy
)->member
);
183 (*proxy
)->member
= NULL
;
189 debug_propro(-1, "dtor", NULL
, NULL
, &offset
, NULL
);
195 static zend_class_entry
*php_property_proxy_class_entry
;
196 static zend_object_handlers php_property_proxy_object_handlers
;
198 zend_class_entry
*php_property_proxy_get_class_entry(void)
200 return php_property_proxy_class_entry
;
203 php_property_proxy_object_t
*php_property_proxy_object_new_ex(
204 zend_class_entry
*ce
, php_property_proxy_t
*proxy
)
206 php_property_proxy_object_t
*o
;
209 ce
= php_property_proxy_class_entry
;
212 o
= ecalloc(1, sizeof(*o
) + sizeof(zval
) * (ce
->default_properties_count
- 1));
213 zend_object_std_init(&o
->zo
, ce
);
214 object_properties_init(&o
->zo
, ce
);
217 o
->zo
.handlers
= &php_property_proxy_object_handlers
;
222 zend_object
*php_property_proxy_object_new(zend_class_entry
*ce
)
224 return &php_property_proxy_object_new_ex(ce
, NULL
)->zo
;
227 static void destroy_obj(zend_object
*object
)
229 php_property_proxy_object_t
*o
= PHP_PROPRO_PTR(object
);
232 php_property_proxy_free(&o
->proxy
);
234 if (!Z_ISUNDEF(o
->parent
)) {
235 zval_ptr_dtor(&o
->parent
);
236 ZVAL_UNDEF(&o
->parent
);
238 zend_object_std_dtor(object
);
241 static ZEND_RESULT_CODE
cast_obj(zval
*object
, zval
*return_value
, int type
)
246 RETVAL_ZVAL(get_proxied_value(object
, &tmp
), 1, 0);
248 debug_propro(0, "cast", get_propro(object
), NULL
, NULL
, return_value
);
250 if (!Z_ISUNDEF_P(return_value
)) {
251 ZVAL_DEREF(return_value
);
252 convert_to_explicit_type_ex(return_value
, type
);
259 static zval
*get_obj(zval
*object
, zval
*return_value
)
264 RETVAL_ZVAL(get_proxied_value(object
, &tmp
), 1, 0);
268 static void set_obj(zval
*object
, zval
*value
) {
269 set_proxied_value(object
, value
);
272 static inline php_property_proxy_object_t
*get_propro(zval
*object
)
274 ZEND_ASSERT(Z_TYPE_P(object
) == IS_OBJECT
);
275 return PHP_PROPRO_PTR(Z_OBJ_P(object
));
278 static inline zval
*get_container(zval
*object
)
280 php_property_proxy_object_t
*obj
= get_propro(object
);
282 if (!Z_ISUNDEF(obj
->parent
)) {
283 zval
*parent_value
, tmp
;
286 parent_value
= get_proxied_value(&obj
->parent
, &tmp
);
287 ZVAL_DEREF(parent_value
);
288 switch (Z_TYPE_P(parent_value
)) {
291 zend_assign_to_variable(&obj
->proxy
->container
, parent_value
, IS_CV
);
298 return &obj
->proxy
->container
;
301 static inline void set_container(zval
*object
, zval
*container
)
303 php_property_proxy_object_t
*obj
= get_propro(object
);
304 zend_assign_to_variable(&obj
->proxy
->container
, container
, IS_CV
);
306 if (!Z_ISUNDEF(obj
->parent
)) {
307 set_proxied_value(&obj
->parent
, &obj
->proxy
->container
);
311 static inline zval
*get_container_value(zval
*container
, zend_string
*member
, zval
*return_value
)
313 zval
*found_value
= NULL
, prop_tmp
;
315 ZVAL_DEREF(container
);
316 switch (Z_TYPE_P(container
)) {
318 found_value
= zend_read_property(Z_OBJCE_P(container
), container
,
319 member
->val
, member
->len
, 0, &prop_tmp
);
324 found_value
= zend_symtable_find(Z_ARRVAL_P(container
), member
);
329 RETVAL_ZVAL(found_value
, 0, 0);
335 static inline void set_container_value(zval
*container
, zend_string
*member
, zval
*value
)
337 ZVAL_DEREF(container
);
338 switch (Z_TYPE_P(container
)) {
340 zend_update_property(Z_OBJCE_P(container
), container
,
341 member
->val
, member
->len
, value
);
345 array_init(container
);
348 SEPARATE_ZVAL(container
);
349 convert_to_array(container
);
352 SEPARATE_ARRAY(container
);
353 Z_TRY_ADDREF_P(value
);
355 zend_symtable_update(Z_ARRVAL_P(container
), member
, value
);
357 zend_hash_next_index_insert(Z_ARRVAL_P(container
), value
);
364 static zval
*get_proxied_value(zval
*object
, zval
*return_value
)
366 php_property_proxy_object_t
*obj
= get_propro(object
);
368 debug_propro(1, "get", obj
, NULL
, NULL
, NULL
);
371 zval
*container
= get_container(object
);
373 return_value
= get_container_value(container
, obj
->proxy
->member
, return_value
);
376 debug_propro(-1, "get", obj
, NULL
, NULL
, return_value
);
381 static void set_proxied_value(zval
*object
, zval
*value
)
383 php_property_proxy_object_t
*obj
= get_propro(object
);
385 debug_propro(1, "set", obj
, NULL
, NULL
, value
);
388 zval
*container
= get_container(object
);
390 set_container_value(container
, obj
->proxy
->member
, value
);
391 set_container(object
, container
);
393 debug_propro(0, "set", obj
, NULL
, NULL
, value
);
396 debug_propro(-1, "set", obj
, NULL
, NULL
, value
);
399 static zval
*read_dimension(zval
*object
, zval
*offset
, int type
, zval
*return_value
)
402 zend_string
*member
= offset
? zval_get_string(offset
) : NULL
;
404 debug_propro(1, type
== BP_VAR_R
? "dim_r" : "dim_R",
405 get_propro(object
), NULL
, offset
, NULL
);
408 value
= get_proxied_value(object
, &tmp
);
410 if (type
== BP_VAR_R
) {
413 if (!Z_ISUNDEF_P(value
)) {
417 RETVAL_ZVAL(get_container_value(value
, member
, &tmp
), 1, 0);
420 php_property_proxy_t
*proxy
;
421 php_property_proxy_object_t
*proxy_obj
;
423 zend_bool created
= Z_ISUNDEF_P(value
);
426 if (Z_REFCOUNTED_P(array
) && Z_REFCOUNT_P(array
) > 1) {
428 SEPARATE_ZVAL_NOREF(array
);
430 if (Z_TYPE_P(array
) != IS_ARRAY
) {
432 if (Z_ISUNDEF_P(array
)) {
435 convert_to_array(array
);
440 member
= zend_long_to_str(zend_hash_next_free_element(
444 proxy
= php_property_proxy_init(value
, member
);
445 proxy_obj
= php_property_proxy_object_new_ex(NULL
, proxy
);
446 ZVAL_COPY(&proxy_obj
->parent
, object
);
447 RETVAL_OBJ(&proxy_obj
->zo
);
453 debug_propro(0, created
? "dim_R pp C" : "dim_R pp", get_propro(object
), NULL
, offset
, return_value
);
457 zend_string_release(member
);
460 debug_propro(-1, type
== BP_VAR_R
? "dim_r" : "dim_R",
461 get_propro(object
), NULL
, offset
, return_value
);
466 static int has_dimension(zval
*object
, zval
*offset
, int check_empty
)
471 debug_propro(1, "dim_e", get_propro(object
), NULL
, offset
, NULL
);
474 value
= get_proxied_value(object
, &tmp
);
477 if (!Z_ISUNDEF_P(value
)) {
478 zend_string
*zs
= zval_get_string(offset
);
481 if (Z_TYPE_P(value
) == IS_ARRAY
) {
482 zval
*zentry
= zend_symtable_find(Z_ARRVAL_P(value
), zs
);
488 exists
= !Z_ISNULL_P(zentry
);
495 zend_string_release(zs
);
498 debug_propro(-1, "dim_e", get_propro(object
), NULL
, offset
, NULL
);
503 static void write_dimension(zval
*object
, zval
*offset
, zval
*input_value
)
506 zend_string
*zs
= NULL
;
508 debug_propro(1, "dim_w", get_propro(object
), NULL
, offset
, input_value
);
511 array
= get_proxied_value(object
, &tmp
);
514 zs
= zval_get_string(offset
);
516 set_container_value(array
, zs
, input_value
);
518 zend_string_release(zs
);
521 set_proxied_value(object
, array
);
523 debug_propro(-1, "dim_w", get_propro(object
), NULL
, offset
, input_value
);
526 static void unset_dimension(zval
*object
, zval
*offset
)
528 zval
*array
, *value
, tmp
;
530 debug_propro(1, "dim_u", get_propro(object
), NULL
, offset
, NULL
);
533 value
= get_proxied_value(object
, &tmp
);
537 if (Z_TYPE_P(array
) == IS_ARRAY
) {
538 zend_string
*o
= zval_get_string(offset
);
540 SEPARATE_ARRAY(array
);
541 zend_symtable_del(Z_ARRVAL_P(array
), o
);
543 set_proxied_value(object
, value
);
545 zend_string_release(o
);
548 debug_propro(-1, "dim_u", get_propro(object
), NULL
, offset
, NULL
);
551 ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct
, 0, 0, 2)
552 ZEND_ARG_INFO(1, object
)
553 ZEND_ARG_INFO(0, member
)
554 ZEND_ARG_OBJ_INFO(0, parent
, php
\\PropertyProxy
, 1)
556 static PHP_METHOD(propro
, __construct
) {
557 zend_error_handling zeh
;
558 zval
*reference
, *container
, *parent
= NULL
;
561 zend_replace_error_handling(EH_THROW
, NULL
, &zeh
);
562 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "zS|O!",
563 &reference
, &member
, &parent
,
564 php_property_proxy_class_entry
)) {
565 php_property_proxy_object_t
*obj
;
567 container
= reference
;
568 ZVAL_DEREF(container
);
569 switch (Z_TYPE_P(container
)) {
571 SEPARATE_ARRAY(container
);
576 SEPARATE_ZVAL(container
);
577 convert_to_array(container
);
581 obj
= get_propro(getThis());
582 obj
->proxy
= php_property_proxy_init(reference
, member
);
585 ZVAL_COPY(&obj
->parent
, parent
);
588 zend_restore_error_handling(&zeh
);
591 static const zend_function_entry php_property_proxy_method_entry
[] = {
592 PHP_ME(propro
, __construct
, ai_propro_construct
, ZEND_ACC_PUBLIC
)
596 static PHP_MINIT_FUNCTION(propro
)
598 zend_class_entry ce
= {0};
600 INIT_NS_CLASS_ENTRY(ce
, "php", "PropertyProxy",
601 php_property_proxy_method_entry
);
602 php_property_proxy_class_entry
= zend_register_internal_class(&ce
);
603 php_property_proxy_class_entry
->create_object
= php_property_proxy_object_new
;
604 php_property_proxy_class_entry
->ce_flags
|= ZEND_ACC_FINAL
;
606 memcpy(&php_property_proxy_object_handlers
, zend_get_std_object_handlers(),
607 sizeof(zend_object_handlers
));
608 php_property_proxy_object_handlers
.offset
= XtOffsetOf(php_property_proxy_object_t
, zo
);
609 php_property_proxy_object_handlers
.free_obj
= destroy_obj
;
610 php_property_proxy_object_handlers
.set
= set_obj
;
611 php_property_proxy_object_handlers
.get
= get_obj
;
612 php_property_proxy_object_handlers
.cast_object
= cast_obj
;
613 php_property_proxy_object_handlers
.read_dimension
= read_dimension
;
614 php_property_proxy_object_handlers
.write_dimension
= write_dimension
;
615 php_property_proxy_object_handlers
.has_dimension
= has_dimension
;
616 php_property_proxy_object_handlers
.unset_dimension
= unset_dimension
;
621 PHP_MINFO_FUNCTION(propro
)
623 php_info_print_table_start();
624 php_info_print_table_header(2, "Property proxy support", "enabled");
625 php_info_print_table_row(2, "Extension version", PHP_PROPRO_VERSION
);
626 php_info_print_table_end();
629 static const zend_function_entry propro_functions
[] = {
633 zend_module_entry propro_module_entry
= {
634 STANDARD_MODULE_HEADER
,
643 STANDARD_MODULE_PROPERTIES
646 #ifdef COMPILE_DL_PROPRO
647 ZEND_GET_MODULE(propro
)
656 * vim600: noet sw=4 ts=4 fdm=marker
657 * vim<600: noet sw=4 ts=4