2

PHP7要使用Google protobuf有两步骤:Protocol Compiler Installation和Protobuf Runtime Installation
一、Protocol Compiler Installation
1、下载:https://github.com/google/pro...
图片描述
2、解压:/Users/User/Downloads/protoc-3.2.0-osx-x86_64/bin/protoc
二、Protobuf Runtime Installation
1、找到PHP对应的package:
https://github.com/google/pro...
下载,相当于给框架添加一个依赖包:
composer require "google/protobuf"

2、在框架目录里生成PHP文件:
--plugin=vendor/google/protobuf/php/generate_descriptor_protos.sh:protobuf对应的php扩展插件

--php_out=src/ :生成protobuf对应php文件的目录

src/demo.proto :框架目录下定义的.proto文件

demo.proto:

syntax = "proto3";

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

执行命令,生成php文件:

/Users/User/Downloads/protoc-3.2.0-osx-x86_64/bin/protoc  --plugin=vendor/google/protobuf/php/generate_descriptor_protos.sh --php_out=src/ src/demo.proto

或者

/Users/User/Downloads/protoc-3.2.0-osx-x86_64/bin/protoc  --proto_path=myProto/  --php_out=app/Http/Controllers/  myProto/demo.proto

图片描述

3、使用protobuf
生成SearchRequest.php文件后,在框架的控制器里引用,经测试,放在Controllers目录以外,会导致引用错误,最终选择在Controllers目录下生成proto文件

<?php

namespace App\Http\Controllers;
require_once "SearchRequest.php";
class ExampleController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(){}

    public function setData() {
        $part1 = null;
        $part1 = new \SearchRequest();
        $part1->setQuery('6');
        $part1->setPageNumber(66);
        $part1->setResultPerPage(666);
        $protoData = $part1->serializeToString();

        return $protoData;
    }

    public function getData() {
        $protoData = file_get_contents("http://api.xxx.com/setData");
        try {
            //运行得到压缩过后的字符串
            $part2 = new \SearchRequest();
            $part2->mergeFromString($protoData);
            $part2->setQuery("okaaa");

            return $part2->serializeToString();
            // 获取到$age的值
        } catch (Exception $ex) {
            die('Upss.. there is a bug in this example');
        }
    }


}

SearchRequest.php,有一处GPBMetadataDemo::initOnce()需要修改命名空间:

<?php
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: demo.proto

use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\GPBUtil;
/**
 * Protobuf type <code>SearchRequest</code>
 */
class SearchRequest extends \Google\Protobuf\Internal\Message
{
    /**
     * <code>string query = 1;</code>
     */
    private $query = '';
    /**
     * <code>int32 page_number = 2;</code>
     */
    private $page_number = 0;
    /**
     * <code>int32 result_per_page = 3;</code>
     */
    private $result_per_page = 0;

    public function __construct() {
        //\GPBMetadata\Demo::initOnce();//原始代码
        \App\Http\Controllers\GPBMetadata\Demo::initOnce();//修改命名空间
        parent::__construct();
    }

    /**
     * <code>string query = 1;</code>
     */
    public function getQuery()
    {
        return $this->query;
    }

    /**
     * <code>string query = 1;</code>
     */
    public function setQuery($var)
    {
        GPBUtil::checkString($var, True);
        $this->query = $var;
    }

    /**
     * <code>int32 page_number = 2;</code>
     */
    public function getPageNumber()
    {
        return $this->page_number;
    }

    /**
     * <code>int32 page_number = 2;</code>
     */
    public function setPageNumber($var)
    {
        GPBUtil::checkInt32($var);
        $this->page_number = $var;
    }

    /**
     * <code>int32 result_per_page = 3;</code>
     */
    public function getResultPerPage()
    {
        return $this->result_per_page;
    }

    /**
     * <code>int32 result_per_page = 3;</code>
     */
    public function setResultPerPage($var)
    {
        GPBUtil::checkInt32($var);
        $this->result_per_page = $var;
    }

}

Demo.php,修改命名空间:

<?php
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: demo.proto

//namespace GPBMetadata;//原始命名空间
namespace App\Http\Controllers\GPBMetadata;//修改后的命名空间

class Demo
{
    public static $is_initialized = false;

    public static function initOnce() {
        $pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();

        if (static::$is_initialized == true) {
          return;
        }
        $pool->internalAddGeneratedFile(hex2bin(
            "0a620a0a64656d6f2e70726f746f224c0a0d536561726368526571756573" .
            "74120d0a05717565727918012001280912130a0b706167655f6e756d6265" .
            "7218022001280512170a0f726573756c745f7065725f7061676518032001" .
            "2805620670726f746f33"
        ));

        static::$is_initialized = true;
    }
}

备注:如果执行命令出现错误,可到vendor/google/protobuf目录下执行:

./autogen.sh
./configure
./generate_descriptor_proto.sh

疯狂的麦克斯
137 声望3 粉丝

I'm a bug


引用和评论

0 条评论