From ef6a1766ceeddac8345aa3102e4d33af4ecd8432 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Wed, 6 Oct 2010 17:10:55 +0000 Subject: [PATCH] THRIFT-927. php: Option to support modifying the PHP include path Make the PHP generator recognize a new-style namespace called "php.path". If it is present, it is segmented and used as a path to include the generated code. (It goes between "packages" and the Thrit file basename.) git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@1005172 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_php_generator.cc | 32 +++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc index 2d9c90c1..04242c44 100644 --- a/compiler/cpp/src/generate/t_php_generator.cc +++ b/compiler/cpp/src/generate/t_php_generator.cc @@ -67,6 +67,8 @@ class t_php_generator : public t_oop_generator { escape_['$'] = "\\$"; } + static bool is_valid_namespace(const std::string& sub_namespace); + /** * Init and close methods */ @@ -183,6 +185,22 @@ class t_php_generator : public t_oop_generator { return ns.size() ? (ns + "_") : ""; } + std::string php_path(t_program* p) { + std::string ns = p->get_namespace("php.path"); + if (ns.empty()) { + return p->get_name(); + } + + // Transform the java-style namespace into a path. + for (std::string::iterator it = ns.begin(); it != ns.end(); ++it) { + if (*it == '.') { + *it = '/'; + } + } + + return ns + '/' + p->get_name(); + } + private: /** @@ -222,6 +240,11 @@ class t_php_generator : public t_oop_generator { }; +bool t_php_generator::is_valid_namespace(const std::string& sub_namespace) { + return sub_namespace == "path"; +} + + /** * Prepares for file generation by opening up the necessary file output * streams. @@ -247,8 +270,9 @@ void t_php_generator::init_generator() { const vector& includes = program_->get_includes(); for (size_t i = 0; i < includes.size(); ++i) { string package = includes[i]->get_name(); + string prefix = php_path(includes[i]); f_types_ << - "include_once $GLOBALS['THRIFT_ROOT'].'/packages/" << package << "/" << package << "_types.php';" << endl; + "include_once $GLOBALS['THRIFT_ROOT'].'/packages/" << prefix << "/" << package << "_types.php';" << endl; } f_types_ << endl; @@ -259,7 +283,7 @@ void t_php_generator::init_generator() { f_consts_ << "get_extends() != NULL) { f_service_ << - "include_once $GLOBALS['THRIFT_ROOT'].'/packages/" << tservice->get_extends()->get_program()->get_name() << "/" << tservice->get_extends()->get_name() << ".php';" << endl; + "include_once $GLOBALS['THRIFT_ROOT'].'/packages/" << php_path(tservice->get_extends()->get_program()) << "/" << tservice->get_extends()->get_name() << ".php';" << endl; } f_service_ << -- 2.17.1