Giter Club home page Giter Club logo

protobuf-php's Introduction

Protobuf for PHP

Protobuf for PHP is an implementation of Google's Protocol Buffers for the PHP language, supporting its binary data serialization and including a protoc plugin to generate PHP classes from .proto files.

Great effort has been put into generating PHP files that include all sort of type hints to aide IDE's with autocompletion. Therefore, it can not only be used to communicate with Protocol Buffers services but also as a generation tool for data objects no matter what the final serialization is.

For more information see the included man pages.

Requirements

  • PHP 5.3
  • Pear's Console_CommandLine (for the protoc wrapper tool)
  • Google's protoc compiler version 2.3 or above
  • GMP or BC Math extensions ¹

¹ Only needed for negative values in int32, int64 or fixed64 types. See the known issues section.

Features

Working

  • Standard types (numbers, string, enums, messages, etc)
  • Pluggable serialization backends (codecs)
    • Standard Binary
    • Standard TextFormat ¹
    • PhpArray
    • JSON
    • ProtoJson (TagMap and Indexed variants)
    • XML
  • Protoc compiler plugin to generate the PHP classes
  • Extensions
  • Unknown fields
  • Packed fields
  • Reflection
  • Dynamic messages with annotations support
  • Generates service interfaces
  • Includes comments from .proto files in the generated files
  • Pear package for easy installation

¹ Only serialization is supported

Future

  • Speed optimized code generation mode
  • Support numbers beyond PHP's native limits

Example usage

<?php
require_once 'DrSlump/Protobuf.php';
\DrSlump\Protobuf::autoload();

$person = new Tutorial\Person();
$person->name = 'DrSlump';
$person->setId(12);

$book = new Tutorial\AddressBook();
$book->addPerson($person);

// Use default codec
$data = $book->serialize();

// Use custom codec
$codec = new \DrSlump\Protobuf\Codec\Binary();
$data = $codec->encode($book);
// ... or ...
$data = $book->serialize($codec);

Installation

Install with Pear

pear channel-discover pear.pollinimini.net
pear install drslump/Protobuf-beta

You can also get the latest version by checking out a copy of the repository in your computer.

Known issues

Types

PHP is very weak when dealing with numbers processing. Several work arounds have been applied to the standard binary codec to reduce incompatibilities between Protobuf types and PHP ones.

  • Protobuf stores floating point values using the IEEE 754 standard with 64bit words for the double and 32bit for the float types. PHP supports IEEE 754 natively although the precission is platform dependant, however it typically supports 64bit doubles. It means that if your PHP was compiled with 64bit sized doubles (or greater) you shouldn't have any problem encoding and decoded float and double typed values with Protobuf.

  • Integer values are also platform dependant in PHP. The library has been developed and tested against PHP binaries compiled with 64bit integers. The encoding and decoding algorithm should in theory work no matter if PHP uses 32bit or 64bit integers internally, just take into account that with 32bit integers the numbers cannot exceed in any case the PHP_INT_MAX value (2147483647).

    While Protobuf supports unsigned integers PHP does not. In fact, numbers above the compiled PHP maximum integer (PHP_INT_MAX, 0x7FFFFFFFFFFFFFFF for 64bits) will be automatically casted to doubles, which typically will offer 53bits of decimal precission, allowing to safely work with numbers upto 0x20000000000000 (2^53), even if they are represented in PHP as floats instead of integers. Higher numbers will loose precission or might even return an infinity value, note that the library does not include any checking for these numbers and using them might provoke unexpected behaviour.

    Negative values when encoded as int32, int64 or fixed64 types require the big integer extensions GMP or BC Math (the later only for 64bit architectures) to be available in your PHP environment. The reason is that when encoding these negative numbers without using zigzag the binary representation uses the most significant bit for the sign, thus the numbers become above the maximum supported values in PHP. The library will check for these conditions and will automatically try to use GMP or BC to process the value.

Strings

The binary codec expects strings to be encoded using UTF-8. PHP does not natively support string encodings, PHP's string data type is basically a length delimited stream of bytes, so it's not trivial to include automatic encoding conversion into the library encoding and decoding routines. Instead of trying to guess or offer a configuration interface for the encoding, the binary codec will process the string type just as it would process byte one, delegating on your application the task of encoding or decoding in the desired character set.

Memory usage

Large messages might be troublesome since the way the library is modelled does not allow to parse or serialize messages as a streams, instead the whole operation is performed in memory, which allows for faster processing but could consume too much RAM if messages are too large.

