Skip to content
This repository was archived by the owner on Jan 12, 2021. It is now read-only.

Commit 22b0ff5

Browse files
authored
Added printDebugString operation (#75)
1 parent cd41276 commit 22b0ff5

File tree

3 files changed

+164
-59
lines changed

3 files changed

+164
-59
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ PHP Protobuf is Google's Protocol Buffers implementation for PHP with a goal to
9494
}
9595
```
9696
97+
Alternatively you can use `printDebugString()` method which produces output in protocol buffers text format.
98+
9799
1. If you would like you can reset an object to its initial state
98100
99101
```php

protobuf.c

Lines changed: 147 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#define PB_FIELDS_METHOD "fields"
3535
#define PB_PARSE_FROM_STRING_METHOD "parseFromString"
3636
#define PB_SERIALIZE_TO_STRING_METHOD "serializeToString"
37+
#define PB_PRINT_DEBUG_STRING_METHOD "printDebugString"
3738

3839
#define PB_FIELD_NAME "name"
3940
#define PB_FIELD_PACKED "packed"
@@ -58,7 +59,9 @@ enum
5859
zend_class_entry *pb_entry;
5960

6061
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);
6163
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);
6265
static zval **pb_get_field_type(zval *this, zval **field_descriptors, uint32_t field_number);
6366
static zval **pb_get_field_descriptor(zval *this, zval *field_descriptors, uint32_t field_number);
6467
static zval *pb_get_field_descriptors(zval *this);
@@ -137,6 +140,80 @@ PHP_METHOD(ProtobufMessage, clear)
137140
RETURN_THIS();
138141
}
139142

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+
140217
PHP_METHOD(ProtobufMessage, dump)
141218
{
142219
zend_bool only_set = 1;
@@ -521,9 +598,13 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_clear, 0, 0, 1)
521598
ZEND_ARG_INFO(0, position)
522599
ZEND_END_ARG_INFO()
523600

601+
ZEND_BEGIN_ARG_INFO_EX(arginfo_printDebugString, 0, 0, 0)
602+
ZEND_ARG_INFO(0, level)
603+
ZEND_END_ARG_INFO()
604+
524605
ZEND_BEGIN_ARG_INFO_EX(arginfo_dump, 0, 0, 0)
525606
ZEND_ARG_INFO(0, onlySet)
526-
ZEND_ARG_INFO(0, indendation)
607+
ZEND_ARG_INFO(0, level)
527608
ZEND_END_ARG_INFO()
528609

529610
ZEND_BEGIN_ARG_INFO_EX(arginfo_count, 0, 0, 1)
@@ -546,7 +627,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_set, 0, 0, 2)
546627
ZEND_ARG_INFO(0, value)
547628
ZEND_END_ARG_INFO()
548629

549-
zend_function_entry pb_methods[] = {
630+
static zend_function_entry pb_methods[] = {
550631
PHP_ME(ProtobufMessage, __construct, arginfo_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
551632
PHP_ABSTRACT_ME(ProtobufMessage, reset, arginfo_reset)
552633
PHP_ME(ProtobufMessage, append, arginfo_append, ZEND_ACC_PUBLIC)
@@ -557,6 +638,7 @@ zend_function_entry pb_methods[] = {
557638
PHP_ME(ProtobufMessage, parseFromString, arginfo_parseFromString, ZEND_ACC_PUBLIC)
558639
PHP_ME(ProtobufMessage, serializeToString, arginfo_serializeToString, ZEND_ACC_PUBLIC)
559640
PHP_ME(ProtobufMessage, set, arginfo_set, ZEND_ACC_PUBLIC)
641+
PHP_ME(ProtobufMessage, printDebugString, arginfo_printDebugString, ZEND_ACC_PUBLIC)
560642
{NULL, NULL, NULL, 0, 0}
561643
};
562644

@@ -667,9 +749,70 @@ static int pb_assign_value(zval *this, zval *dst, zval *src, uint32_t field_numb
667749
return -1;
668750
}
669751

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)
671753
{
672754
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+
{
673816
zval tmp, ret, arg0, arg1, *args[2];
674817
TSRMLS_FETCH();
675818

@@ -697,30 +840,9 @@ static int pb_dump_field_value(zval **value, long level, zend_bool only_set)
697840
return -1;
698841
else
699842
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);
714843
}
715844

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);
724846
}
725847

726848
static zval **pb_get_field_descriptor(zval *this, zval *field_descriptors, uint32_t field_number)

stubs/ProtobufMessage.php

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,52 +26,42 @@ abstract public function reset();
2626
*
2727
* @return null
2828
*/
29-
public function append($position, $value)
30-
{
31-
32-
}
29+
public function append($position, $value) {}
3330

3431
/**
3532
* @param int $position
3633
*
3734
* @return null
3835
*/
39-
public function clear($position)
40-
{
41-
42-
}
36+
public function clear($position) {}
4337

4438
/**
4539
* @param bool $onlySet
46-
* @param int $indentation
4740
*
48-
* @return string
41+
* @return null
42+
*/
43+
public function dump($onlySet = true) {}
44+
45+
/**
46+
*
47+
* @return null
4948
*/
50-
public function dump($onlySet = true, $indentation = 0)
51-
{
52-
53-
}
49+
public function printDebugString() {}
5450

5551
/**
5652
* @param int $position
5753
*
5854
* @return int
5955
*/
60-
public function count($position)
61-
{
62-
63-
}
56+
public function count($position) {}
6457

6558
/**
6659
* @param int $position
6760
* @param int|null $offset
6861
*
6962
* @return mixed
7063
*/
71-
public function get($position = -1, $offset = null)
72-
{
73-
74-
}
64+
public function get($position = -1, $offset = null) {}
7565

7666
/**
7767
* @param string $packed
@@ -80,29 +70,20 @@ public function get($position = -1, $offset = null)
8070
*
8171
* @return mixed
8272
*/
83-
public function parseFromString($packed)
84-
{
85-
86-
}
73+
public function parseFromString($packed) {}
8774

8875
/**
8976
* @throws Exception
9077
*
9178
* @return string
9279
*/
93-
public function serializeToString()
94-
{
95-
96-
}
80+
public function serializeToString() {}
9781

9882
/**
9983
* @param int $position
10084
* @param mixed $value
10185
*
10286
* @return null
10387
*/
104-
public function set($position = -1, $value)
105-
{
106-
107-
}
88+
public function set($position = -1, $value) {}
10889
}

0 commit comments

Comments
 (0)