phptars/tars2php

此包最新版本(0.3.2)没有提供许可证信息。

PHP代码自动生成工具

0.3.2 2020-04-17 07:49 UTC

This package is auto-updated.

Last update: 2024-09-17 17:05:35 UTC


README

简要介绍

tars2php的主要功能是通过tars协议文件自动生成客户端和服务器端的PHP代码,这是大家都在使用的。 (服务器端主要是框架代码,实际业务逻辑需要自行补充和实现)

基本类型的映射

以下是我们的基本类型映射

    bool => \TARS::BOOL
    char => \TARS::CHAR
    uint8 => \TARS::UINT8
    short => \TARS::SHORT
    uint16 => \TARS::UINT16
    float => \TARS::FLOAT
    double => \TARS::DOUBLE
    int32 => \TARS::INT32
    uint32 => \TARS::UINT32
    int64 => \TARS::INT64
    string => \TARS::STRING
    vector => \TARS::VECTOR
    map => \TARS::MAP
    struct => \TARS::STRUCT

当我们需要识别特定变量类型时,我们需要使用这些基本类型,它们是1-14的常量。

##复杂类型的映射

对于vector、map和struct有一些特殊的打包和解包机制。因此,需要引入特殊的数据类型。

Vector

    vector => \TARS_VECTOR
    It has two member functions, push back() and push back()

The input parameter depends on what type of array the vector itself contains



For example:
    $shorts = ["test1","test2"];
    $vector = new \TARS_VECTOR(\TARS::STRING); //定义一个string类型的vector
    foreach ($shorts as $short) {
        $vector->pushBack($short); //依次吧test1,test2两个元素,压入vector中
    }

map

    map => \TARS_MAP
   It has two member functions, push back() and push back()

The input parameter depends on what type the map itself contains



For example:
    $strings = [["test1"=>1],["test2"=>2]];
    $map = new \TARS_MAP(\TARS::STRING,\TARS::INT64); //定义一个key为string,value是int64的map
    foreach ($strings as $string) {
        $map->pushBack($string); //Press two elements into the map in turn, and notice that pushback receives an array, and that array has only one element
    }

struct

    struct => \TARS_Struct
    sThe constructor of struct is special. It takes two parameters, classname and classfields

The first describes the name, and the second describes the information of the variables in struct



For example:
	class SimpleStruct extends \TARS_Struct {
		const ID = 0; //TARS file every struct的tag
		const COUNT = 1;

		public $id; //strcutThe values of each element in are saved here
		public $count; 

		protected static $fields = array(
			self::ID => array(
				'name'=>'id',//struct every element 
				'required'=>true,//Whether each element in struct is required or not corresponds to the requirements and optional in the tars file
				'type'=>\TARS::INT64,//struct every element type
				),
			self::COUNT => array(
				'name'=>'count',
				'required'=>true,
				'type'=>\TARS::UINT8,
				),
		);

		public function __construct() {
			parent::__construct('App_Server_Servant.SimpleStruct', self::$fields);
		}
	}

如何使用tars2php

如果用户只使用打包和解包需求,过程如下

  1. 准备一个tar协议文件,例如example.tar

  2. 编写一个tar.proto.php文件,这是用于生成代码的配置文件。


//The service name of this example is phptest.phpserver.obj
return array(
‘appName’=>‘PHPTest’,//tars name

“server name”=>“PHPServer”,//tars servant name

‘obj name’=>‘obj’,//tars slaver name

“with servant”=>correct,//

'tarsFiles'=>array(

'./example.tars'//tars

),

'dstPath'=>'./server/',//php

'namespace prefix' = > 'server / service',//php'

);
  1. 执行php ./tars2php.php ./tars.proto.php

  2. 该工具将根据服务名自动生成三层目录结构。在demo中,将在./server目录下生成phptest / PHP服务器 / obj / 目录。obj目录下的类是struct对应的PHP对象,tar目录是tar协议文件本身。

例如,example.tar中的struct

struct SimpleStruct {
    0 require long id=0;
    1 require unsigned int count=0;
    2 require short page=0;
};

转变为classes/SimpleStruct.php

<?php

namespace Server\servant\PHPTest\PHPServer\obj\classes;

class SimpleStruct extends \TARS_Struct {
	const ID = 0; //tars protocal tag
	const COUNT = 1;
	const PAGE = 2;
	
	public $id; //Actual value of element
	public $count; 
	public $page; 
	
	protected static $_fields = array(
		self::ID => array(
			'name'=>'id', //tars Name of no element in the agreement
			'required'=>true, //tars Required or optional in the agreement
			'type'=>\TARS::INT64, //type
			),
		self::COUNT => array(
			'name'=>'count',
			'required'=>true,
			'type'=>\TARS::UINT32,
			),
		self::PAGE => array(
			'name'=>'page',
			'required'=>true,
			'type'=>\TARS::SHORT,
			),
	);

	public function __construct() {
		parent::__construct('PHPTest_PHPServer_obj_SimpleStruct', self::$_fields);
	}
}
  1. example.Tar中的接口部分自动生成一个单独的接口PHP文件。

例如,'int testlofoftags (lotoftags tags, out lotoftags outbound);' 的接口生成方法如下

服务器部分


<? PHP

//Note that the comment part of the generated file will be converted to PHP code when the server is started. If not necessary, do not modify it

//The specific implementation of the server part needs to be inherited by itself. The notes are as follows



//The parameter is of struct type, corresponding to $tags variable. The corresponding PHP object is in \ server \ service \ phptest \ phpserver \ obj \ classes \ lotoftags

//The parameter is of struct type, corresponding to $outputs variable. The corresponding PHP object is in \ server \ service \ phptest \ phpserver \ obj \ classes \ lotoftags, which is the output parameter

//Interface prevent to int
	/**
	 * @param struct $tags \Server\servant\PHPTest\PHPServer\obj\classes\LotofTags
	 * @param struct $outtags \Server\servant\PHPTest\PHPServer\obj\classes\LotofTags =out=
	 * @return int 
	 */
	public function testLofofTags(LotofTags $tags,LotofTags &$outtags);

客户端部分

<?php
	try {
		$requestPacket = new RequestPacket(); //Parameters required to build the request package
		$requestPacket->_iVersion = $this->_iVersion;
		$requestPacket->_funcName = __FUNCTION__;
		$requestPacket->_servantName = $this->_servantName;
		
		$encodeBufs = [];

		$__buffer = TUPAPIWrapper::putStruct("tags",1,$tags,$this->_iVersion); //Package first parameter tags
		$encodeBufs['tags'] = $__buffer;
		
		$requestPacket->_encodeBufs = $encodeBufs; //Set request in request package bufs

		$sBuffer = $this->_communicator->invoke($requestPacket,$this->_iTimeout); //Send request package, receive return package

		$ret = TUPAPIWrapper::getStruct("outtags",2,$outtags,$sBuffer,$this->_iVersion); //Extract the first output parameter from the return packageouttags
		return TUPAPIWrapper::getInt32("",0,$sBuffer,$this->_iVersion); //The return parameter name is empty and the tag is 0

	}
	catch (\Exception $e) {
		throw $e;
	}