Make inheriting from TBase and using inherited read method optional in generated Thrift PHP code
Reviewed By: dreiss, akhil
Test Plan: Generate falcon etc. without -phpo and check it out
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665372 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 9cf98ae..b1182d1 100644
--- a/compiler/cpp/src/generate/t_php_generator.cc
+++ b/compiler/cpp/src/generate/t_php_generator.cc
@@ -402,14 +402,16 @@
"class " << php_namespace(tstruct->get_program()) << tstruct->get_name();
if (is_exception) {
out << " extends TException";
- } else {
+ } else if (oop_) {
out << " extends TBase";
}
out <<
" {" << endl;
indent_up();
- indent(out) << "static $_TSPEC;" << endl << endl;
+ if (oop_) {
+ indent(out) << "static $_TSPEC;" << endl << endl;
+ }
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
string dval = "null";
@@ -424,27 +426,31 @@
out << endl;
// Generate constructor from array
- string param = (members.size() > 0) ? "$vals=null" : "";
- out <<
- indent() << "public function __construct(" << param << ") {" << endl;
- indent_up();
-
- generate_php_struct_spec(out, tstruct);
-
- if (members.size() > 0) {
- for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_type* t = get_true_type((*m_iter)->get_type());
- if ((*m_iter)->get_value() != NULL && (t->is_struct() || t->is_xception())) {
- indent(out) << "$this->" << (*m_iter)->get_name() << " = " << render_const_value(t, (*m_iter)->get_value()) << ";" << endl;
- }
- }
+ if (oop_ || members.size() > 0) {
+ string param = (members.size() > 0) ? "$vals=null" : "";
out <<
- indent() << "if (is_array($vals)) {" << endl <<
- indent() << " parent::__construct(self::$_TSPEC, $vals);" << endl <<
- indent() << "}" << endl;
+ indent() << "public function __construct(" << param << ") {" << endl;
+ indent_up();
+
+ if (oop_) {
+ generate_php_struct_spec(out, tstruct);
+ }
+
+ if (members.size() > 0) {
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ t_type* t = get_true_type((*m_iter)->get_type());
+ if ((*m_iter)->get_value() != NULL && (t->is_struct() || t->is_xception())) {
+ indent(out) << "$this->" << (*m_iter)->get_name() << " = " << render_const_value(t, (*m_iter)->get_value()) << ";" << endl;
+ }
+ }
+ out <<
+ indent() << "if (is_array($vals)) {" << endl <<
+ indent() << " parent::__construct(self::$_TSPEC, $vals);" << endl <<
+ indent() << "}" << endl;
+ }
+ scope_down(out);
+ out << endl;
}
- scope_down(out);
- out << endl;
out <<
indent() << "public function getName() {" << endl <<
@@ -473,10 +479,11 @@
"public function read($input)" << endl;
scope_up(out);
- // TODO(mcslee): Testing this new jonx!
- indent(out) << "return $this->_read('" << tstruct->get_name() << "', self::$_TSPEC, $input);" << endl;
- scope_down(out);
- return;
+ if (oop_) {
+ indent(out) << "return $this->_read('" << tstruct->get_name() << "', self::$_TSPEC, $input);" << endl;
+ scope_down(out);
+ return;
+ }
out <<
indent() << "$xfer = 0;" << endl <<
@@ -599,10 +606,11 @@
}
indent_up();
- // TODO(mcslee): Testing this new j0nx
- indent(out) << "return $this->_write('" << tstruct->get_name() << "', self::$_TSPEC, $output);" << endl;
- scope_down(out);
- return;
+ if (oop_) {
+ indent(out) << "return $this->_write('" << tstruct->get_name() << "', self::$_TSPEC, $output);" << endl;
+ scope_down(out);
+ return;
+ }
indent(out) <<
"$xfer = 0;" << endl;
diff --git a/compiler/cpp/src/generate/t_php_generator.h b/compiler/cpp/src/generate/t_php_generator.h
index 3f85fea..be2ca60 100644
--- a/compiler/cpp/src/generate/t_php_generator.h
+++ b/compiler/cpp/src/generate/t_php_generator.h
@@ -25,12 +25,14 @@
bool binary_inline=false,
bool rest=false,
bool phps=false,
- bool autoload=false) :
+ bool autoload=false,
+ bool oop=false) :
t_oop_generator(program),
binary_inline_(binary_inline),
rest_(rest),
phps_(phps),
- autoload_(autoload) {
+ autoload_(autoload),
+ oop_(oop) {
out_dir_base_ = (binary_inline_ ? "gen-phpi" : "gen-php");
}
@@ -180,6 +182,11 @@
*/
bool autoload_;
+ /**
+ * Whether to use OOP base class TBase
+ */
+ bool oop_;
+
};
#endif
diff --git a/compiler/cpp/src/main.cc b/compiler/cpp/src/main.cc
index da4ce16..0a47e53 100644
--- a/compiler/cpp/src/main.cc
+++ b/compiler/cpp/src/main.cc
@@ -142,6 +142,7 @@
bool gen_phpi = false;
bool gen_phps = true;
bool gen_phpa = false;
+bool gen_phpo = false;
bool gen_rest = false;
bool gen_perl = false;
bool gen_erl = false;
@@ -566,6 +567,7 @@
fprintf(stderr, " -phps Generate PHP server stubs (with -php)\n");
fprintf(stderr, " -phpl Generate PHP-lite (with -php)\n");
fprintf(stderr, " -phpa Generate PHP with autoload (with -php)\n");
+ fprintf(stderr, " -phpo Generate PHP with object oriented subclasses (with -php)\n");
fprintf(stderr, " -py Generate Python output files\n");
fprintf(stderr, " -rb Generate Ruby output files\n");
fprintf(stderr, " -xsd Generate XSD output files\n");
@@ -818,7 +820,7 @@
if (gen_php) {
pverbose("Generating PHP\n");
- t_php_generator* php = new t_php_generator(program, false, gen_rest, gen_phps, gen_phpa);
+ t_php_generator* php = new t_php_generator(program, false, gen_rest, gen_phps, gen_phpa, gen_phpo);
php->generate_program();
delete php;
}
@@ -971,6 +973,11 @@
gen_phps = false;
}
gen_phpa = true;
+ } else if (strcmp(arg, "-phpo") == 0) {
+ if (!gen_php) {
+ gen_php = true;
+ }
+ gen_phpo = true;
} else if (strcmp(arg, "-rest") == 0) {
gen_rest = true;
} else if (strcmp(arg, "-py") == 0) {