The tars2php module is to automatically generate client-side and server-side php code through the tars protocol file. (The server-side code is mainly frame code, and the actual business logic needs to be implemented by the user.)
Instructions
If you only have the requirements for packaging and unpacking, then the use process is as follows: 1. Prepare a tars protocol file, such as example.tars
Write a tars.proto.php file, which is a configuration file used to generate code.
//The servant name for this example is PHPTest.PHPServer.obj returnarray( 'appName'=>'PHPTest','serverName'=>'PHPServer','objName'=>'obj','withServant'=> true,//whether it is server-side or client-side 'tarsFiles'=>array( './example.tars'//The path of the tars file ),'dstPath'=>'./server/',//Location of the generated php file 'namespacePrefix'=>'Server\servant',//the namespace prefix of the php file );
Execute php ./tars2php.php ./tars.proto.php
The tool will automatically generate a three-level directory structure according to the servant name. In the demo, the PHPTest/PHPServer/obj/ directory is generated under the ./server directory. The classers in the obj directory are the php objects corresponding to the struct. As in struct in example.tars:
struct SimpleStruct {
0 require long id=0;
1 require unsigned int count=0;
2 require short page=0;
};
The interface section in example.tars will automatically generate a separate php file named interface. For example, int testLofofTags(LotofTags tags, out LotofTags outtags); The method generated by the interface is as follows
server part
<?php //Note that the comment part of the generated file will be converted to php code when the server is started. If not necessary, please do not modify it. /** * @paramstruct $tags \Server\servant\PHPTest\PHPServer\obj\classes\LotofTags * @paramstruct $outtags \Server\servant\PHPTest\PHPServer\obj\classes\LotofTags =out= * @returnint */publicfunctiontestLofofTags(LotofTags $tags,LotofTags&$outtags);
client part
<?php try { $requestPacket =newRequestPacket(); //parameters required to build the request packet $requestPacket->_iVersion =$this->_iVersion; $requestPacket->_funcName =__FUNCTION__; $requestPacket->_servantName =$this->_servantName; $encodeBufs = []; $__buffer =TUPAPIWrapper::putStruct("tags",1,$tags,$this->_iVersion); $encodeBufs['tags'] = $__buffer; $requestPacket->_encodeBufs = $encodeBufs; $sBuffer =$this->_communicator->invoke($requestPacket,$this->_iTimeout); $ret =TUPAPIWrapper::getStruct("outtags",2,$outtags,$sBuffer,$this->_iVersion); // Extract the first output parameter outtags from the returned package returnTUPAPIWrapper::getInt32("",0,$sBuffer,$this->_iVersion); //solve the return parameter return parameter name is empty, tag is 0 } catch (\Exception $e) { throw $e; }