You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2014/07/04 21:46:26 UTC

git commit: THRIFT-1926 PHP Constant Generation Refactoring Client: PHP Patch: Xavier HAUSHERR

Repository: thrift
Updated Branches:
  refs/heads/master 45abf12f1 -> bc2ca4e5d


THRIFT-1926 PHP Constant Generation Refactoring
Client: PHP
Patch: Xavier HAUSHERR


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/bc2ca4e5
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/bc2ca4e5
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/bc2ca4e5

Branch: refs/heads/master
Commit: bc2ca4e5d76486729b5ebc0e5318b822cd87a438
Parents: 45abf12
Author: Jens Geyer <je...@apache.org>
Authored: Fri Jul 4 21:16:09 2014 +0200
Committer: Jens Geyer <je...@apache.org>
Committed: Fri Jul 4 21:45:55 2014 +0200

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_php_generator.cc | 47 ++++++++++++++++++--
 lib/php/lib/Thrift/Type/TConstant.php        | 53 +++++++++++++++++++++++
 2 files changed, 97 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/bc2ca4e5/compiler/cpp/src/generate/t_php_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc
index 124039b..e2e4e9f 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<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 @@ void t_php_generator::generate_enum(t_enum* tenum) {
 }
 
 /**
+ * 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;
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/thrift/blob/bc2ca4e5/lib/php/lib/Thrift/Type/TConstant.php
----------------------------------------------------------------------
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 <xa...@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;
+    }
+}