commit
c5f7b4b0b3
50 changed files with 4508 additions and 761 deletions
@ -1,46 +0,0 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. All rights reserved.
|
||||
// https://developers.google.com/protocol-buffers/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#ifndef PHP_PROTOBUF_BUNDLED_PHP_H_ |
||||
#define PHP_PROTOBUF_BUNDLED_PHP_H_ |
||||
|
||||
// We embed PHP source code into the binary for things we don't want to
|
||||
// implement in C. This struct serves as a table of contents for all of
|
||||
// the embedded files.
|
||||
typedef struct { |
||||
const char *filename; |
||||
const char *contents; |
||||
} BundledPhp_File; |
||||
|
||||
// An array of all the embedded file structs. This array is terminated with a
|
||||
// {NULL, NULL} entry.
|
||||
extern BundledPhp_File *bundled_files; |
||||
|
||||
#endif // PHP_PROTOBUF_BUNDLED_PHP_H_
|
@ -1,62 +0,0 @@ |
||||
<?php |
||||
|
||||
$cwd = dirname($argv[0]) . "/../../../src"; |
||||
chdir($cwd); |
||||
|
||||
$cmd = "grep -r -l 'Generated by the protocol buffer' * | grep 'php$' | grep -v Internal"; |
||||
$handle = popen($cmd, 'r'); |
||||
$filenames = explode("\n", stream_get_contents($handle)); |
||||
array_pop($filenames); // empty string after last '\n' |
||||
$filenames[] = "Google/Protobuf/DescriptorPool.php"; |
||||
$output = "../ext/google/protobuf/bundled_php.c"; |
||||
|
||||
function stripSuffix($str, $suffix) { |
||||
return substr($str, 0, strlen($str) - strlen($suffix)); |
||||
} |
||||
|
||||
function toClassName($filename) { |
||||
# Google/Protobuf/BoolValue.php -> Google\\Protobuf\\BoolValue |
||||
$ret = stripSuffix($filename, ".php"); |
||||
return str_replace("/", "\\\\", $ret); |
||||
} |
||||
|
||||
function toCSymbolName($filename) { |
||||
# Google/Protobuf/BoolValue.php -> Google__Protobuf__BoolValue |
||||
$ret = stripSuffix($filename, ".php"); |
||||
return str_replace("/", "__", $ret); |
||||
} |
||||
|
||||
$f = fopen($output, "w"); |
||||
|
||||
fwrite($f, "#include \"bundled_php.h\"\n"); |
||||
fwrite($f, "#include \"stdlib.h\"\n"); |
||||
|
||||
foreach ($filenames as $filename) { |
||||
print("Reading $filename...\n"); |
||||
$contents = file_get_contents($filename); |
||||
$contents = substr($contents, 5); // Strip <?php |
||||
$c_symbol_name = toCSymbolName($filename); |
||||
fwrite($f, "static const char {$c_symbol_name}[] = {"); |
||||
for ($i = 0; $i < strlen($contents); $i++) { |
||||
if ($i % 10 == 0) { |
||||
fwrite($f, "\n"); |
||||
} |
||||
fprintf($f, " 0x%02x,", ord($contents[$i])); |
||||
} |
||||
fwrite($f, "0};\n"); |
||||
} |
||||
|
||||
fwrite($f, "static BundledPhp_File php[] = {\n"); |
||||
foreach ($filenames as $filename) { |
||||
$class_name = toClassName($filename); |
||||
$c_symbol_name = toCSymbolName($filename); |
||||
fwrite($f, " {\"$class_name\", $c_symbol_name},\n"); |
||||
} |
||||
|
||||
fwrite($f, " {NULL, NULL}\n"); |
||||
fwrite($f, "};\n"); |
||||
fwrite($f, "BundledPhp_File *bundled_files = &php[0];\n"); |
||||
fclose($f); |
||||
|
||||
print("Wrote $output\n"); |
||||
?> |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,86 @@ |
||||
<?php |
||||
|
||||
namespace Google\Protobuf\Internal; |
||||
|
||||
/** |
||||
* Base class for Google\Protobuf\Any, this contains hand-written convenience |
||||
* methods like pack() and unpack(). |
||||
*/ |
||||
class AnyBase extends \Google\Protobuf\Internal\Message |
||||
{ |
||||
const TYPE_URL_PREFIX = 'type.googleapis.com/'; |
||||
|
||||
/** |
||||
* This method will try to resolve the type_url in Any message to get the |
||||
* targeted message type. If failed, an error will be thrown. Otherwise, |
||||
* the method will create a message of the targeted type and fill it with |
||||
* the decoded value in Any. |
||||
* @return Message unpacked message |
||||
* @throws \Exception Type url needs to be type.googleapis.com/fully-qualified. |
||||
* @throws \Exception Class hasn't been added to descriptor pool. |
||||
* @throws \Exception cannot decode data in value field. |
||||
*/ |
||||
public function unpack() |
||||
{ |
||||
// Get fully qualified name from type url. |
||||
$url_prifix_len = strlen(GPBUtil::TYPE_URL_PREFIX); |
||||
if (substr($this->type_url, 0, $url_prifix_len) != |
||||
GPBUtil::TYPE_URL_PREFIX) { |
||||
throw new \Exception( |
||||
"Type url needs to be type.googleapis.com/fully-qulified"); |
||||
} |
||||
$fully_qualifed_name = |
||||
substr($this->type_url, $url_prifix_len); |
||||
|
||||
// Create message according to fully qualified name. |
||||
$pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool(); |
||||
$desc = $pool->getDescriptorByProtoName($fully_qualifed_name); |
||||
if (is_null($desc)) { |
||||
throw new \Exception("Class ".$fully_qualifed_name |
||||
." hasn't been added to descriptor pool"); |
||||
} |
||||
$klass = $desc->getClass(); |
||||
$msg = new $klass(); |
||||
|
||||
// Merge data into message. |
||||
$msg->mergeFromString($this->value); |
||||
return $msg; |
||||
} |
||||
|
||||
/** |
||||
* The type_url will be created according to the given message’s type and |
||||
* the value is encoded data from the given message.. |
||||
* @param message: A proto message. |
||||
*/ |
||||
public function pack($msg) |
||||
{ |
||||
if (!$msg instanceof Message) { |
||||
trigger_error("Given parameter is not a message instance.", |
||||
E_USER_ERROR); |
||||
return; |
||||
} |
||||
|
||||
// Set value using serialized message. |
||||
$this->value = $msg->serializeToString(); |
||||
|
||||
// Set type url. |
||||
$pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool(); |
||||
$desc = $pool->getDescriptorByClassName(get_class($msg)); |
||||
$fully_qualifed_name = $desc->getFullName(); |
||||
$this->type_url = GPBUtil::TYPE_URL_PREFIX . $fully_qualifed_name; |
||||
} |
||||
|
||||
/** |
||||
* This method returns whether the type_url in any_message is corresponded |
||||
* to the given class. |
||||
* @param klass: The fully qualified PHP class name of a proto message type. |
||||
*/ |
||||
public function is($klass) |
||||
{ |
||||
$pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool(); |
||||
$desc = $pool->getDescriptorByClassName($klass); |
||||
$fully_qualifed_name = $desc->getFullName(); |
||||
$type_url = GPBUtil::TYPE_URL_PREFIX . $fully_qualifed_name; |
||||
return $this->type_url === $type_url; |
||||
} |
||||
} |
@ -0,0 +1,32 @@ |
||||
<?php |
||||
|
||||
namespace Google\Protobuf\Internal; |
||||
|
||||
/** |
||||
* Base class for Google\Protobuf\Timestamp, this contains hand-written |
||||
* convenience methods. |
||||
*/ |
||||
class TimestampBase extends \Google\Protobuf\Internal\Message |
||||
{ |
||||
/* |
||||
* Converts PHP DateTime to Timestamp. |
||||
* |
||||
* @param \DateTime $datetime |
||||
*/ |
||||
public function fromDateTime(\DateTime $datetime) |
||||
{ |
||||
$this->seconds = $datetime->getTimestamp(); |
||||
$this->nanos = 1000 * $datetime->format('u'); |
||||
} |
||||
|
||||
/** |
||||
* Converts Timestamp to PHP DateTime. |
||||
* |
||||
* @return \DateTime $datetime |
||||
*/ |
||||
public function toDateTime() |
||||
{ |
||||
$time = sprintf('%s.%06d', $this->seconds, $this->nanos / 1000); |
||||
return \DateTime::createFromFormat('U.u', $time); |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue