Skip to content

Commit

Permalink
Add json encode/decode for php. (protocolbuffers#3226)
Browse files Browse the repository at this point in the history
* Add json encode/decode for php.

* Fix php conformance test on 32-bit machines.

* Fix conformance test for c extension.

* Fix comments
  • Loading branch information
TeBoring authored Jun 30, 2017
1 parent 5a52b35 commit ecca6ea
Show file tree
Hide file tree
Showing 36 changed files with 3,118 additions and 2,274 deletions.
92 changes: 48 additions & 44 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -597,60 +597,63 @@ php_EXTRA_DIST= \
php/ext/google/protobuf/upb.c \
php/ext/google/protobuf/protobuf.c \
php/src/phpdoc.dist.xml \
php/src/Google/Protobuf/Internal/Descriptor.php \
php/src/Google/Protobuf/Internal/CodedInputStream.php \
php/src/Google/Protobuf/Internal/CodedOutputStream.php \
php/src/Google/Protobuf/Internal/DescriptorPool.php \
php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php \
php/src/Google/Protobuf/Internal/OneofField.php \
php/src/Google/Protobuf/Internal/MessageOptions.php \
php/src/Google/Protobuf/Internal/FileDescriptor.php \
php/src/Google/Protobuf/Internal/FileDescriptorProto.php \
php/src/Google/Protobuf/Internal/MapEntry.php \
php/src/Google/Protobuf/Internal/FieldDescriptor.php \
php/src/Google/Protobuf/Internal/FieldDescriptorProto.php \
php/src/Google/Protobuf/Internal/InputStream.php \
php/src/Google/Protobuf/Internal/UninterpretedOption.php \
php/src/Google/Protobuf/Internal/ServiceOptions.php \
php/src/Google/Protobuf/Internal/MethodOptions_IdempotencyLevel.php \
php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php \
php/src/Google/Protobuf/Internal/OneofDescriptor.php \
php/src/Google/Protobuf/Internal/OneofDescriptorProto.php \
php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php \
php/src/Google/Protobuf/Internal/OutputStream.php \
php/src/Google/Protobuf/Internal/MessageBuilderContext.php \
php/src/Google/Protobuf/Internal/EnumValueDescriptor.php \
php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php \
php/src/Google/Protobuf/Internal/FileOptions_OptimizeMode.php \
php/src/Google/Protobuf/Internal/DescriptorProto.php \
php/src/Google/Protobuf/Internal/MapField.php \
php/src/Google/Protobuf/Internal/MapFieldIter.php \
php/src/Google/Protobuf/Internal/MethodDescriptorProto.php \
php/src/Google/Protobuf/Internal/DescriptorProto_ExtensionRange.php \
php/src/Google/Protobuf/Internal/DescriptorProto_ReservedRange.php \
php/src/Google/Protobuf/Internal/RepeatedField.php \
php/src/Google/Protobuf/Internal/RepeatedFieldIter.php \
php/src/Google/Protobuf/Internal/EnumValueOptions.php \
php/src/Google/Protobuf/Internal/MethodOptions.php \
php/src/Google/Protobuf/Internal/OneofOptions.php \
php/src/Google/Protobuf/Internal/Message.php \
php/src/Google/Protobuf/Internal/FileOptions.php \
php/src/Google/Protobuf/Internal/FileDescriptorSet.php \
php/src/Google/Protobuf/Internal/DescriptorProto.php \
php/src/Google/Protobuf/Internal/Descriptor.php \
php/src/Google/Protobuf/Internal/EnumBuilderContext.php \
php/src/Google/Protobuf/Internal/EnumDescriptor.php \
php/src/Google/Protobuf/Internal/EnumDescriptorProto.php \
php/src/Google/Protobuf/Internal/GPBWire.php \
php/src/Google/Protobuf/Internal/EnumOptions.php \
php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php \
php/src/Google/Protobuf/Internal/EnumValueDescriptor.php \
php/src/Google/Protobuf/Internal/EnumValueOptions.php \
php/src/Google/Protobuf/Internal/FieldDescriptorProto_Label.php \
php/src/Google/Protobuf/Internal/FieldDescriptorProto.php \
php/src/Google/Protobuf/Internal/FieldDescriptor.php \
php/src/Google/Protobuf/Internal/FieldDescriptorProto_Type.php \
php/src/Google/Protobuf/Internal/FieldOptions_CType.php \
php/src/Google/Protobuf/Internal/FieldOptions_JSType.php \
php/src/Google/Protobuf/Internal/FieldOptions.php \
php/src/Google/Protobuf/Internal/FileDescriptorProto.php \
php/src/Google/Protobuf/Internal/FileDescriptorSet.php \
php/src/Google/Protobuf/Internal/FileDescriptor.php \
php/src/Google/Protobuf/Internal/FileOptions_OptimizeMode.php \
php/src/Google/Protobuf/Internal/FileOptions.php \
php/src/Google/Protobuf/Internal/GeneratedCodeInfo_Annotation.php \
php/src/Google/Protobuf/Internal/FieldDescriptorProto_Type.php \
php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php \
php/src/Google/Protobuf/Internal/GPBDecodeException.php \
php/src/Google/Protobuf/Internal/GPBJsonWire.php \
php/src/Google/Protobuf/Internal/GPBLabel.php \
php/src/Google/Protobuf/Internal/GPBType.php \
php/src/Google/Protobuf/Internal/FieldOptions_JSType.php \
php/src/Google/Protobuf/Internal/GPBUtil.php \
php/src/Google/Protobuf/Internal/GPBWireType.php \
php/src/Google/Protobuf/Internal/GPBWire.php \
php/src/Google/Protobuf/Internal/MapEntry.php \
php/src/Google/Protobuf/Internal/MapFieldIter.php \
php/src/Google/Protobuf/Internal/MapField.php \
php/src/Google/Protobuf/Internal/MessageBuilderContext.php \
php/src/Google/Protobuf/Internal/MessageOptions.php \
php/src/Google/Protobuf/Internal/Message.php \
php/src/Google/Protobuf/Internal/MethodDescriptorProto.php \
php/src/Google/Protobuf/Internal/MethodOptions_IdempotencyLevel.php \
php/src/Google/Protobuf/Internal/MethodOptions.php \
php/src/Google/Protobuf/Internal/OneofDescriptorProto.php \
php/src/Google/Protobuf/Internal/OneofDescriptor.php \
php/src/Google/Protobuf/Internal/OneofField.php \
php/src/Google/Protobuf/Internal/OneofOptions.php \
php/src/Google/Protobuf/Internal/RawInputStream.php \
php/src/Google/Protobuf/Internal/RepeatedFieldIter.php \
php/src/Google/Protobuf/Internal/RepeatedField.php \
php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php \
php/src/Google/Protobuf/Internal/ServiceOptions.php \
php/src/Google/Protobuf/Internal/SourceCodeInfo_Location.php \
php/src/Google/Protobuf/Internal/SourceCodeInfo.php \
php/src/Google/Protobuf/Internal/EnumOptions.php \
php/src/Google/Protobuf/Internal/GPBLabel.php \
php/src/Google/Protobuf/Internal/EnumBuilderContext.php \
php/src/Google/Protobuf/Internal/GPBUtil.php \
php/src/Google/Protobuf/Internal/FieldOptions_CType.php \
php/src/Google/Protobuf/Internal/GPBDecodeException.php \
php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php \
php/src/Google/Protobuf/Internal/UninterpretedOption.php \
php/src/GPBMetadata/Google/Protobuf/Internal/Descriptor.php \
php/tests/array_test.php \
php/tests/autoload.php \
Expand All @@ -673,10 +676,11 @@ php_EXTRA_DIST= \
php/tests/test_base.php \
php/tests/test_util.php \
php/tests/well_known_test.php \
php/tests/undefined_test.php \
php/README.md \
php/phpunit.xml \
php/composer.json \
php/generate_descriptor_protos.sh \
php/phpunit.xml \
composer.json

python_EXTRA_DIST= \
Expand Down
5 changes: 4 additions & 1 deletion conformance/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ conformance-csharp: $(other_language_protoc_outputs)
conformance-php:
@echo "Writing shortcut script conformance-php..."
@echo '#! /bin/sh' > conformance-php
@echo 'php ./conformance_php.php' >> conformance-php
@echo 'php -d auto_prepend_file=autoload.php ./conformance_php.php' >> conformance-php
@chmod +x conformance-php

conformance-php-c:
Expand Down Expand Up @@ -336,6 +336,9 @@ test_php: protoc_middleman conformance-test-runner conformance-php $(other_langu
test_php_c: protoc_middleman conformance-test-runner conformance-php-c $(other_language_protoc_outputs)
./conformance-test-runner --enforce_recommended --failure_list failure_list_php_c.txt ./conformance-php-c

test_php_zts_c: protoc_middleman conformance-test-runner conformance-php-c $(other_language_protoc_outputs)
./conformance-test-runner --enforce_recommended --failure_list failure_list_php_zts_c.txt ./conformance-php-c

# These depend on library paths being properly set up. The easiest way to
# run them is to just use "tox" from the python dir.
test_python: protoc_middleman conformance-test-runner
Expand Down
21 changes: 21 additions & 0 deletions conformance/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

define("GOOGLE_INTERNAL_NAMESPACE", "Google\\Protobuf\\Internal\\");
define("GOOGLE_NAMESPACE", "Google\\Protobuf\\");
define("GOOGLE_GPBMETADATA_NAMESPACE", "GPBMetadata\\Google\\Protobuf\\Internal\\");

function protobuf_autoloader_impl($class, $prefix) {
$length = strlen($prefix);
if ((substr($class, 0, $length) === $prefix)) {
$path = '../php/src/' . implode('/', array_map('ucwords', explode('\\', $class))) . '.php';
include_once $path;
}
}

function protobuf_autoloader($class) {
protobuf_autoloader_impl($class, GOOGLE_INTERNAL_NAMESPACE);
protobuf_autoloader_impl($class, GOOGLE_NAMESPACE);
protobuf_autoloader_impl($class, GOOGLE_GPBMETADATA_NAMESPACE);
}

spl_autoload_register('protobuf_autoloader');
7 changes: 4 additions & 3 deletions conformance/conformance_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function doTest($request)
}
} elseif ($request->getPayload() == "json_payload") {
try {
$test_message->jsonDecode($request->getJsonPayload());
$test_message->mergeFromJsonString($request->getJsonPayload());
} catch (Exception $e) {
$response->setParseError($e->getMessage());
return $response;
Expand All @@ -67,7 +67,7 @@ function doTest($request)
} elseif ($request->getRequestedOutputFormat() == WireFormat::PROTOBUF) {
$response->setProtobufPayload($test_message->serializeToString());
} elseif ($request->getRequestedOutputFormat() == WireFormat::JSON) {
$response->setJsonPayload($test_message->jsonEncode());
$response->setJsonPayload($test_message->serializeToJsonString());
}

return $response;
Expand All @@ -79,7 +79,8 @@ function doTestIO()
if (strlen($length_bytes) == 0) {
return false; # EOF
} elseif (strlen($length_bytes) != 4) {
trigger_error("I/O error", E_USER_ERROR);
fwrite(STDERR, "I/O error\n");
return false;
}

$length = unpack("V", $length_bytes)[1];
Expand Down
Loading

0 comments on commit ecca6ea

Please sign in to comment.