protoc-gen-php --php_out: protoc-gen-php: Plugin output is unparseable.

背景

业务需要用protobuffer 进行通讯。

client: php

server: c++

在github 上找到 Protobuf-PHP (https://github.com/drslump/Protobuf-PHP) 的评分不错,故采用它作为协议生成库来生成PHP侧的交互协议。

 

问题

生成php proto

protoc --plugin=/usr/local/php/bin/protoc-gen-php --php_out=. -I. tutorial.proto

 

报错:--php_out: protoc-gen-php: Plugin output is unparseable.

 

proto 文件 tutorial.proto 如下:

package tutorial;

message Person {
   required string name = 1;
   required int32 id = 2;
   optional string email = 3;
   
    enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
   }

   message PhoneNumber {
        required string number = 1;
        optional PhoneType type = 2 [default = HOME];   
   }

   repeated PhoneNumber phone = 4; 
}  

message AddressBook {
   repeated Person person = 1; 
}

 

定位与解决

PHP日志跟踪发现:

[13-Apr-2015 15:33:55 GMT] PHP Warning:  Missing argument 2 for DrSlump\Protobuf\Codec\Binary::getWireType(), called in /usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Codec/Binary.php on line 249 and defined in /usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Codec/Binary.php on line 333
[13-Apr-2015 15:33:55 GMT] PHP Stack trace:
[13-Apr-2015 15:33:55 GMT] PHP   1. {main}() /usr/local/services/php-5.4.39/bin/protoc-gen-php:0
[13-Apr-2015 15:33:55 GMT] PHP   2. DrSlump\Protobuf\Compiler\Cli::run() /usr/local/services/php-5.4.39/bin/protoc-gen-php:43
[13-Apr-2015 15:33:55 GMT] PHP   3. DrSlump\Protobuf\Compiler->compile() /usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Compiler/Cli.php:55
[13-Apr-2015 15:33:55 GMT] PHP   4. DrSlump\Protobuf\Message->__construct() /usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Compiler.php:104
[13-Apr-2015 15:33:55 GMT] PHP   5. DrSlump\Protobuf\Message->parse() /usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Message.php:56
[13-Apr-2015 15:33:55 GMT] PHP   6. DrSlump\Protobuf\Codec\Binary->decode() /usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Message.php:107
[13-Apr-2015 15:33:55 GMT] PHP   7. DrSlump\Protobuf\Codec\Binary->decodeMessage() /usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Codec/Binary.php:65
[13-Apr-2015 15:33:55 GMT] PHP   8. DrSlump\Protobuf\Codec\Binary->decodeMessage() /usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Codec/Binary.php:266
[13-Apr-2015 15:33:55 GMT] PHP   9. DrSlump\Protobuf\Codec\Binary->decodeMessage() /usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Codec/Binary.php:266
[13-Apr-2015 15:33:55 GMT] PHP  10. DrSlump\Protobuf\Codec\Binary->decodeMessage() /usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Codec/Binary.php:266
[13-Apr-2015 15:33:55 GMT] PHP  11. DrSlump\Protobuf\Codec\Binary->getWireType() /usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Codec/Binary.php:249

 

跟入代码发现:/usr/local/services/php-5.4.39/lib/php/DrSlump/Protobuf/Codec/Binary.php 中的第249行,

$wire = $this->getWireType($type);

 

参数传递异常。

 

发现需要传入2个参数,于是修改为:

$wire = $this->getWireType($type, null);

 

重新执行

protoc-gen-php --out=./ -i. -Dsuffix=.proto.php tutorial.proto

生成成功:tutorial.proto.php

另一种方式:

protoc --plugin=/usr/local/php/bin/protoc-gen-php --php_out=. -I. tutorial.proto

生成成功:tutorial.php

 

协议已经成功生成。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。