Giter VIP home page Giter VIP logo

phpbuf's Introduction

There are no generators of a code. You self should create classes of messages. 

The detailed description http://memo.undr.su/2009/10/15/realizaciya-google-protocol-buffers-na-php/ (Inside Russian)

Example of usage of library:

protobufer file:

message Message_Example {
    required int32 id = 1;
    required sint32 balance = 2;
    required bool isAdmin = 3;
    enum Status {
        active = 0;
        inactive = 1;
        deleted = 2;
    }
    required enum Status status = 4;
    required string name = 5;
    required bytes bytes = 6;
}

php code:

<?php
/**
 * Class of message
 *
 */
class Message_Example extends PhpBuf_Message_Abstract {
    public function __construct() {
        $this->setField("id", PhpBuf_Type::INT, PhpBuf_Rule::REQUIRED, 1);
        $this->setField("balance", PhpBuf_Type::SINT, PhpBuf_Rule::REQUIRED, 2);
        $this->setField("isAdmin", PhpBuf_Type::BOOL, PhpBuf_Rule::REQUIRED, 3);
        $this->setField("status", PhpBuf_Type::ENUM, PhpBuf_Rule::REQUIRED, 4, array("active", "inactive", "deleted"));
        $this->setField("name", PhpBuf_Type::STRING, PhpBuf_Rule::REQUIRED, 5);
        $this->setField("bytes", PhpBuf_Type::BYTES, PhpBuf_Rule::REQUIRED, 6);
    }
    public static function name(){
        return __CLASS__;
    }
}

/**
 * Work with message
 */
require_once("/phpbuf/lib/PhpBuf.php");

$message = new Message_Example();
$writer = new PhpBuf_IO_Writer();
$message->id = 150;
$message->balance = -12345;
$message->isAdmin = true;
$message->status = "deleted";
$message->name = "Andrey Lepeshkin";
$message->bytes = "Some bytes";
$message->write($writer);
                
$reader = PhpBuf_IO_Reader::createFromWriter($writer);
$message = new Message_Example();
$message->read($reader);
                
$this->assertEquals(150, $message->id);
$this->assertEquals(-12345, $message->balance);         
$this->assertTrue($message->isAdmin);
$this->assertEquals("deleted", $message->status);               
$this->assertEquals("Andrey Lepeshkin", $message->name);                
$this->assertEquals("Some bytes", $message->bytes);  

?>

RPC call:
----

RPC call required implements: http://code.google.com/p/protobuf-socket-rpc/

package com.example.protobuf;

message Request {
    message Hoge {
        required string value = 1;
    }
}
message Response {
    message Foo {
        repeated string values = 1;
    }
}

service MyService {
    rpc sayHello(Request.Hoge) returns(Response.Foo);
}

php code:

<?php

class Request_Hoge extends PhpBuf_Message_Abstract {
    public function __construct(){
        $this->setField('value', PhpBuf_Type::STRING, PhpBuf_Rule::REQUIRED, 1);
    }
    public static function name(){
        return __CLASS__;
    }
}
class Response_Foo extends PhpBuf_Message_Abstract {
    public function __construct(){
        $this->setField('values', PhpBuf_Type::STRING, PhpBuf_Rule::REPEATED, 1);
    }
    public static function name(){
        return __CLASS__;
    }
}

class MyService extends PhpBuf_RPC_Service_Client {
    public function __construct(PhpBuf_RPC_Context $context){
        parent::__construct($context);
        $this->setServiceFullQualifiedName('com.example.protobuf.MyService');
        $this->registerMethodResponderClass('sayHello', Response_Foo::name());
    }
}

$request = new Request_Hoge;
$request->value = 'hello world';

$ctx = new PhpBuf_RPC_Context;
$ctx->addServer('127.0.0.1', 12345);
$ctx->addServer('127.0.0.1', 67890);

$service = new MyService($ctx);
$response = $service->sayHello($request);

?>

phpbuf's People

Contributors

knevcher avatar nowelium avatar undr avatar yusak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

phpbuf's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.