Unknown fields

Since wire types are different across different codec's formats, it's not possible to transcode unkwnon fields consumed in one codec to another. This means, for example, that when consuming a message using the binary codec, if it contains unknown fields, they won't be included when serializing the message using the Json codec.

Generating PHP classes

The generation tool is designed to be run as a protoc plugin, thus it should work with any proto file supported by the official compiler.

protoc --plugin=protoc-gen-php --php_out=./build tutorial.proto

To make use of non-standard options in your proto files (like php.namespace) you'll have to import the php.proto file included with the library. It's location will depend on where you've installed this library.

protoc -I=./Protobuf-PHP/library/DrSlump/Protobuf/Compiler/protos \
       --plugin=protoc-gen-php --php_out=./build tutorial.proto

In order to make your life easier, the supplied protoc plugin offers an additional execution mode, where it acts as a wrapper for the protoc invocation. It will automatically include the php.proto path so that you don't need to worry about it.

protoc-gen-php -o ./build tutorial.proto

LICENSE:

The MIT License

Copyright (c) 2011 Iván -DrSlump- Montes

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

protobuf-php's People

Contributors

ablagoev avatar arul- avatar cavedon avatar drslump avatar felipernb avatar slimus avatar

Stargazers

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

Watchers

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

protobuf-php's Issues

Int64 wrong parsing.

Hi. I found that Int64 parsing from binary reader is wrong.
it should be done like this.

    public function varint()
    {
        $result = $shift = 0;
        $bytes=array();

        do {
            $byte = $this->byte();
            $bytes[]=$byte;
        } while ($byte > 0x7f);
        for ($j = count($bytes)-1; $j >= 0; $j--)
        {
            $result = $result * 128 + ($bytes[$j] & 0x7F);
        }
        return $result;
    }

Missing file when running the benchmark

When I attempt to run the benchmark I get this error

[27-Jun-2017 10:04:22 Pacific/Auckland] PHP Warning:  require_once(Benchmark/Profiler.php): failed to open stream: No such file or directory in C:\WebServer\something\tests\benchmark.php on line 3
[27-Jun-2017 10:04:22 Pacific/Auckland] PHP Fatal error:  require_once(): Failed opening required 'Benchmark/Profiler.php' (include_path='.;C:\php\pear') in C:\WebServer\something\tests\benchmark.php on line 3

I cant seem to find that file on the repo at all, can you help me out here?

Getting path errors

I'm just trying to use this, installed everything via Pear as described but get the following...

PHP Warning: require_once(DrSlump/Protobuf.php): failed to open stream: No such file or directory in /usr/local/bin/protoc-gen-php on line 36

Warning: require_once(DrSlump/Protobuf.php): failed to open stream: No such file or directory in /usr/local/bin/protoc-gen-php on line 36
PHP Fatal error: require_once(): Failed opening required 'DrSlump/Protobuf.php' (include_path='.:') in /usr/local/bin/protoc-gen-php on line 36

Fatal error: require_once(): Failed opening required 'DrSlump/Protobuf.php' (include_path='.:') in /usr/local/bin/protoc-gen-php on line 36

Does anyone have any suggestions?!

Unknow error

I try generate php proc files:
root@user: /var/www/web/test/dota# protoc --plugin=protoc-gen-php --php_out=./ ai_activity.proto
protoc-gen-php: program not found or is not executable
--php_out: protoc-gen-php: Plugin failed with status code 1.

What mean this error?

optional int32 value may not be 0

Hi,

I've found a problem today. I have a proto file like the following:

Package stream;

Message User{
optional int32 userid = 1;
optional string name = 2;
}
Message Other{
optional string one = 1;
optional string two = 2;
}

Message Container{
required string header = 1;
repeated User user = 2;
repeated Other other = 3;
}

php-file:
$header0 = new stream\User();
$header0->userid = "0";
$header0->name = "testuser";

$header1 = new stream\Other();
$header1->one = "one";
$header1->two = "two";

$container = new stream\Container();
$container->setHeader("page1");
$container->addUser($header0);
$container->addUser($header1);

When I set userid to 0 like above, my c++ application sees only a message Container with one message user, where the values of Other are not somehow getting lost. If I set userid to any other value than 0 it does work. Is this a limitation of protobuffers in general or is this a bug?

can't find Console/CommandLine.php why?

