r1317@opsdev009 (orig r70384): mcslee | 2007-11-16 16:32:36 -0800
[amiethrift.git] / compiler / cpp / src / generate / t_php_generator.h
blob3f85feaaf5b0ac5152041a956766aba40887327a
1 // Copyright (c) 2006- Facebook
2 // Distributed under the Thrift Software License
3 //
4 // See accompanying file LICENSE or visit the Thrift site at:
5 // http://developers.facebook.com/thrift/
7 #ifndef T_PHP_GENERATOR_H
8 #define T_PHP_GENERATOR_H
10 #include <string>
11 #include <fstream>
12 #include <iostream>
13 #include <vector>
15 #include "t_oop_generator.h"
17 /**
18 * PHP code generator.
20 * @author Mark Slee <mcslee@facebook.com>
22 class t_php_generator : public t_oop_generator {
23 public:
24 t_php_generator(t_program* program,
25 bool binary_inline=false,
26 bool rest=false,
27 bool phps=false,
28 bool autoload=false) :
29 t_oop_generator(program),
30 binary_inline_(binary_inline),
31 rest_(rest),
32 phps_(phps),
33 autoload_(autoload) {
34 out_dir_base_ = (binary_inline_ ? "gen-phpi" : "gen-php");
37 /**
38 * Init and close methods
41 void init_generator();
42 void close_generator();
44 /**
45 * Program-level generation functions
48 void generate_typedef (t_typedef* ttypedef);
49 void generate_enum (t_enum* tenum);
50 void generate_const (t_const* tconst);
51 void generate_struct (t_struct* tstruct);
52 void generate_xception (t_struct* txception);
53 void generate_service (t_service* tservice);
55 std::string render_const_value(t_type* type, t_const_value* value);
57 /**
58 * Structs!
61 void generate_php_struct(t_struct* tstruct, bool is_exception);
62 void generate_php_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false);
63 void _generate_php_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false);
64 void generate_php_struct_reader(std::ofstream& out, t_struct* tstruct);
65 void generate_php_struct_writer(std::ofstream& out, t_struct* tstruct);
66 void generate_php_function_helpers(t_function* tfunction);
68 void generate_php_type_spec(std::ofstream &out, t_type* t);
69 void generate_php_struct_spec(std::ofstream &out, t_struct* tstruct);
71 /**
72 * Service-level generation functions
75 void generate_service_helpers (t_service* tservice);
76 void generate_service_interface (t_service* tservice);
77 void generate_service_rest (t_service* tservice);
78 void generate_service_client (t_service* tservice);
79 void _generate_service_client (std::ofstream &out, t_service* tservice);
80 void generate_service_processor (t_service* tservice);
81 void generate_process_function (t_service* tservice, t_function* tfunction);
83 /**
84 * Serialization constructs
87 void generate_deserialize_field (std::ofstream &out,
88 t_field* tfield,
89 std::string prefix="",
90 bool inclass=false);
92 void generate_deserialize_struct (std::ofstream &out,
93 t_struct* tstruct,
94 std::string prefix="");
96 void generate_deserialize_container (std::ofstream &out,
97 t_type* ttype,
98 std::string prefix="");
100 void generate_deserialize_set_element (std::ofstream &out,
101 t_set* tset,
102 std::string prefix="");
104 void generate_deserialize_map_element (std::ofstream &out,
105 t_map* tmap,
106 std::string prefix="");
108 void generate_deserialize_list_element (std::ofstream &out,
109 t_list* tlist,
110 std::string prefix="");
112 void generate_serialize_field (std::ofstream &out,
113 t_field* tfield,
114 std::string prefix="");
116 void generate_serialize_struct (std::ofstream &out,
117 t_struct* tstruct,
118 std::string prefix="");
120 void generate_serialize_container (std::ofstream &out,
121 t_type* ttype,
122 std::string prefix="");
124 void generate_serialize_map_element (std::ofstream &out,
125 t_map* tmap,
126 std::string kiter,
127 std::string viter);
129 void generate_serialize_set_element (std::ofstream &out,
130 t_set* tmap,
131 std::string iter);
133 void generate_serialize_list_element (std::ofstream &out,
134 t_list* tlist,
135 std::string iter);
138 * Helper rendering functions
141 std::string php_includes();
142 std::string declare_field(t_field* tfield, bool init=false, bool obj=false);
143 std::string function_signature(t_function* tfunction, std::string prefix="");
144 std::string argument_list(t_struct* tstruct);
145 std::string type_to_cast(t_type* ttype);
146 std::string type_to_enum(t_type* ttype);
148 std::string php_namespace(t_program* p) {
149 std::string ns = p->get_php_namespace();
150 return ns.size() ? (ns + "_") : "";
153 private:
156 * File streams
158 std::ofstream f_types_;
159 std::ofstream f_consts_;
160 std::ofstream f_helpers_;
161 std::ofstream f_service_;
164 * Generate protocol-independent template? Or Binary inline code?
166 bool binary_inline_;
169 * Generate a REST handler class
171 bool rest_;
174 * Generate stubs for a PHP server
176 bool phps_;
179 * Generate PHP code that uses autoload
181 bool autoload_;
185 #endif