From: Jens Geyer Date: Fri, 4 Jul 2014 19:16:09 +0000 (+0200) Subject: THRIFT-1926 PHP Constant Generation Refactoring X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=bc2ca4e5d76486729b5ebc0e5318b822cd87a438;p=common%2Fthrift.git THRIFT-1926 PHP Constant Generation Refactoring Client: PHP Patch: Xavier HAUSHERR --- diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc index 124039b3..e2e4e9f1 100644 --- a/compiler/cpp/src/generate/t_php_generator.cc +++ b/compiler/cpp/src/generate/t_php_generator.cc @@ -102,6 +102,7 @@ class t_php_generator : public t_oop_generator { void generate_typedef (t_typedef* ttypedef); void generate_enum (t_enum* tenum); void generate_const (t_const* tconst); + void generate_consts (vector consts); void generate_struct (t_struct* tstruct); void generate_xception (t_struct* txception); void generate_service (t_service* tservice); @@ -479,18 +480,58 @@ void t_php_generator::generate_enum(t_enum* tenum) { f_types_ << "}" << endl << endl; } +/** + * Generate constant class + * + * Override the one from t_generator + */ +void t_php_generator::generate_consts(vector consts) { + vector::iterator c_iter; + + // Create class only if needed + if(consts.size() > 0) + { + f_types_ << "final class Constant extends \\Thrift\\Type\\TConstant {" << endl; + indent_up(); + + // Create static property + for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) { + string name = (*c_iter)->get_name(); + + indent(f_types_) << "static protected $" << name << ";" << endl; + } + + // Create init function + for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) { + string name = (*c_iter)->get_name(); + + f_types_ << endl; + + indent(f_types_) << "static protected function init_" << name << "() {" << endl; + indent_up(); + + indent(f_types_) << "return "; + generate_const(*c_iter); + f_types_ << ";" << endl; + + indent_down(); + indent(f_types_) << "}" << endl; + } + + indent_down(); + f_types_ << "}" << endl << endl; + } +} + /** * Generate a constant value */ void t_php_generator::generate_const(t_const* tconst) { t_type* type = tconst->get_type(); - string name = tconst->get_name(); t_const_value* value = tconst->get_value(); generate_php_doc(f_types_, tconst); - f_types_ << "$GLOBALS['" << program_name_ << "_CONSTANTS']['" << name << "'] = "; f_types_ << render_const_value(type, value); - f_types_ << ";" << endl << endl; } /** diff --git a/lib/php/lib/Thrift/Type/TConstant.php b/lib/php/lib/Thrift/Type/TConstant.php new file mode 100644 index 00000000..b66dda9f --- /dev/null +++ b/lib/php/lib/Thrift/Type/TConstant.php @@ -0,0 +1,53 @@ + + */ +abstract class TConstant +{ + /** + * Don't instanciate this class + */ + protected function __construct() {} + + /** + * Get a constant value + * @param string $constant + * @return mixed + */ + public static function get($constant) + { + if(is_null(static::$$constant)) + { + static::$$constant = call_user_func( + sprintf('static::init_%s', $constant) + ); + } + + return static::$$constant; + } +}