function load($path) { $data = @file_get_contents($path); try { $result = ProtocolBuffers::decode("Tutorial_AddressBook", $data); } catch (ProtocolBuffersInvalidProtocolBufferException $e) { $result = new Tutorial_AddressBook(); } return $result; }
<?php class Person extends ProtocolBuffersMessage { protected $value; /** * get descriptor for protocol buffers * * @return array */ public static function getDescriptor() { static $descriptor; if (!isset($descriptor)) { $desc = new ProtocolBuffersDescriptorBuilder(); $desc->addField(1, new ProtocolBuffersFieldDescriptor(array("type" => ProtocolBuffers::TYPE_INT32, "name" => "value", "required" => false, "optional" => false, "repeated" => false, "packable" => false, "default" => null, "message" => "Tutorial_Person"))); $desc->addExtensionRange(200, 500); $desc->addExtensionRange(600, 700); $descriptor = $desc->build(); $descriptor->dump(); } return $descriptor; } } $registry = ProtocolBuffersExtensionRegistry::getInstance(); // You can extend Person message like this. (normally we generate these code from .proto. adding by hand is bad idea.) $registry->add("Person", 256, new ProtocolBuffersFieldDescriptor(array("type" => ProtocolBuffers::TYPE_INT32, "name" => "address", "required" => false, "optional" => false, "repeated" => false, "packable" => false, "default" => null))); $p = new Person(); $p->setExtension("address", 256); $p2 = ProtocolBuffers::decode("Person", $p->serializeToString()); var_dump($p2);
/** * @param $input raw protocol buffers message * @return \google\protobuf\compiler\CodeGeneratorResponse */ public function compile($input) { $packages = array(); $req = \ProtocolBuffers::decode('google\\protobuf\\compiler\\CodeGeneratorRequest', $input); $this->setupFullName($req); $this->setupDictionary($req); /* @var $req \google\protobuf\compiler\CodeGeneratorRequest */ $parameter = array(); $resp = new \google\protobuf\compiler\CodeGeneratorResponse(); $context = new GeneratorContext($resp); $gen = new Generator(); $error = new StringStream(); PragmaticInserter::loadYaml(".protoc.php.yml"); foreach ($req->getProtoFile() as $file_descriptor) { if (!in_array($file_descriptor->getName(), $req->getFileToGenerate())) { //error_log($file_descriptor->getName()); continue; } $gen->generate($file_descriptor, $parameter, $context, $error); } $resp->setError($error); return $resp; }
<?php require dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . "contrib/php/addressbook.proto.php"; $person = new Tutorial_Person(); $person->setId(21); $person->setName("John Doe"); $data = ProtocolBuffers::encode($person); printf("# decoding benchmark (protocolbuffers).\n"); printf("# target message size: %d bytes.\n", strlen($data)); printf("# start\n"); $begin = microtime(true); $attempts = 100000; for ($i = 0; $i < $attempts; $i++) { ProtocolBuffers::decode("Tutorial_Person", $data); } $end = microtime(true); printf("# Result:\n"); printf("# decoding %d messages\n", $attempts); printf("# processing time: %6f (%f/message)\n", $end - $begin, ($end - $begin) / $attempts); printf("# total bytes: %d bytes\n", strlen($data) * $attempts);
foreach (explode("\n", $a) as $line) { $line = trim($line); if (empty($line)) { continue; } $args = explode("\t", trim($line)); $prefecture = new Tutorial_Prefecture(); $prefecture->setId($i); $prefecture->setName($args[0]); $prefecture->setPopulation($args[1]); $prefecture->setArea($args[2]); $prefecture->setPopulationDensity($args[3]); $jpn->appendPrefecture($prefecture); $i++; } file_put_contents("prefecture.db", $jpn->serializeToString()); $data = $jpn->serializeToString(); $data = file_get_contents("prefecture.db"); printf("# decoding benchmark (protocolbuffers).\n"); printf("# target message size: %d bytes.\n", strlen($data)); printf("# start\n"); $begin = microtime(true); $attempts = 10000; for ($i = 0; $i < $attempts; $i++) { ProtocolBuffers::decode("Tutorial_Japan", $data); } $end = microtime(true); printf("# Result:\n"); printf("# decoding %d messages\n", $attempts); printf("# processing time: %6f (%f/message)\n", $end - $begin, ($end - $begin) / $attempts); printf("# total bytes: %d bytes\n", strlen($data) * $attempts);