PageRenderTime 60ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/runtime/base/config.h

https://gitlab.com/Manu343726/hhvm
C Header | 302 lines | 210 code | 32 blank | 60 comment | 9 complexity | 9c9293daac261489ea02abe217260351 MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifndef incl_HPHP_CONFIG_H_
  17. #define incl_HPHP_CONFIG_H_
  18. #include <folly/dynamic.h>
  19. #include "hphp/util/hdf.h"
  20. namespace HPHP {
  21. ///////////////////////////////////////////////////////////////////////////////
  22. typedef folly::dynamic IniSettingMap;
  23. // forward declarations
  24. const folly::dynamic* ini_iterate(const IniSettingMap& ini,
  25. const std::string& name);
  26. template<class T>
  27. void ini_bind_core_system(const std::string& name, T *p);
  28. template<class T>
  29. bool ini_set_helper(const std::string& name, T& value);
  30. /**
  31. * Parts of the language can individually be made stricter, warning or
  32. * erroring when there's dangerous/unintuive usage; for example,
  33. * array_fill_keys() with non-int/string keys: Hack.Lang.StrictArrayFillKeys
  34. */
  35. enum class HackStrictOption {
  36. OFF, // PHP5 behavior
  37. WARN,
  38. ON
  39. };
  40. /*
  41. * Normalizes hdf string names to their ini counterparts
  42. *
  43. * We have special handling for a few hdf strings such as those containing
  44. * MySQL, Eval, IPv[4|6] and EnableHipHopSyntax
  45. */
  46. std::string hdfToIni(const std::string&);
  47. struct Config {
  48. static void ParseConfigFile(const std::string &filename, IniSettingMap &ini,
  49. Hdf &hdf);
  50. static void ParseIniFile(const std::string &filename);
  51. static void ParseIniFile(const std::string &filename, IniSettingMap &ini,
  52. const bool constants_only = false);
  53. static void ParseHdfFile(const std::string &filename, Hdf &hdf);
  54. // Parse and process a .ini string (e.g., -d)
  55. static void ParseIniString(const std::string iniStr, IniSettingMap &ini);
  56. // Parse and process a .hdf string (e.g., -v)
  57. static void ParseHdfString(const std::string hdfStr, Hdf &hdf);
  58. /** Prefer the Bind() over the GetFoo() as it makes ini_get() work too. */
  59. static void Bind(bool& loc, const IniSettingMap &ini,
  60. const Hdf& config, const bool defValue = false);
  61. static void Bind(const char*& loc, const IniSettingMap &ini,
  62. const Hdf& config, const char *defValue = nullptr);
  63. static void Bind(std::string& loc, const IniSettingMap &ini,
  64. const Hdf& config, const std::string defValue = "");
  65. static void Bind(char& loc, const IniSettingMap &ini,
  66. const Hdf& config, const char defValue = 0);
  67. static void Bind(unsigned char& loc,const IniSettingMap &ini,
  68. const Hdf& config, const unsigned char defValue = 0);
  69. static void Bind(int16_t& loc, const IniSettingMap &ini,
  70. const Hdf& config, const int16_t defValue = 0);
  71. static void Bind(uint16_t& loc, const IniSettingMap &ini,
  72. const Hdf& config, const uint16_t defValue = 0);
  73. static void Bind(int32_t& loc, const IniSettingMap &ini,
  74. const Hdf& config, const int32_t defValue = 0);
  75. static void Bind(uint32_t& loc, const IniSettingMap &ini,
  76. const Hdf& config, const uint32_t defValue = 0);
  77. static void Bind(int64_t& loc, const IniSettingMap &ini,
  78. const Hdf& config, const int64_t defValue = 0);
  79. static void Bind(uint64_t& loc, const IniSettingMap &ini,
  80. const Hdf& config, const uint64_t defValue = 0);
  81. static void Bind(double& loc, const IniSettingMap &ini,
  82. const Hdf& config, const double defValue = 0);
  83. static void Bind(HackStrictOption& loc, const IniSettingMap &ini,
  84. const Hdf& config);
  85. /**
  86. * These Bind()s should be used for ini settings. Specifically, they should
  87. * be used when the bound setting is needed before the main ini processing
  88. * pass. Unlike IniSetting::Bind, these bindings will fetch the value in
  89. * an ini setting if it is set otherwise it will use the defValue.
  90. */
  91. static void Bind(bool& loc, const IniSettingMap &ini,
  92. const std::string name, const bool defValue = false);
  93. static void Bind(const char*& loc, const IniSettingMap &ini,
  94. const std::string name, const char *defValue = nullptr);
  95. static void Bind(std::string& loc, const IniSettingMap &ini,
  96. const std::string name, const std::string defValue = "");
  97. static void Bind(char& loc, const IniSettingMap &ini,
  98. const std::string name, const char defValue = 0);
  99. static void Bind(unsigned char& loc,const IniSettingMap &ini,
  100. const std::string name, const unsigned char defValue = 0);
  101. static void Bind(int16_t& loc, const IniSettingMap &ini,
  102. const std::string name, const int16_t defValue = 0);
  103. static void Bind(uint16_t& loc, const IniSettingMap &ini,
  104. const std::string name, const uint16_t defValue = 0);
  105. static void Bind(int32_t& loc, const IniSettingMap &ini,
  106. const std::string name, const int32_t defValue = 0);
  107. static void Bind(uint32_t& loc, const IniSettingMap &ini,
  108. const std::string name, const uint32_t defValue = 0);
  109. static void Bind(int64_t& loc, const IniSettingMap &ini,
  110. const std::string name, const int64_t defValue = 0);
  111. static void Bind(uint64_t& loc, const IniSettingMap &ini,
  112. const std::string name, const uint64_t defValue = 0);
  113. static void Bind(double& loc, const IniSettingMap &ini,
  114. const std::string name, const double defValue = 0);
  115. static bool GetBool(const IniSettingMap &ini, const Hdf& config,
  116. const bool defValue = false);
  117. static const char *Get(const IniSettingMap &ini, const Hdf& config,
  118. const char *defValue = nullptr);
  119. static std::string GetString(const IniSettingMap &ini, const Hdf& config,
  120. const std::string defValue = "");
  121. static char GetByte(const IniSettingMap &ini, const Hdf& config,
  122. const char defValue = 0);
  123. static unsigned char GetUByte(const IniSettingMap &ini, const Hdf& config,
  124. const unsigned char defValue = 0);
  125. static int16_t GetInt16(const IniSettingMap &ini, const Hdf& config,
  126. const int16_t defValue = 0);
  127. static uint16_t GetUInt16(const IniSettingMap &ini, const Hdf& config,
  128. const uint16_t defValue = 0);
  129. static int32_t GetInt32(const IniSettingMap &ini, const Hdf& config,
  130. const int32_t defValue = 0);
  131. static uint32_t GetUInt32(const IniSettingMap &ini, const Hdf& config,
  132. const uint32_t defValue = 0);
  133. static int64_t GetInt64(const IniSettingMap &ini, const Hdf& config,
  134. const int64_t defValue = 0);
  135. static uint64_t GetUInt64(const IniSettingMap &ini, const Hdf& config,
  136. const uint64_t defValue = 0);
  137. static double GetDouble(const IniSettingMap &ini, const Hdf& config,
  138. const double defValue = 0);
  139. /**
  140. * INI specific GetXXX methods. Will be used exclusively once we move
  141. * off of HDF.
  142. */
  143. static bool GetBool(const IniSettingMap &ini, const std::string &name,
  144. const bool defValue = false);
  145. static const char *Get(const IniSettingMap &ini, const std::string &name,
  146. const char *defValue = nullptr);
  147. static std::string GetString(const IniSettingMap &ini,
  148. const std::string &name,
  149. const std::string defValue = "");
  150. static char GetByte(const IniSettingMap &ini, const std::string &name,
  151. const char defValue = 0);
  152. static unsigned char GetUByte(const IniSettingMap &ini,
  153. const std::string &name,
  154. const unsigned char defValue = 0);
  155. static int16_t GetInt16(const IniSettingMap &ini, const std::string &name,
  156. const int16_t defValue = 0);
  157. static uint16_t GetUInt16(const IniSettingMap &ini, const std::string &name,
  158. const uint16_t defValue = 0);
  159. static int32_t GetInt32(const IniSettingMap &ini, const std::string &name,
  160. const int32_t defValue = 0);
  161. static uint32_t GetUInt32(const IniSettingMap &ini, const std::string &name,
  162. const uint32_t defValue = 0);
  163. static int64_t GetInt64(const IniSettingMap &ini, const std::string &name,
  164. const int64_t defValue = 0);
  165. static uint64_t GetUInt64(const IniSettingMap &ini, const std::string &name,
  166. const uint64_t defValue = 0);
  167. static double GetDouble(const IniSettingMap &ini, const std::string &name,
  168. const double defValue = 0);
  169. /**
  170. * Use these Iterate methods for iterating over options that are stored as
  171. * objects in runtime options (e.g. FilesMatch). This function iterates over
  172. * the settings passed as ini/hdf, calls back to, generally, the constructor
  173. * of the object in question.
  174. *
  175. * Note: For now, we are not `ini_get()` enabling these type of options as
  176. * it is not trivial to come up with a non-hacky and workable way to store
  177. * the data correctly. Also, as usual, Hdf takes priority.
  178. */
  179. // Only use if an Hdf setting is a possibility from the caller.
  180. static void Iterate(const IniSettingMap &ini, const Hdf &hdf,
  181. std::function<void (const IniSettingMap&,
  182. const Hdf&,
  183. const std::string&)> cb);
  184. // This only supports directly named ini settings. This will become the
  185. // default iterate function when we get rid of Hdf
  186. static void Iterate(const IniSettingMap &ini, const std::string &name,
  187. std::function<void (const IniSettingMap&,
  188. const std::string&)> cb);
  189. /**
  190. * Generic get methods, particularly used for objects like arrays or vectors
  191. **/
  192. // This will go away when we move from Hdf
  193. template<class T>
  194. static void Get(const IniSettingMap &ini, const Hdf& config, T &data) {
  195. auto key = IniName(config);
  196. config.configGet(data);
  197. if (!data.empty()) {
  198. // ensure that hdf is set correctly as the winning value over ini
  199. ini_set_helper(key, data);
  200. return;
  201. }
  202. auto* value = ini.get_ptr(key);
  203. if (!value) {
  204. return;
  205. }
  206. if (value->isArray() || value->isObject()) {
  207. for (auto &pair : value->items()) {
  208. StringInsert(data, pair.first.asString().toStdString(),
  209. pair.second.asString().toStdString());
  210. }
  211. }
  212. }
  213. // This will be replaced with an ini only version when we remove Hdf
  214. template<class T>
  215. static void Bind(T &loc, const IniSettingMap &ini, const Hdf &config) {
  216. Get(ini, config, loc);
  217. ini_bind_core_system(IniName(config), &loc);
  218. }
  219. template<class T>
  220. static void Get(const IniSettingMap &ini, const std::string &name, T &data) {
  221. const folly::dynamic* value = nullptr;
  222. value = ini_iterate(ini, name);
  223. if (!value) {
  224. return;
  225. }
  226. if (value->isArray() || value->isObject()) {
  227. for (auto &pair : value->items()) {
  228. StringInsert(data, pair.first.asString().toStdString(),
  229. pair.second.asString().toStdString());
  230. }
  231. }
  232. }
  233. private:
  234. static std::string IniName(const Hdf& config);
  235. static std::string IniName(const std::string& config);
  236. static void SetParsedIni(IniSettingMap &ini, const std::string confStr,
  237. const std::string filename, bool extensions_only);
  238. static void StringInsert(std::vector<std::string> &values,
  239. const std::string &key,
  240. const std::string &value) {
  241. values.push_back(value);
  242. }
  243. static void StringInsert(boost::container::flat_set<std::string> &values,
  244. const std::string &key,
  245. const std::string &value) {
  246. values.insert(value);
  247. }
  248. static void StringInsert(std::set<std::string, stdltistr> &values,
  249. const std::string &key,
  250. const std::string &value) {
  251. values.insert(value);
  252. }
  253. static void StringInsert(std::set<std::string> &values,
  254. const std::string &key,
  255. const std::string &value) {
  256. values.insert(value);
  257. }
  258. static void StringInsert(std::map<std::string, std::string> &values,
  259. const std::string &key,
  260. const std::string &value) {
  261. values[key] = value;
  262. }
  263. static void StringInsert(hphp_string_imap<std::string> &values,
  264. const std::string &key,
  265. const std::string &value) {
  266. values[key] = value;
  267. }
  268. };
  269. }
  270. #endif /* incl_HPHP_CONFIG_H_ */