Merge commit 'remotes/trunk'
[amiethrift.git] / compiler / cpp / src / generate / t_php_generator.h
blobbe2ca601e68c23370c0d86bab130aafefec2a376
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 bool oop=false) :
30 t_oop_generator(program),
31 binary_inline_(binary_inline),
32 rest_(rest),
33 phps_(phps),
34 autoload_(autoload),
35 oop_(oop) {
36 out_dir_base_ = (binary_inline_ ? "gen-phpi" : "gen-php");
39 /**
40 * Init and close methods
43 void init_generator();
44 void close_generator();
46 /**
47 * Program-level generation functions
50 void generate_typedef (t_typedef* ttypedef);
51 void generate_enum (t_enum* tenum);
52 void generate_const (t_const* tconst);
53 void generate_struct (t_struct* tstruct);
54 void generate_xception (t_struct* txception);
55 void generate_service (t_service* tservice);
57 std::string render_const_value(t_type* type, t_const_value* value);
59 /**
60 * Structs!
63 void generate_php_struct(t_struct* tstruct, bool is_exception);
64 void generate_php_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false);
65 void _generate_php_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false);
66 void generate_php_struct_reader(std::ofstream& out, t_struct* tstruct);
67 void generate_php_struct_writer(std::ofstream& out, t_struct* tstruct);
68 void generate_php_function_helpers(t_function* tfunction);
70 void generate_php_type_spec(std::ofstream &out, t_type* t);
71 void generate_php_struct_spec(std::ofstream &out, t_struct* tstruct);
73 /**
74 * Service-level generation functions
77 void generate_service_helpers (t_service* tservice);
78 void generate_service_interface (t_service* tservice);
79 void generate_service_rest (t_service* tservice);
80 void generate_service_client (t_service* tservice);
81 void _generate_service_client (std::ofstream &out, t_service* tservice);
82 void generate_service_processor (t_service* tservice);
83 void generate_process_function (t_service* tservice, t_function* tfunction);
85 /**
86 * Serialization constructs
89 void generate_deserialize_field (std::ofstream &out,
90 t_field* tfield,
91 std::string prefix="",
92 bool inclass=false);
94 void generate_deserialize_struct (std::ofstream &out,
95 t_struct* tstruct,
96 std::string prefix="");
98 void generate_deserialize_container (std::ofstream &out,
99 t_type* ttype,
100 std::string prefix="");
102 void generate_deserialize_set_element (std::ofstream &out,
103 t_set* tset,
104 std::string prefix="");
106 void generate_deserialize_map_element (std::ofstream &out,
107 t_map* tmap,
108 std::string prefix="");
110 void generate_deserialize_list_element (std::ofstream &out,
111 t_list* tlist,
112 std::string prefix="");
114 void generate_serialize_field (std::ofstream &out,
115 t_field* tfield,
116 std::string prefix="");
118 void generate_serialize_struct (std::ofstream &out,
119 t_struct* tstruct,
120 std::string prefix="");
122 void generate_serialize_container (std::ofstream &out,
123 t_type* ttype,
124 std::string prefix="");
126 void generate_serialize_map_element (std::ofstream &out,
127 t_map* tmap,
128 std::string kiter,
129 std::string viter);
131 void generate_serialize_set_element (std::ofstream &out,
132 t_set* tmap,
133 std::string iter);
135 void generate_serialize_list_element (std::ofstream &out,
136 t_list* tlist,
137 std::string iter);
140 * Helper rendering functions
143 std::string php_includes();
144 std::string declare_field(t_field* tfield, bool init=false, bool obj=false);
145 std::string function_signature(t_function* tfunction, std::string prefix="");
146 std::string argument_list(t_struct* tstruct);
147 std::string type_to_cast(t_type* ttype);
148 std::string type_to_enum(t_type* ttype);
150 std::string php_namespace(t_program* p) {
151 std::string ns = p->get_php_namespace();
152 return ns.size() ? (ns + "_") : "";
155 private:
158 * File streams
160 std::ofstream f_types_;
161 std::ofstream f_consts_;
162 std::ofstream f_helpers_;
163 std::ofstream f_service_;
166 * Generate protocol-independent template? Or Binary inline code?
168 bool binary_inline_;
171 * Generate a REST handler class
173 bool rest_;
176 * Generate stubs for a PHP server
178 bool phps_;
181 * Generate PHP code that uses autoload
183 bool autoload_;
186 * Whether to use OOP base class TBase
188 bool oop_;
192 #endif