Search code examples
phpnamespacesthriftpsr-4

PSR-4 namespace in generated files with Thrift


I generate php files with Apache Thrift

thrift -out / --gen php:nsglobal=src\\Application\\Package\\Thrift package.thrift

The above command generates code in the following path src/Application/Package/Thrift and set src\Application\Package\Thrift for namespace but I use Psr-4 (with composer) in my project we know src as App so namespace should be App\Application\Package\Thrift

my project structure

src |
    | Application |
    |             | Package |
    |             |         | Thrift

I went generate files in Thrift folder with namespace same as following:

App\Application\Package\Thrift

Do you have an idea to solve this problem?

I try add namespace php App.Application.Package to IDL file and run following command:

thrift -out /src --gen php /data/service.thrift

But it create extra App folder => src/App/Application/Package/Thrift


Solution

  • Declare the namespace in the IDL file:

     namespace php Whatever.You.Want
    

    Full example can be found here

    Also, remove the nsglobal option and just do

    thrift -out / --gen php package.thrift
    

    i need App in namespace and src in folder name

    If you need the folder being different from what the namespace says, consider writing a shell script (or batch file) to move the generated files to where you need them to be after Thrift code generation.


    PS: Not sure if specifying a root folder as the output target is such a great idea at all.