PHP Warning: require_once(Console/CommandLine.php): failed to open stream: No such file or directory in /Users/Qing/code/gitcode/Protobuf-PHP/library/DrSlump/Protobuf/Compiler/Cli.php on line 5

Warning: require_once(Console/CommandLine.php): failed to open stream: No such file or directory in /Users/Qing/code/gitcode/Protobuf-PHP/library/DrSlump/Protobuf/Compiler/Cli.php on line 5

Read the contents of a protocol buffer

I have a socket that returns a protocol buffer. When I try and read it, I see it as array but with junk for data (binary I'm assuming).

How can I un-serialize this data? Currently I'm trying:

$codec= new \DrSlump\Protobuf\Codec\PhpArray();
echo $codec->encode($response);

$response is not an instance of DrSlump\Protobuf\message - obviously...

Note: I can send the protocol buffer TO the socket fine - I'm just trying to read the response back.

UnicodeDecodeError: 'utf8' codec can't decode

Hi. I trying to test my code with your library by Google AdX requester (https://developers.google.com/ad-exchange/rtb/downloads) written on python.
This tool send me binary requests, I decode them, set result to object (which created by your library) and send out serialized response, but got error bellow.
I'm not sure that the problem in your library, but I have no ideas what happend. If you have any ideas, please let me know.

Traceback (most recent call last): File "requester.py", line 337, in <module> main() File "requester.py", line 333, in main PrintSummary(logger_obj, opts.sample_encrypted_price) File "requester.py", line 198, in PrintSummary summarizer.Summarize() File "/home/op/requester/log.py", line 277, in Summarize bid_response.ParseFromString(record.payload) File "/usr/lib/pymodules/python2.7/google/protobuf/message.py", line 168, in ParseFromString self.MergeFromString(serialized) File "/usr/lib/pymodules/python2.7/google/protobuf/reflection.py", line 821, in MergeFromString if self._InternalParse(serialized, 0, length) != length: File "/usr/lib/pymodules/python2.7/google/protobuf/reflection.py", line 848, in InternalParse pos = field_decoder(buffer, new_pos, end, self, field_dict) File "/usr/lib/pymodules/python2.7/google/protobuf/internal/decoder.py", line 450, in DecodeRepeatedField if value.add()._InternalParse(buffer, pos, new_pos) != new_pos: File "/usr/lib/pymodules/python2.7/google/protobuf/reflection.py", line 848, in InternalParse pos = field_decoder(buffer, new_pos, end, self, field_dict) File "/usr/lib/pymodules/python2.7/google/protobuf/internal/decoder.py", line 337, in DecodeField field_dict[key] = local_unicode(buffer[pos:new_pos], 'utf-8') UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 126: invalid continuation byte

class

where do i find these?

$person = new Tutorial\Person();
$book = new Tutorial\AddressBook();

fix, managed to get it to work:

require_once 'DrSlump/Protobuf.php';// keep this on top
\DrSlump\Protobuf::autoload();

include_once 'tests/protos/addressbook.php';// important this comes after the autoload

$person = new tests\Person();
$person->name = 'DrSlump2';
$person->setId(12);

$book = new tests\AddressBook();
$book->addPerson($person);

// Use default codec
//$data = $book->serialize();

// Use custom codec
$codec = new \DrSlump\Protobuf\Codec\Binary();
$data = $codec->encode($book);


// ... or ...
//$data = $book->serialize($codec);

print_r($data);

protoc-gen-php is not found when protoc is run from the command line

protoc --plugin=protoc-gen-php --php_out=./build simple.proto

error message:
protoc-gen-php: program not found or is not executable
--php_out: protoc-gen-php: Plugin failed with status code 1.

fixed: protoc --plugin=/FULL/PATH/TO/protoc-gen-php --php_out=./build simple.proto

protoc-gen-php: command not found

After installation

pear channel-discover pear.pollinimini.net
pear install drslump/Protobuf-beta

I still can't run this generator. With output like this :

sudo protoc --plugin=protoc-gen-php --php_out=./build Bank.proto
protoc-gen-php: program not found or is not executable
--php_out: protoc-gen-php: Plugin failed with status code 1.
$ protoc-gen-php
-bash: protoc-gen-php: command not found

"File does not reside within path"

I'm not quite understanding this error:

$ protoc-gen-php -o ./php MsgMgtPing.proto 
Protobuf-PHP 0.9.4 by Ivan -DrSlump- Montes

/var/.../.../../message/MsgMgtPing.proto: File does not reside within any 
path specified using --proto_path (or -I).  You must specify a --proto_path 
which encompasses this file.  Note that the proto_path must be an exact 
prefix of the .proto file names -- protoc is too dumb to figure out when two 
paths (e.g. absolute and relative) are equivalent (it's harder than you think).

ERROR: protoc exited with an error (1) when executed with: 
  protoc \
    --plugin=protoc-gen-php='/usr/bin/protoc-gen-php' \
    --proto_path='/usr/share/php/DrSlump/Protobuf/Compiler/protos' \
    --php_out=':./php' \
    '/var/.../.../.../message/MsgMgtPing.proto'
$ protoc --version
libprotoc 2.4.1

You must provide at least 1 argument

My enviroment:
PHP v.5.3.13
Pear's Console_CommandLine v.1.2.0
protoc.exe v.2.6.1

Steps:

  1. Installed Protobuf-PHP library using pear:
    pear channel-discover pear.pollinimini.net
    pear install drslump/Protobuf-beta
  2. Tried to generate PHP from protobuf (protoc.exe, protoc-gen-php.bat and tutorial.proto located in the same directory "P:\pear"):
    protoc-gen-php.bat -i . tutorial.proto

Output:


P:\pear>protoc-gen-php.bat -o ./build -i . tutorial.proto
X-Powered-By: PHP/5.3.13
Content-type: text/html

Protobuf-PHP 0.9.4 by Ivan -DrSlump- Montes

Error: You must provide at least 1 argument.
Type "P:\pear\protoc-gen-php --help" to get help.


P.S. There are also similar question without answer on stackoverflow.com (http://stackoverflow.com/questions/30478277/parse-proto-to-php-class)

protoc-gen-php blocks on select syscall

Hello,

First of all, thank you for all the work you've done.

I've recently tried to generate a simple proto file using the helper script protoc-gen-php, but the execution never ends. If I strace the process it seems to be blocking for IO on the select() syscall.

I know, this is kind of fuzzy report, but have you ever noticed such a behavior ? (i'm currently using php 5.4 and protoc 2.4.1)

Here is the debug trace (well the end of it that I get), select is waiting for read IO on fd 5, do you have any idea ?

Thanx again.

open("./message.proto", O_RDONLY) = 3
close(3) = 0
open("message.proto", O_RDONLY) = 3
read(3, "message RoutingFrame {\n\trequired"..., 8192) = 1432
read(3, "", 8192) = 0
close(3) = 0
pipe([3, 4]) = 0
pipe([5, 6]) = 0
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f748f137a10) = 12897
close(3) = 0
close(6) = 0
rt_sigaction(SIGPIPE, {SIG_IGN, [PIPE], SA_RESTORER|SA_RESTART, 0x7f748e3bd4a0}, {SIG_DFL, [], 0}, 8) = 0
select(6, [5], [4], NULL, NULL) = 1 (out [4])
write(4, "\n\rmessage.protoz\276\23\n\rmessage.prot"..., 2512) = 2512
close(4) = 0
select(6, [5], [], NULL, NULL^C <unfinished ...>

Can not compile .proto file with protoc version 2.5.

$ protoc-gen-php social.proto -i `pwd`
Protobuf-PHP 0.9.4 by Ivan -DrSlump- Montes

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

ERROR: protoc exited with an error (1) when executed with: 
  protoc \
    --plugin=protoc-gen-php='/usr/local/Cellar/php54/5.4.15/bin/protoc-gen-php' \
    --proto_path='/usr/local/Cellar/php54/5.4.15/lib/php/DrSlump/Protobuf/Compiler/protos' \
    --proto_path='/Users/***/lib/model/ProtocolBuffers' \
    --php_out=':./' \
    '/Users/***/lib/model/ProtocolBuffers/social.proto'

protoc installed with brew.
php54 installed with brew.
protobuf-php installed with pear.

after downgrading (still using brew) to protoc 2.4.1 it compiled fine.

pear.pollinimini.net down

I cannot build Protobuf-PHP because it can't access pear.pollinimini.net. I cannot access it using my web browser, wget, or the pear discover-channel command. Here are the errors I get when attempting to discover the channel:

Discovering channel pear.pollinimini.net over http:// failed with message: channel-add: Cannot open "http://pear.pollinimini.net/channel.xml" (Connection to `pear.pollinimini.net:80' failed: Network is unreachable)
Trying to discover channel pear.pollinimini.net over https:// instead
Discovery of channel "pear.pollinimini.net" failed (channel-add: Cannot open "https://pear.pollinimini.net/channel.xml" (Connection to `pear.pollinimini.net:443' failed: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?))

Message::_set allows potentially unsafe usage

Today I bumped my environment to PHP7.2 and saw a new failure. It came about while serializing a structure with a repeated field, we'll call it SomeRepeatedValue - I had inadvertently used something like this:

$object->setSomeRepeatedValue($val)

Message::_has will call count if the field is repeated which is the cause of this error.

What seems to be happening is Message::$SomeRepeatedValue is not set as an array, because (1) the field isn't an extension, and (2) the $idx was null (the default value)

The difference between the two cases:

idx=null
$this->$SomeRepeatedValue = $val
and serialization fails

idx=0
$this->{$SomeRepeatedValue}[$idx] = $val
and serialization succeeds

Calling Message::_set() should check if the tag is a repeated field, and if so, should change the idx default from null to 0.

Please note, I'm not an expert on protobufs, and haven't dug into the spec in detail, but it seems _set/_has/_get should be consistent in how they handle repeated values, and would best be accomplished in _set.

PHP extension names are not compatible with Java

Hi,

I am using your Protobuf classes in my server-side PHP application. I had an issue sending my proto objects to my Java servlet because of the way the extension names were modified. I have implemented a temporary solution and have committed the changes in my fork. The issue was that replacing the "." with "" were causing an issue on the Java side where extensions are identified in the DOT notation.

Cheers for the great effort.

Compatible interfaces

Do we need to disable E_WARNING or maybe we have another way to avoid this problem?

PHP 7.0.9 (cli) (built: Jul 29 2016 20:47:36) ( NTS )
libprotoc 2.6.1

--php_out: protoc-gen-php: Plugin output is unparseable:
Warning: Declaration of google\protobuf\FileDescriptorProto::hasExtension() should be compatible with DrSlump\Protobuf\Message::hasExtension($extname) in /var/www/vendor/datto/protobuf-php/library/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 111\n\nWarning: Declaration of google\protobuf\FileDescriptorProto::getExtension($idx = NULL) should be compatible with DrSlump\Protobuf\Message::getExtension($extname, $idx = NULL) in /var/www/vendor/datto/protobuf-php/library/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 111\n\nWarning: Declaration of google\protobuf\FileDescriptorProto::getExtensionList() should be compatible with DrSlump\Protobuf\Message::getExtensionList($extname)

and so on

Expected wire type 2 but got 1 for type 11

Hi. Also one new question.

I tryed to decode request (see example bellow) but in any way I have these message
Uncaught exception 'RuntimeException' with message 'Expected wire type 2 but got 1 for type 11'
where type 11 is "url" field in request.

Request example

id: "Mv\2005\000\017.\001\n\345\177\307X\200M8"
ip: "\314j\310\004"
user_agent: "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.107 Safari/534.13,gzip"
country: "US"
region: "US-MA"
city: "Boston"
metro: 506
url: "http://www.example.com/"
detected_language: "en"

Code example

require_once 'class/DrSlump/Protobuf.php';
\DrSlump\Protobuf::autoload();

include_once 'class/AdX.php';
$request = new BidRequest();

$data = file_get_contents('php://input');
$request->parse($data);

P.S. For the next time can I'll ask my questions here?

getWireType()

Hi,

First time I used this interface (I'm also a newbie in protobuff in general), and got pages of warnings when trying to compile even one of the example proto file.
Turned out I had to change line 249 of Binary.php from:
$wire = $this->getWireType($type);
to:
$wire = $this->getWireType($type, null);

Seems like it stopped the original php warning that was triggering a bunch of other errors.
Hope it helps

Unexpected namespace naming with nested messages

I try to make PHP classes from this proto: https://developers.google.com/ad-exchange/rtb/downloads/realtime-bidding-proto.txt with following command:

protoc-gen-php -o ~/rtb/ -i ./ -v -Dmultifile -Dpackage="Google.Rtb" realtime-bidding.proto

I expect to see PSR-0 like structure of generated library, but actually protoc-gen-php generates namespace Google\Rtb for BidRequest and BidResponse classes only:

Protobuf-PHP 0.9.4 by Ivan -DrSlump- Montes

NOTICE: Mapping  to Google\Rtb
WARNING: Non tracked package name found "BidRequest.UserList"
WARNING: Non tracked package name found "BidRequest.Vertical"
WARNING: Non tracked package name found "BidRequest.KeyValue"
WARNING: Non tracked package name found "BidRequest.Mobile"
WARNING: Non tracked package name found "BidRequest.Video"
WARNING: Non tracked package name found "BidRequest.MatchingNetwork"
...

Other classes have wrong namespaces, e.g., Ad class has \BidResponse namespace instead \Google\Rtb\BidResponse. Is it a bug or just wrong usage?

Wrong value UINT64

TYPE_UINT64
Binary compiler, function decode()

Value 76561198028888080 -> 85399568

How convert 85399568 in 76561198028888080 again?

Improve runtime for Enum types

Right now Enums are generated as simple PHP classes with constants. They should inherit from a base Enum class that offers introspection features.

class Enum implements \Iterator, \ArrayAccess {             
    public function getInstance(){ 
        static $inst;
        return $inst ?: new static();
    }

    public function __isset() { ... }
    public function __get() { ... }
}

class MyEnum extends Enum {
   const FOO = 1;
   const BAR = 2;
}

$enum = MyEnum::getInstance();
foreach ($enum as $k=>$v) {
  echo $k => $v;
}

$enum['FOO'] === 1;
$enum[1] === 'FOO';

proto map is not support?

my proto file has the keyworld: map<string,Object>
generate the php file by proto file,that is ok.
but, protobuf::decode is not ok, the data as following:
class tests\Storys#3 (4) {
public $data =>
array(1) {
[0] =>
class tests\Storys\DataEntry#26 (5) {
public $key =>
NULL
public $value =>
NULL
protected $_descriptor =>
class DrSlump\Protobuf\Descriptor#27 (4) {
...
}
protected $_extensions =>
array(0) {
...
}
protected $_unknown =>
array(1) {
...
}
}
}
protected $_descriptor =>
class DrSlump\Protobuf\Descriptor#5 (4) {
protected $class =>
string(12) "tests\Storys"
protected $name =>
string(12) "tests.Storys"
protected $fields =>
array(1) {
[1] =>
class DrSlump\Protobuf\Field#6 (8) {
...
}
}
protected $names =>
array(1) {
'data' =>
int(1)
}
}
protected $_extensions =>
array(0) {
}
protected $_unknown =>
array(0) {
}
}

One class per file, PSR-1 Basic Coding Standard

Currently Protobuf-PHP generates files which have more than one class per file.

This has the effect of not playing well with PSR-4 autoloading.

3 Namespace and Class Names

Namespaces and classes MUST follow an "autoloading" PSR: [PSR-0, PSR-4].

This means each class is in a file by itself, and is in a namespace of at least one level: a top-level vendor name.

http://www.php-fig.org/psr/psr-1/

Proto Polymorphism Support

I use extensions in proto like,
message Cat
{ extend Animal
{
required Cat animal = 100; // Unique Animal extension number
}
optional bool declawed = 1;
}
Potobuf-PHP generate successfully, and addExtension() also there
But it seems not working while codec, instead only gives me the rest of the message without extensions.

Just want to clarify, does Protobuf-PHP support codec of protos with extensions?

[ I have done with http://www.indelible.org/ink/protobuf-polymorphism/ and it worked well with Java, now using the same proto to setup PHP project ]

protoc-gen-php options ignored

I try generate php classes with custom suffix, ns and multifile:

>protoc-gen-php -Dsuffix=custom.php -Dmultifile -Dpackage="MyNs" -o ./build -i ./protos protos/example.proto
Protobuf-PHP 0.9.4 by Ivan -DrSlump- Montes

[libprotobuf WARNING google/protobuf/compiler/parser.cc:546] No syntax specified for the proto file: php.proto. Please u
se 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)
[libprotobuf WARNING google/protobuf/compiler/parser.cc:546] No syntax specified for the proto file: descriptor.proto. P
lease use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)

But output I have one file example.pb.php and dont drslump

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

require_once('php.pb.php');
use Google\Protobuf\Internal\DescriptorPool;
use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\GPBUtil;

class Click extends \Google\Protobuf\Internal\Message
{
...

Reader constant not exist

Fatal error: Undefined class constant 'BIG_ENDIAN' in library/DrSlump/Protobuf/Codec/Binary/Reader.php on line 182

Only imported files are generated even with skip imported option

I have branched off of the lazy branch and I am unable to generate files that have imports in them (e.g. plugin.proto)

Can you advise if you are experiencing this issue or where I should look?

Inside Compiler.php

        // Get the list of files to generate
        $files = $req->getFileToGenerate();

        // Run each file
        foreach ($req->proto_file as $file) {
            // Only compile those given to generate, not the imported ones
            if ($this->skipImported && !in_array($file->getName(), $files)) {
                $this->notice('Skipping generation of imported file "' . $file->getName() . '"');
                continue;
            }

            $this->notice("FILE {$file->getName()}");
            $sources = $generator->generate($file);
            foreach ($sources as $source) {
                $this->notice('Generating "' . $source->getName() . '"');
                $resp->addFile($source);
            }
        }

result of files is correct.

$req->proto_file only returns file listed as import

Doesn't play well with mbstring plugin with mbstring.func_overload ini set

If the mbstring.func_overload ini setting is set to & 2, it will overload the strlen function to mb_strlen, which causes problems when using it to count the number of bytes.

Causing: strlen(hextobin("c708")) === 1

It may be helpful to wrap all strlen calls in a function that checks for this ini setting, and if it's set, use
mb_strlen($str, '8bit') instead just to reduce headaches in environments where this is on.

Empty repeated messages are not parsed correctly

A message containing an empty repeated messages breaks parsing. See examples below

// test.proto
package test;
message Inner {
    optional int32 inner_value = 1;
}

message Outer {
    repeated Inner inner = 1;
    optional int32 outer_value = 2;
} 

and

// create_dump_test.py 
#!/usr/bin/env python

import test_pb2

# Create PB

outer = test_pb2.Outer()
outer.inner.add()
outer.outer_value = 1

s = outer.SerializeToString()
with open('test.pb', 'w') as f:
    f.write(s)

# Load and dump PB

outer = test_pb2.Outer()

with open('test.pb', 'r') as f:
    outer.ParseFromString(f.read())

print outer

Will give you:

inner {
}
outer_value: 1

If you parse it with Protobuf-PHP it will try to decode outer_value as member of the Inner message, instead of Outer, therefore failing to parse the rest of the message.

The following php code

use \DrSlump\Protobuf;
Protobuf::autoload();
require_once('test.php');

$fp = fopen("test.pb", 'r');
$pb_data = fread($fp, 1024);
fclose($fp);

$pb = new test\Outer();
$pb->parse($pb_data);

$codec = new \DrSlump\Protobuf\Codec\TextFormat();

print($codec->encode($pb)."\n");

Will give you :

inner {
}

(i.e. missing outer_value: 1)

Incompatible signatures, cannot compile full

class FileDescriptorProto extends \DrSlump\Protobuf\Message

function
https://github.com/drslump/Protobuf-PHP/blob/master/library/DrSlump/Protobuf/Message.php#L283

bun parent has parm:
https://github.com/drslump/Protobuf-PHP/blob/master/library/DrSlump/Protobuf/Message.php#L283

and:

/work/grpc/examples/php/route_guide (release-0_15_0*) $ protoc-gen-php -i . -o . ./route_guide.proto
Protobuf-PHP 1.0 by Ivan -DrSlump- Montes

PHP Warning: Declaration of google\protobuf\FileDescriptorProto::hasExtension() should be compatible with DrSlump\Protobuf\Message::hasExtension($extname) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 111
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6
PHP Warning: Declaration of google\protobuf\FileDescriptorProto::getExtension($idx = NULL) should be compatible with DrSlump\Protobuf\Message::getExtension($extname, $idx = NULL) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 111
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6
PHP Warning: Declaration of google\protobuf\FileDescriptorProto::getExtensionList() should be compatible with DrSlump\Protobuf\Message::getExtensionList($extname) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 111
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6
PHP Warning: Declaration of google\protobuf\FileDescriptorProto::setExtension(google\protobuf\FieldDescriptorProto $value, $idx = NULL) should be compatible with DrSlump\Protobuf\Message::setExtension($extname, $value, $idx = NULL) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 111
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6
PHP Warning: Declaration of google\protobuf\FileDescriptorProto::addExtension(google\protobuf\FieldDescriptorProto $value) should be compatible with DrSlump\Protobuf\Message::addExtension($extname, $value) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 111
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6
PHP Warning: Declaration of google\protobuf\FileDescriptorProto::clearExtension() should be compatible with DrSlump\Protobuf\Message::clearExtension($extname) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 111
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6
PHP Warning: Declaration of google\protobuf\DescriptorProto::hasExtension() should be compatible with DrSlump\Protobuf\Message::hasExtension($extname) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 688
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6
PHP Warning: Declaration of google\protobuf\DescriptorProto::getExtension($idx = NULL) should be compatible with DrSlump\Protobuf\Message::getExtension($extname, $idx = NULL) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 688
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6
PHP Warning: Declaration of google\protobuf\DescriptorProto::getExtensionList() should be compatible with DrSlump\Protobuf\Message::getExtensionList($extname) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 688
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6
PHP Warning: Declaration of google\protobuf\DescriptorProto::setExtension(google\protobuf\FieldDescriptorProto $value, $idx = NULL) should be compatible with DrSlump\Protobuf\Message::setExtension($extname, $value, $idx = NULL) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 688
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6
PHP Warning: Declaration of google\protobuf\DescriptorProto::addExtension(google\protobuf\FieldDescriptorProto $value) should be compatible with DrSlump\Protobuf\Message::addExtension($extname, $value) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 688
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6
PHP Warning: Declaration of google\protobuf\DescriptorProto::clearExtension() should be compatible with DrSlump\Protobuf\Message::clearExtension($extname) in /usr/share/php/DrSlump/Protobuf/Compiler/protos/descriptor.pb.php on line 688
PHP Stack trace:
PHP 1. {main}() /usr/bin/protoc-gen-php:0
PHP 2. DrSlump\Protobuf\Compiler\Cli::run() /usr/bin/protoc-gen-php:43
PHP 3. spl_autoload_call() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 4. DrSlump\Protobuf::DrSlump{closure}() /usr/share/php/DrSlump/Protobuf/Compiler/Cli.php:54
PHP 5. include() /usr/share/php/DrSlump/Protobuf.php:58
PHP 6. require_once() /usr/share/php/DrSlump/Protobuf/Compiler.php:6

So, route_guide.php not contains client code (RouteGuideClient class)
What is wrong?

How to get human readable incoming message.

Hi. I reviewed all code from your library but not found how to get incoming message in human readable view.
I found method encode, but I don't know what kind of object I need to put into this method.
Maybe you know the best way to do this. What method can I use after "$request->parse($this->_request_string)" to get usual string for binary representation of "$this->_request_string".

_has() behaves incorrectly

Hi,
it looks like that Message::_has() is not behaving according to the specifications. See the following example:

Given the following PB definition:

message Test {
    optional bool mybool = 1 [default = false];
}

and the following code:

$codec = new \DrSlump\Protobuf\Codec\TextFormat();
echo $codec->encode($test);

The output is:


While it should be empty because "mybool" was not set (or maybe the default value "false", but deninitely not "true").

I tracked this bug to the fact that Message::_has() return true if the descriptor has a field with that tag, but not whether the field has been set or not (as it should).

Do you confirm this?
Or am I misusing the library?
What is the easiest way to fix this?

Thanks!
Ludovico

Generating php classes script error

When Generating PHP classes, it report error:

File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).

ERROR: protoc exited with an error (1) when executed with:
protoc
--plugin=protoc-gen-php='/Users/myproject/libs/Protobuf-PHP/protoc-gen-php.php'
--proto_path='/myproject/libs/Protobuf-PHP/library/DrSlump/Protobuf/Compiler/protos'
--php_out=':./'
'/myproject/test.proto'

Groups are deprecated in Protocol Buffers and unsupported by this library

Hi.
I'm having such a mistake and I understand the message I have got, but I have no other way to get the data coming in this request. Maybe you have older versions of your library, which supports this data type or you can provide some code snippets, which will be helpful for me to resolve this problem.

400 Bad Request when calling Google service

Hi,

I'm using protobufs for the first time to access a Google web service and am getting a 400 Bad Request error.
I'm not entirely sure the problem comes from the content of my POST message but I would like to make sure I am sending the right content.
It seems Google only accepts protobuff encoded with binary codec (\DrSlump\Protobuf\Codec\Binary), is that correct? I tried with multiple codecs (JSON, text, etc, trying to send the appropriate content-type in parallel) but I always get the same result.
When I check the serialized message everything looks fine I believe the library is working nicely, but can you advice on what codecs are "allowed" by Google plateforms?

Thanks!

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.