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
diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc
index 2d9c90c..04242c4 100644
--- a/compiler/cpp/src/generate/t_php_generator.cc
+++ b/compiler/cpp/src/generate/t_php_generator.cc
@@ -67,6 +67,8 @@
     escape_['$'] = "\\$";
   }
 
+  static bool is_valid_namespace(const std::string& sub_namespace);
+
   /**
    * Init and close methods
    */
@@ -183,6 +185,22 @@
     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 @@
 };
 
 
+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 @@
   const vector<t_program*>& 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 @@
     f_consts_ <<
       "<?php" << endl <<
       autogen_comment() <<
-      "include_once $GLOBALS['THRIFT_ROOT'].'/packages/" + program_name_ + "/" + program_name_ + "_types.php';" << endl <<
+      "include_once $GLOBALS['THRIFT_ROOT'].'/packages/" + php_path(program_) + "/" + program_name_ + "_types.php';" << endl <<
       endl <<
       "$GLOBALS['" << program_name_ << "_CONSTANTS'] = array();" << endl <<
       endl;
@@ -902,11 +926,11 @@
     php_includes();
 
   f_service_ <<
-    "include_once $GLOBALS['THRIFT_ROOT'].'/packages/" << program_name_ << "/" << program_name_ << "_types.php';" << endl;
+    "include_once $GLOBALS['THRIFT_ROOT'].'/packages/" << php_path(program_) << "/" << program_name_ << "_types.php';" << endl;
 
   if (tservice->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_ <<