[
https://issues.apache.org/jira/browse/THRIFT-1241?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13068435#comment-13068435
]
Darius Staisiunas commented on THRIFT-1241:
---
--- compiler/cpp/src/generate/t_php_generator.cc.orig 2011-06-23
12:28:07.0 +0300
+++ compiler/cpp/src/generate/t_php_generator.cc2011-07-20
18:09:05.403366001 +0300
@@ -29,6 +29,12 @@
#include "platform.h"
using namespace std;
+#define NSGLOBAL (nsglobal_.size() ? nsglobal_ : "")
+#define NSGLOBAL_A ("\\" + NSGLOBAL )
+#define NSGLOBAL_B ( NSGLOBAL + "\\")
+#define NSGLOBAL_AB ("\\" + NSGLOBAL + "\\")
+#define NS_ROOT ( namespace53_ ? "\\" : "")
+
/**
* PHP code generator.
@@ -60,6 +66,18 @@
iter = parsed_options.find("oop");
oop_ = (iter != parsed_options.end());
+iter = parsed_options.find("namespace53");
+namespace53_ = (iter != parsed_options.end());
+
+iter = parsed_options.find("nsglobal");
+if(iter != parsed_options.end()) {
+ if(namespace53_) nsglobal_ = iter->second;
+ else throw "cannot use nsglobal without namespace53.";
+}
+else {
+ nsglobal_ = ""; // by default global namespace is empty
+}
+
if (oop_ && binary_inline_) {
throw "oop and inlined are mutually exclusive.";
}
@@ -183,7 +201,24 @@
std::string php_namespace(t_program* p) {
std::string ns = p->get_namespace("php");
-return ns.size() ? (ns + "_") : "";
+size_t position = ns.find( "." );
+while (position != string::npos) {
+ ns.replace(position, 1, "\\");
+ position = ns.find(".", position+1);
+}
+if(namespace53_) return (nsglobal_.size() ? NSGLOBAL_AB : NSGLOBAL_B) +
(ns.size() ? (ns + "\\") : "");
+else return "";
+ }
+
+ std::string php_namespace_suffix(const t_program* p) {
+std::string ns = p->get_namespace("php");
+size_t position = ns.find( "." );
+while (position != string::npos) {
+ ns.replace(position, 1, "\\");
+ position = ns.find(".", position+1);
+}
+if (namespace53_) return (nsglobal_.size() ? NSGLOBAL_B : NSGLOBAL) + ns;
+else return "";
}
std::string php_path(t_program* p) {
@@ -199,7 +234,7 @@
}
}
-return ns + '/' + p->get_name();
+return ((namespace53_) ? ns + '/' : "" ) + p->get_name();
}
private:
@@ -237,7 +272,16 @@
* Whether to use OOP base class TBase
*/
bool oop_;
-
+
+ /**
+ * Generate namespaces in PHP5.3 style
+ */
+ bool namespace53_;
+
+ /**
+ * Global namespace for PHP 5.3
+ */
+ std::string nsglobal_;
};
@@ -263,9 +307,9 @@
// Print header
f_types_ <<
-"& includes = program_->get_includes();
@@ -282,8 +326,9 @@
string f_consts_name = package_dir_+program_name_+"_constants.php";
f_consts_.open(f_consts_name.c_str());
f_consts_ <<
- "get_program()) << tenum->get_name()
<< " {" << endl;
+"final class " << tenum->get_name() << " {" << endl;
indent_up();
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
@@ -420,7 +465,7 @@
} else if (type->is_enum()) {
indent(out) << value->get_integer();
} else if (type->is_struct() || type->is_xception()) {
-out << "new " << php_namespace(type->get_program()) << type->get_name() <<
"(array(" << endl;
+out << "new " << NS_ROOT << php_namespace(type->get_program()) <<
type->get_name() << "(array(" << endl;
indent_up();
const vector& fields = ((t_struct*)type)->get_members();
vector::const_iterator f_iter;
@@ -513,7 +558,7 @@
void t_php_generator::generate_php_type_spec(ofstream& out,
t_type* t) {
t = get_true_type(t);
- indent(out) << "'type' => " << type_to_enum(t) << "," << endl;
+ indent(out) << "'type' => " << NS_ROOT << type_to_enum(t) << "," << endl;
if (t->is_base_type() || t->is_enum()) {
// Noop, type is all we need
@@ -522,8 +567,8 @@
} else if (t->is_map()) {
t_type* ktype = get_true_type(((t_map*)t)->get_key_type());
t_type* vtype = get_true_type(((t_map*)t)->get_val_type());
-indent(out) << "'ktype' => " << type_to_enum(ktype) << "," << endl;
-indent(out) << "'vtype' => " << type_to_enum(vtype) << "," << endl;
+indent(out) << "'ktype' => " << NS_ROOT << type_to_enum(ktype) << "," <<
endl;
+indent(out) << "'vtype' => " << NS_ROOT << type_to_enum(vtype) << "," <<
endl;
indent(out) << "'key' => array(" << endl;
indent_up();
generate_php_type_spec(out, ktype);
@@ -541,7 +586,7 @@
} else {
etype = get_true_type(((t_set*)t)->get_elem_type());
}
-indent(out) << "'etype' => " << type_to_enum(etype) <<"," << endl;
+indent(out) << "'etype' => " << NS_ROOT << type_to_enum(etype) <<"," <<
endl;
indent(out) << "'elem' => array(" << endl;
indent_up();
generate_php_type_spec(out, etype);
@@ -621,11 +666,11 @@
vector::