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..e2e4e9f 100644
--- a/compiler/cpp/src/generate/t_php_generator.cc
+++ b/compiler/cpp/src/generate/t_php_generator.cc
@@ -102,6 +102,7 @@
void generate_typedef (t_typedef* ttypedef);
void generate_enum (t_enum* tenum);
void generate_const (t_const* tconst);
+ void generate_consts (vector<t_const*> consts);
void generate_struct (t_struct* tstruct);
void generate_xception (t_struct* txception);
void generate_service (t_service* tservice);
@@ -480,17 +481,57 @@
}
/**
+ * Generate constant class
+ *
+ * Override the one from t_generator
+ */
+void t_php_generator::generate_consts(vector<t_const*> consts) {
+ vector<t_const*>::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 0000000..b66dda9
--- /dev/null
+++ b/lib/php/lib/Thrift/Type/TConstant.php
@@ -0,0 +1,53 @@
+<?php
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * @package thrift
+ */
+
+namespace Thrift\Type;
+
+/**
+ * Base class for constant Management
+ *
+ * @author Xavier HAUSHERR <xavier.hausherr@ebuzzing.com>
+ */
+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;
+ }
+}