Commit 6266b4f1 authored by Wouter van Oortmerssen's avatar Wouter van Oortmerssen

Merge pull request #308 from chobie/php

Add support for PHP code.
parents e4eb2864 5ce86826
...@@ -34,6 +34,7 @@ set(FlatBuffers_Compiler_SRCS ...@@ -34,6 +34,7 @@ set(FlatBuffers_Compiler_SRCS
src/idl_gen_general.cpp src/idl_gen_general.cpp
src/idl_gen_go.cpp src/idl_gen_go.cpp
src/idl_gen_js.cpp src/idl_gen_js.cpp
src/idl_gen_php.cpp
src/idl_gen_python.cpp src/idl_gen_python.cpp
src/idl_gen_fbs.cpp src/idl_gen_fbs.cpp
src/flatc.cpp src/flatc.cpp
......
{
"name": "google/flatbuffers",
"type": "library",
"description": "FlatBuffers for PHP",
"keywords": ["google", "flatbuffers", "serialization"],
"homepage": "https://github.com/google/flatbuffers",
"license": "Apache-2.0",
"require": {
"php": ">=5.4"
},
"require-dev": {
},
"autoload": {
"psr-4": {
"Google\\FlatBuffers\\": "php"
}
}
}
\ No newline at end of file
...@@ -528,6 +528,13 @@ extern bool GenerateJava(const Parser &parser, ...@@ -528,6 +528,13 @@ extern bool GenerateJava(const Parser &parser,
const std::string &file_name, const std::string &file_name,
const GeneratorOptions &opts); const GeneratorOptions &opts);
// Generate Php code from the definitions in the Parser object.
// See idl_gen_php.
extern bool GeneratePhp(const Parser &parser,
const std::string &path,
const std::string &file_name,
const GeneratorOptions &opts);
// Generate Python files from the definitions in the Parser object. // Generate Python files from the definitions in the Parser object.
// See idl_gen_python.cpp. // See idl_gen_python.cpp.
extern bool GeneratePython(const Parser &parser, extern bool GeneratePython(const Parser &parser,
......
This diff is collapsed.
<?php
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\FlatBuffers;
class Constants
{
const SIZEOF_SHORT = 2;
const SIZEOF_INT = 4;
const FILE_IDENTIFIER_LENGTH = 4;
}
This diff is collapsed.
<?php
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\FlatBuffers;
abstract class Struct
{
/**
* @var int $bb_pos
*/
protected $bb_pos;
/**
* @var ByteBuffer $bb
*/
protected $bb;
}
<?php
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\FlatBuffers;
abstract class Table
{
/**
* @var int $bb_pos
*/
protected $bb_pos;
/**
* @var ByteBuffer $bb
*/
protected $bb;
public function __construct()
{
}
/**
* returns actual vtable offset
*
* @param $vtable_offset
* @return int offset > 0 means exist value. 0 means not exist
*/
protected function __offset($vtable_offset)
{
$vtable = $this->bb_pos - $this->bb->getInt($this->bb_pos);
return $vtable_offset < $this->bb->getShort($vtable) ? $this->bb->getShort($vtable + $vtable_offset) : 0;
}
/**
* @param $offset
* @return mixed
*/
protected function __indirect($offset)
{
return $offset + $this->bb->getInt($offset);
}
/**
* fetch utf8 encoded string.
*
* @param $offset
* @return string
*/
protected function __string($offset)
{
$offset += $this->bb->getInt($offset);
$len = $this->bb->getInt($offset);
$startPos = $offset + Constants::SIZEOF_INT;
return substr($this->bb->_buffer, $startPos, $len);
}
/**
* @param $offset
* @return int
*/
protected function __vector_len($offset)
{
$offset += $this->bb_pos;
$offset += $this->bb->getInt($offset);
return $this->bb->getInt($offset);
}
/**
* @param $offset
* @return int
*/
protected function __vector($offset)
{
$offset += $this->bb_pos;
// data starts after the length
return $offset + $this->bb->getInt($offset) + Constants::SIZEOF_INT;
}
// protected function __vector_as_bytebuffer($vector_offset, $elem_size)
// {
// }
/**
* @param Table $table
* @param int $offset
* @return Table
*/
protected function __union($table, $offset)
{
$offset += $this->bb_pos;
$table->bb_pos = $offset + $this->bb->getInt($offset);
$table->bb = $this->bb;
return $table;
}
/**
* @param ByteBuffer $bb
* @param string $ident
* @return bool
* @throws \ArgumentException
*/
protected static function __has_identifier($bb, $ident)
{
if (strlen($ident) != Constants::FILE_IDENTIFIER_LENGTH) {
throw new \ArgumentException("FlatBuffers: file identifier must be length " . Constants::FILE_IDENTIFIER_LENGTH);
}
for ($i = 0; $i < 4; $i++) {
if ($ident[$i] != $bb->get($bb->getPosition() + Constants::SIZEOF_INT + $i)) {
return false;
}
}
return true;
}
}
...@@ -72,6 +72,10 @@ const Generator generators[] = { ...@@ -72,6 +72,10 @@ const Generator generators[] = {
flatbuffers::GeneratorOptions::kMAX, flatbuffers::GeneratorOptions::kMAX,
"Generate Python files for tables/structs", "Generate Python files for tables/structs",
flatbuffers::GeneralMakeRule }, flatbuffers::GeneralMakeRule },
{ flatbuffers::GeneratePhp, "--php", "PHP",
flatbuffers::GeneratorOptions::kMAX,
"Generate Php files for tables/structs",
flatbuffers::GeneralMakeRule },
}; };
const char *program_name = NULL; const char *program_name = NULL;
......
This diff is collapsed.
<?php
// automatically generated, do not modify
namespace MyGame\Example;
class Any
{
const NONE = 0;
const Monster = 1;
const TestSimpleTableWithEnum = 2;
private static $names = array(
"NONE",
"Monster",
"TestSimpleTableWithEnum",
);
public static function Name($e)
{
if (!isset(self::$names[$e])) {
throw new \Exception();
}
return self::$names[$e];
}
}
<?php
// automatically generated, do not modify
namespace MyGame\Example;
class Color
{
const Red = 1;
const Green = 2;
const Blue = 8;
private static $names = array(
"Red",
"Green",
"Blue",
);
public static function Name($e)
{
if (!isset(self::$names[$e])) {
throw new \Exception();
}
return self::$names[$e];
}
}
This diff is collapsed.
<?php
// automatically generated, do not modify
namespace MyGame\Example;
use \Google\FlatBuffers\Struct;
use \Google\FlatBuffers\Table;
use \Google\FlatBuffers\ByteBuffer;
use \Google\FlatBuffers\FlatBufferBuilder;
class Stat extends Table
{
/**
* @param ByteBuffer $bb
* @return Stat
*/
public static function getRootAsStat(ByteBuffer $bb)
{
$obj = new Stat();
return ($obj->init($bb->getInt($bb->getPosition()) + $bb->getPosition(), $bb));
}
public static function StatIdentifier()
{
return "MONS";
}
public static function StatBufferHasIdentifier(ByteBuffer $buf)
{
return self::__has_identifier($buf, self::StatIdentifier());
}
public static function StatExtension()
{
return "mon";
}
/**
* @param int $_i offset
* @param ByteBuffer $_bb
* @return Stat
**/
public function init($_i, ByteBuffer $_bb)
{
$this->bb_pos = $_i;
$this->bb = $_bb;
return $this;
}
public function getId()
{
$o = $this->__offset(4);
return $o != 0 ? $this->__string($o + $this->bb_pos) : null;
}
/**
* @return long
*/
public function getVal()
{
$o = $this->__offset(6);
return $o != 0 ? $this->bb->getLong($o + $this->bb_pos) : 0;
}
/**
* @return ushort
*/
public function getCount()
{
$o = $this->__offset(8);
return $o != 0 ? $this->bb->getUshort($o + $this->bb_pos) : 0;
}
/**
* @param FlatBufferBuilder $builder
* @return void
*/
public static function startStat(FlatBufferBuilder $builder)
{
$builder->StartObject(3);
}
/**
* @param FlatBufferBuilder $builder
* @return Stat
*/
public static function createStat(FlatBufferBuilder $builder, $id, $val, $count)
{
$builder->startObject(3);
self::addId($builder, $id);
self::addVal($builder, $val);
self::addCount($builder, $count);
$o = $builder->endObject();
return $o;
}
/**
* @param FlatBufferBuilder $builder
* @param StringOffset
* @return void
*/
public static function addId(FlatBufferBuilder $builder, $id)
{
$builder->addOffsetX(0, $id, 0);
}
/**
* @param FlatBufferBuilder $builder
* @param long
* @return void
*/
public static function addVal(FlatBufferBuilder $builder, $val)
{
$builder->addLongX(1, $val, 0);
}
/**
* @param FlatBufferBuilder $builder
* @param ushort
* @return void
*/
public static function addCount(FlatBufferBuilder $builder, $count)
{
$builder->addUshortX(2, $count, 0);
}
/**
* @param FlatBufferBuilder $builder
* @return int table offset
*/
public static function endStat(FlatBufferBuilder $builder)
{
$o = $builder->endObject();
return $o;
}
}
<?php
// automatically generated, do not modify
namespace MyGame\Example;
use \Google\FlatBuffers\Struct;
use \Google\FlatBuffers\Table;
use \Google\FlatBuffers\ByteBuffer;
use \Google\FlatBuffers\FlatBufferBuilder;
class Test extends Struct
{
/**
* @param int $_i offset
* @param ByteBuffer $_bb
* @return Test
**/
public function init($_i, ByteBuffer $_bb)
{
$this->bb_pos = $_i;
$this->bb = $_bb;
return $this;
}
/**
* @return short
*/
public function GetA()
{
return $this->bb->getShort($this->bb_pos + 0);
}
/**
* @return sbyte
*/
public function GetB()
{
return $this->bb->getSbyte($this->bb_pos + 2);
}
/**
* @return int offset
*/
public static function createTest(FlatBufferBuilder $builder, $a, $b)
{
$builder->prep(2, 4);
$builder->pad(1);
$builder->putSbyte($b);
$builder->putShort($a);
return $builder->offset();
}
}
<?php
// automatically generated, do not modify
namespace MyGame\Example;
use \Google\FlatBuffers\Struct;
use \Google\FlatBuffers\Table;
use \Google\FlatBuffers\ByteBuffer;
use \Google\FlatBuffers\FlatBufferBuilder;
class TestSimpleTableWithEnum extends Table
{
/**
* @param ByteBuffer $bb
* @return TestSimpleTableWithEnum
*/
public static function getRootAsTestSimpleTableWithEnum(ByteBuffer $bb)
{
$obj = new TestSimpleTableWithEnum();
return ($obj->init($bb->getInt($bb->getPosition()) + $bb->getPosition(), $bb));
}
public static function TestSimpleTableWithEnumIdentifier()
{
return "MONS";
}
public static function TestSimpleTableWithEnumBufferHasIdentifier(ByteBuffer $buf)
{
return self::__has_identifier($buf, self::TestSimpleTableWithEnumIdentifier());
}
public static function TestSimpleTableWithEnumExtension()
{
return "mon";
}
/**
* @param int $_i offset
* @param ByteBuffer $_bb
* @return TestSimpleTableWithEnum
**/
public function init($_i, ByteBuffer $_bb)
{
$this->bb_pos = $_i;
$this->bb = $_bb;
return $this;
}
/**
* @return sbyte
*/
public function getColor()
{
$o = $this->__offset(4);
return $o != 0 ? $this->bb->getSbyte($o + $this->bb_pos) : \MyGame\Example\Color::Green;
}
/**
* @param FlatBufferBuilder $builder
* @return void
*/
public static function startTestSimpleTableWithEnum(FlatBufferBuilder $builder)
{
$builder->StartObject(1);
}
/**
* @param FlatBufferBuilder $builder
* @return TestSimpleTableWithEnum
*/
public static function createTestSimpleTableWithEnum(FlatBufferBuilder $builder, $color)
{
$builder->startObject(1);
self::addColor($builder, $color);
$o = $builder->endObject();
return $o;
}
/**
* @param FlatBufferBuilder $builder
* @param sbyte
* @return void
*/
public static function addColor(FlatBufferBuilder $builder, $color)
{
$builder->addSbyteX(0, $color, 2);
}
/**
* @param FlatBufferBuilder $builder
* @return int table offset
*/
public static function endTestSimpleTableWithEnum(FlatBufferBuilder $builder)
{
$o = $builder->endObject();
return $o;
}
}
<?php
// automatically generated, do not modify
namespace MyGame\Example;
use \Google\FlatBuffers\Struct;
use \Google\FlatBuffers\Table;
use \Google\FlatBuffers\ByteBuffer;
use \Google\FlatBuffers\FlatBufferBuilder;
class Vec3 extends Struct
{
/**
* @param int $_i offset
* @param ByteBuffer $_bb
* @return Vec3
**/
public function init($_i, ByteBuffer $_bb)
{
$this->bb_pos = $_i;
$this->bb = $_bb;
return $this;
}
/**
* @return float
*/
public function GetX()
{
return $this->bb->getFloat($this->bb_pos + 0);
}
/**
* @return float
*/
public function GetY()
{
return $this->bb->getFloat($this->bb_pos + 4);
}
/**
* @return float
*/
public function GetZ()
{
return $this->bb->getFloat($this->bb_pos + 8);
}
/**
* @return double
*/
public function GetTest1()
{
return $this->bb->getDouble($this->bb_pos + 16);
}
/**
* @return sbyte
*/
public function GetTest2()
{
return $this->bb->getSbyte($this->bb_pos + 24);
}
/**
* @return Test
*/
public function getTest3()
{
$obj = new Test();
$obj->init($this->bb_pos + 26, $this->bb);
return $obj;
}
/**
* @return int offset
*/
public static function createVec3(FlatBufferBuilder $builder, $x, $y, $z, $test1, $test2, $test3_a, $test3_b)
{
$builder->prep(16, 32);
$builder->pad(2);
$builder->prep(2, 4);
$builder->pad(1);
$builder->putSbyte($test3_b);
$builder->putShort($test3_a);
$builder->pad(1);
$builder->putSbyte($test2);
$builder->putDouble($test1);
$builder->pad(4);
$builder->putFloat($z);
$builder->putFloat($y);
$builder->putFloat($x);
return $builder->offset();
}
}
..\flatc.exe -c -j -n -g -b -p --php -s --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
..\flatc.exe -b --schema monster_test.fbs
../flatc -c -j -n -g -b -p -s --gen-mutable --no-includes monster_test.fbs monsterdata_test.json ../flatc -c -j -n -g -b -p -s --php --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
../flatc -b --schema monster_test.fbs ../flatc -b --schema monster_test.fbs
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment