34
34
#define PB_FIELDS_METHOD "fields"
35
35
#define PB_PARSE_FROM_STRING_METHOD "parseFromString"
36
36
#define PB_SERIALIZE_TO_STRING_METHOD "serializeToString"
37
+ #define PB_PRINT_DEBUG_STRING_METHOD "printDebugString"
37
38
38
39
#define PB_FIELD_NAME "name"
39
40
#define PB_FIELD_PACKED "packed"
58
59
zend_class_entry * pb_entry ;
59
60
60
61
static int pb_assign_value (zval * this , zval * dst , zval * src , uint32_t field_number );
62
+ static int pb_print_field_value (zval * * value , long level , zend_bool only_set );
61
63
static int pb_dump_field_value (zval * * value , long level , zend_bool only_set );
64
+ static int pb_print_debug_field_value (zval * * value , long level );
62
65
static zval * * pb_get_field_type (zval * this , zval * * field_descriptors , uint32_t field_number );
63
66
static zval * * pb_get_field_descriptor (zval * this , zval * field_descriptors , uint32_t field_number );
64
67
static zval * pb_get_field_descriptors (zval * this );
@@ -137,6 +140,80 @@ PHP_METHOD(ProtobufMessage, clear)
137
140
RETURN_THIS ();
138
141
}
139
142
143
+ PHP_METHOD (ProtobufMessage , printDebugString )
144
+ {
145
+ int indent ;
146
+ char indent_char ;
147
+ long level = 0 ;
148
+ const char * field_name ;
149
+ ulong field_number , index ;
150
+ HashPosition i , j ;
151
+ zval * * field_descriptor , * field_descriptors , * * val , * * value , * * values ;
152
+
153
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "|l" , & level ) == FAILURE || level < 0 ) {
154
+ return ;
155
+ }
156
+
157
+ indent = ((int )level ) * 2 ;
158
+ indent_char = indent ? ' ' : '\0' ;
159
+
160
+ if ((field_descriptors = pb_get_field_descriptors (getThis ())) == NULL )
161
+ return ;
162
+
163
+ if ((values = pb_get_values (getThis ())) == NULL )
164
+ return ;
165
+
166
+ PB_FOREACH (& i , Z_ARRVAL_PP (values )) {
167
+ zend_hash_get_current_key_ex (Z_ARRVAL_PP (values ), NULL , NULL , & field_number , 0 , & i );
168
+ zend_hash_get_current_data_ex (Z_ARRVAL_PP (values ), (void * * ) & value , & i );
169
+
170
+ if ((field_descriptor = pb_get_field_descriptor (getThis (), field_descriptors , field_number )) == NULL )
171
+ return ;
172
+
173
+ if ((field_name = pb_get_field_name (getThis (), field_number )) == NULL )
174
+ return ;
175
+
176
+ if (Z_TYPE_PP (value ) == IS_ARRAY ) {
177
+ if (zend_hash_num_elements (Z_ARRVAL_PP (value )) == 0 )
178
+ continue ;
179
+
180
+ zval * * field_type ;
181
+ if ((field_type = pb_get_field_type (getThis (), field_descriptor , field_number )) == NULL )
182
+ return ;
183
+
184
+ int wire = pb_get_wire_type (Z_LVAL_PP (field_type ));
185
+
186
+ PB_FOREACH (& j , Z_ARRVAL_PP (value )) {
187
+ zend_hash_get_current_key_ex (Z_ARRVAL_PP (value ), NULL , NULL , & index , 0 , & j );
188
+ zend_hash_get_current_data_ex (Z_ARRVAL_PP (value ), (void * * ) & val , & j );
189
+
190
+ if (Z_TYPE_PP (value ) == IS_NULL )
191
+ continue ;
192
+
193
+ if ((wire == WIRE_TYPE_LENGTH_DELIMITED && Z_TYPE_PP (val ) != IS_STRING ) || wire == -1 ) {
194
+ php_printf ("%*c%s {" , indent , indent_char , field_name );
195
+ if (pb_print_debug_field_value (val , level + 1 ) != 0 )
196
+ return ;
197
+ php_printf ("%*c}\n" , indent , indent_char );
198
+ } else {
199
+ php_printf ("%*c%s:" , indent , indent_char , field_name );
200
+ if (pb_print_debug_field_value (val , level + 1 ) != 0 )
201
+ return ;
202
+ }
203
+ }
204
+ } else if (Z_TYPE_PP (value ) == IS_OBJECT ) {
205
+ php_printf ("%*c%s {" , indent , indent_char , field_name );
206
+ if (pb_print_debug_field_value (value , level + 1 ) != 0 )
207
+ return ;
208
+ php_printf ("%*c}\n" , indent , indent_char );
209
+ } else if (Z_TYPE_PP (value ) != IS_NULL ) {
210
+ php_printf ("%*c%s:" , indent , indent_char , field_name );
211
+ if (pb_print_debug_field_value (value , level + 1 ) != 0 )
212
+ return ;
213
+ }
214
+ }
215
+ }
216
+
140
217
PHP_METHOD (ProtobufMessage , dump )
141
218
{
142
219
zend_bool only_set = 1 ;
@@ -521,9 +598,13 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_clear, 0, 0, 1)
521
598
ZEND_ARG_INFO (0 , position )
522
599
ZEND_END_ARG_INFO ()
523
600
601
+ ZEND_BEGIN_ARG_INFO_EX (arginfo_printDebugString , 0 , 0 , 0 )
602
+ ZEND_ARG_INFO (0 , level )
603
+ ZEND_END_ARG_INFO ()
604
+
524
605
ZEND_BEGIN_ARG_INFO_EX (arginfo_dump , 0 , 0 , 0 )
525
606
ZEND_ARG_INFO (0 , onlySet )
526
- ZEND_ARG_INFO (0 , indendation )
607
+ ZEND_ARG_INFO (0 , level )
527
608
ZEND_END_ARG_INFO ()
528
609
529
610
ZEND_BEGIN_ARG_INFO_EX (arginfo_count , 0 , 0 , 1 )
@@ -546,7 +627,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_set, 0, 0, 2)
546
627
ZEND_ARG_INFO (0 , value )
547
628
ZEND_END_ARG_INFO ()
548
629
549
- zend_function_entry pb_methods [] = {
630
+ static zend_function_entry pb_methods [] = {
550
631
PHP_ME (ProtobufMessage , __construct , arginfo_construct , ZEND_ACC_PUBLIC | ZEND_ACC_CTOR )
551
632
PHP_ABSTRACT_ME (ProtobufMessage , reset , arginfo_reset )
552
633
PHP_ME (ProtobufMessage , append , arginfo_append , ZEND_ACC_PUBLIC )
@@ -557,6 +638,7 @@ zend_function_entry pb_methods[] = {
557
638
PHP_ME (ProtobufMessage , parseFromString , arginfo_parseFromString , ZEND_ACC_PUBLIC )
558
639
PHP_ME (ProtobufMessage , serializeToString , arginfo_serializeToString , ZEND_ACC_PUBLIC )
559
640
PHP_ME (ProtobufMessage , set , arginfo_set , ZEND_ACC_PUBLIC )
641
+ PHP_ME (ProtobufMessage , printDebugString , arginfo_printDebugString , ZEND_ACC_PUBLIC )
560
642
{NULL , NULL , NULL , 0 , 0 }
561
643
};
562
644
@@ -667,9 +749,70 @@ static int pb_assign_value(zval *this, zval *dst, zval *src, uint32_t field_numb
667
749
return -1 ;
668
750
}
669
751
670
- static int pb_dump_field_value (zval * * value , long level , zend_bool only_set )
752
+ static int pb_print_field_value (zval * * value , long level , zend_bool only_set )
671
753
{
672
754
const char * string_value ;
755
+ zval tmp ;
756
+ TSRMLS_FETCH ();
757
+
758
+ INIT_ZVAL (tmp );
759
+
760
+ if (Z_TYPE_PP (value ) == IS_NULL )
761
+ string_value = "null" ;
762
+ else if (Z_TYPE_PP (value ) == IS_BOOL ) {
763
+ if (Z_BVAL_PP (value ) )
764
+ string_value = "true" ;
765
+ else
766
+ string_value = "false" ;
767
+ } else {
768
+ tmp = * * value ;
769
+ zval_copy_ctor (& tmp );
770
+ Z_SET_REFCOUNT (tmp , 1 );
771
+ Z_UNSET_ISREF (tmp );
772
+ convert_to_string (& tmp );
773
+ string_value = Z_STRVAL (tmp );
774
+ }
775
+
776
+ if (Z_TYPE_PP (value ) == IS_STRING )
777
+ php_printf (" \"%s\"\n" , string_value );
778
+ else
779
+ php_printf (" %s\n" , string_value );
780
+
781
+ zval_dtor (& tmp );
782
+
783
+ return 0 ;
784
+ }
785
+
786
+ static int pb_print_debug_field_value (zval * * value , long level )
787
+ {
788
+ zval tmp , ret , arg0 , * args [1 ];
789
+ TSRMLS_FETCH ();
790
+
791
+ INIT_ZVAL (tmp );
792
+
793
+ if (Z_TYPE_PP (value ) == IS_OBJECT ) {
794
+ php_printf ("\n" );
795
+
796
+ INIT_ZVAL (arg0 );
797
+ Z_TYPE (arg0 ) = IS_LONG ;
798
+ Z_LVAL (arg0 ) = level ;
799
+ Z_ADDREF (arg0 );
800
+
801
+ args [0 ] = & arg0 ;
802
+
803
+ ZVAL_STRING (& tmp , PB_PRINT_DEBUG_STRING_METHOD , 0 );
804
+
805
+ if (call_user_function (NULL , value , & tmp , & ret , 1 , args TSRMLS_CC ) == FAILURE )
806
+ return -1 ;
807
+ else
808
+ return 0 ;
809
+ }
810
+
811
+ return pb_print_field_value (value , level , 1 );
812
+ }
813
+
814
+ static int pb_dump_field_value (zval * * value , long level , zend_bool only_set )
815
+ {
673
816
zval tmp , ret , arg0 , arg1 , * args [2 ];
674
817
TSRMLS_FETCH ();
675
818
@@ -697,30 +840,9 @@ static int pb_dump_field_value(zval **value, long level, zend_bool only_set)
697
840
return -1 ;
698
841
else
699
842
return 0 ;
700
- } else if (Z_TYPE_PP (value ) == IS_NULL )
701
- string_value = "null (not set)" ;
702
- else if (Z_TYPE_PP (value ) == IS_BOOL ) {
703
- if (Z_BVAL_PP (value ) )
704
- string_value = "true" ;
705
- else
706
- string_value = "false" ;
707
- } else {
708
- tmp = * * value ;
709
- zval_copy_ctor (& tmp );
710
- Z_SET_REFCOUNT (tmp , 1 );
711
- Z_UNSET_ISREF (tmp );
712
- convert_to_string (& tmp );
713
- string_value = Z_STRVAL (tmp );
714
843
}
715
844
716
- if (Z_TYPE_PP (value ) == IS_STRING )
717
- php_printf (" '%s'\n" , string_value );
718
- else
719
- php_printf (" %s\n" , string_value );
720
-
721
- zval_dtor (& tmp );
722
-
723
- return 0 ;
845
+ return pb_print_field_value (value , level , only_set );
724
846
}
725
847
726
848
static zval * * pb_get_field_descriptor (zval * this , zval * field_descriptors , uint32_t field_number )
0 commit comments