PHP generation, namespaces, and directory structureSorry for the slow response here. Yeah... the PHP directory structure stuff is pretty opaque.
So, the reason for the absolute paths with the THRIFT_ROOT stuff is that PHP is suitable idiotic about how it resolves include paths. All include statements are relative to the directory the PHP interpreter is invoked from, which means that when writing an include statement in a PHP file, you either need to (1) know for certain exactly where PHP will be invoked from and include relative to that, or (2) use absolute paths. There are some other issues related to storing precompiled PHP files in an APC cache, using absolute paths as unique identifiers to avoid conflicts. At Facebook we build our PHP stuff with wrappers that copy the Thrift stuff into an appropriate packages folder, part of our larger tree. I think it'd be a reasonable change to have the generator automatically place files into subdirectories with the appropriate package name (i.e. gen-php/<package>/blah.php). This would make off-the-shelf building, without extra scripts, much easier. We should have just done this off the bat -- the original generator was written before we added include functionality to Thrift, hence weren't thinking about multiple directories being generated at once. Cheers, mcslee ----- Original Message ----- From: David Engberg To: [email protected] Sent: Wednesday, September 10, 2008 11:57 PM Subject: PHP generation, namespaces, and directory structure After a year of development with Thrift on Java, C++, and Cocoa, we recently tried to prepare some PHP sample code for an upcoming public API release. I'm a bit confused about the (inconsistent?) use of directories in the PHP generated code. I have two simple Thrift IDL files: Body.thrift (the main project) and Organs.thrift (a referenced child module) ... // Body.thrift include "Organs.thrift" php_namespace com_evernote_body namespace java com.evernote.body struct Chest { 1: required Organs.Heart heart } service Body { void operate(1: Chest chest) } // Organs.thrift php_namespace com_evernote_organs namespace java com.evernote.organs struct Heart { 1: required i32 pulse } Using the trunk IDL compiler, I can generate Java and PHP sources like this: thrift -r --gen java:beans -php Body.thrift On the Java side, this will produce a nice directory structure based on the Java namespace declarations, and the generated Java code will go into those directories. The generated PHP code just goes into a flat "gen-php" directory with no subdirectories: gen-php/Body.php gen-php/Body_types.php gen-php/Organs_types.php The flat file structure could be ok, since the file names are unambiguous, but the generated code seems to assume a more complicated directory structure: $ grep packages gen-php/*.php gen-php/Body.php:include_once $GLOBALS['THRIFT_ROOT'].'/packages/Body/Body_types.php'; gen-php/Body_types.php:include_once $GLOBALS['THRIFT_ROOT'].'/packages/Organs/Organs_types.php'; It seems like the generated files go into a flat directory, but then their internal include directives assume that the files are maintained in per-IDL subdirectories. This means that the generated code can't be used out of the box without moving some stuff around (as far as I can tell). I'd be happy to submit a compiler patch that would resolve this by either producing the expected subdirectory structure during generation, or else excluding the subdirectories on the include directives. However, I'm a bit concerned that this would break other folks who are actually using PHP intensively today. Could anyone shed any light on this? Thanks
