Guest User

Untitled

a guest
Oct 31st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Class Config
  5. *
  6. */
  7. class Config
  8. {
  9. /** @var array */
  10. private $configs;
  11.  
  12. /** @var string */
  13. private $dsn;
  14.  
  15. /**
  16. * データソース名を適切なものを取得する
  17. * (MySQL/PostgreSQL/CUBRID/SQLite 3/SQLite 2/Oracleに対応)
  18. * @return mixed
  19. */
  20. public function buildDsn()
  21. {
  22. if (preg_match('/^(MySQL)+$/', $this->configs['type'])) {
  23. // MySQLの場合
  24. $this->dsn = 'mysql:';
  25. if (! empty($this->configs['server'])) {
  26. $this->dsn .= 'host=' . $this->checkVar($this->configs['server']);
  27. }
  28. if (! empty($this->configs['port'])) {
  29. $this->dsn .= 'port=' . $this->checkVar($this->configs['port']);
  30. }
  31. if (! empty($this->configs['dbname'])) {
  32. $this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
  33. }
  34. if (! empty($this->configs['charset'])) {
  35. $this->dsn .= 'charset=' . $this->checkVar($this->configs['charset']);
  36. }
  37. if (! empty($this->configs['socket'])) {
  38. $this->dsn .= 'unix_socket=' . $this->checkVar($this->configs['socket']);
  39. }
  40. } elseif (preg_match('/^(PostgreSQL)+$/', $this->configs['type'])) {
  41. // PostgreSQLの場合
  42. $this->dsn = 'pgsql:';
  43. if (! empty($this->configs['server'])) {
  44. $this->dsn .= 'host=' . $this->checkVar($this->configs['server']);
  45. }
  46. if (! empty($this->configs['port'])) {
  47. $this->dsn .= 'port=' . $this->checkVar($this->configs['port']);
  48. }
  49. if (! empty($this->configs['dbname'])) {
  50. $this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
  51. }
  52. if (! empty($this->configs['username'])) {
  53. $this->dsn .= 'user=' . $this->checkVar($this->configs['username']);
  54. }
  55. if (! empty($this->configs['password'])) {
  56. $this->dsn .= 'password=' . $this->checkVar($this->configs['password']);
  57. }
  58. } elseif (preg_match('/^(CUBRID)+$/', $this->configs['type'])) {
  59. $this->dsn = 'cubrid:';
  60. if (! empty($this->configs['server'])) {
  61. $this->dsn .= 'host=' . $this->checkVar($this->configs['server']);
  62. }
  63. if (! empty($this->configs['port'])) {
  64. $this->dsn .= 'port=' . $this->checkVar($this->configs['port']);
  65. }
  66. if (! empty($this->configs['dbname'])) {
  67. $this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
  68. }
  69. } elseif (preg_match('/^(SQLite 3)+$/', $this->configs['type'])) {
  70. // SQLite 3の場合
  71. $this->dsn = 'sqlite:';
  72. if (! empty($this->configs['dbname'])) {
  73. $this->dsn .= $this->checkVar($this->configs['dbname']);
  74. }
  75. } elseif (preg_match('/^(SQLite 2)+$/', $this->configs['type'])) {
  76. // SQLite 2の場合
  77. $this->dsn = 'sqlite2:';
  78. if (! empty($this->configs['dbname'])) {
  79. $this->dsn .= $this->checkVar($this->configs['dbname']);
  80. }
  81. } elseif (preg_match('/^(Oracle)+$/', $this->configs['type'])) {
  82. // Oracleデータベースの場合
  83. $this->dsn = 'oci:';
  84. if (! empty($this->configs['dbname'])) {
  85. $this->dsn .= 'dbname=' . $this->checkVar($this->configs['dbname']);
  86. }
  87. if (! empty($this->configs['charset'])) {
  88. $this->dsn .= 'charset=' . $this->checkVar($this->configs['charset']);
  89. }
  90. }
  91. return preg_replace('/;$/', '', $this->dsn);
  92. }
  93.  
  94. /**
  95. * データベースの文字コードを取得する
  96. * @return mixed|string
  97. */
  98. public function buildCharset()
  99. {
  100. return isset($this->configs['charset']) ? $this->configs['charset'] : '';
  101. }
  102.  
  103. /**
  104. * データベースサーバーを取得
  105. * @return mixed
  106. */
  107. public function buildServer()
  108. {
  109. return isset($this->configs['server']) ? (string)$this->configs['server'] : '' ;
  110. }
  111.  
  112. /**
  113. * データベース名を取得する
  114. * @return mixed
  115. */
  116. public function buildDatabaseName()
  117. {
  118. return isset($this->configs['dbname']) ? (string)$this->configs['dbname'] : '';
  119. }
  120.  
  121. /**
  122. * データベースのポートを取得する
  123. * @return mixed
  124. */
  125. public function buildPort()
  126. {
  127. return isset($this->configs['port']) ? $this->configs['port'] : '';
  128. }
  129.  
  130. /**
  131. * Unixソケットを取得する
  132. * @return mixed
  133. */
  134. public function buildSocket()
  135. {
  136. return isset($this->configs['socket']) ? (string)$this->configs['socket'] : '';
  137. }
  138.  
  139. /**
  140. * データベースユーザー名を取得
  141. * @return string
  142. */
  143. public function buildUserName()
  144. {
  145. return isset($this->configs['username']) ? (string)$this->configs['username'] : '' ;
  146. }
  147.  
  148. /**
  149. * データベースのパスワードを取得
  150. * @return string
  151. */
  152. public function buildPassword()
  153. {
  154. return isset($this->configs['password']) ? (string)$this->configs['password'] : '' ;
  155. }
  156.  
  157. /**
  158. * 変数にセットされているかチェック
  159. * (buildDsnメソッド内で使用)
  160. * @param $var
  161. * @return string
  162. */
  163. private function checkVar($var)
  164. {
  165. // 渡されたものが nullもしくは空っぽの場合
  166. if (is_null($var) || empty($var)) {
  167. return $var . '';
  168. } elseif (isset($var)) {
  169. return $var . ';';
  170. }
  171. return $var;
  172. }
  173.  
  174. /**
  175. * Unixソケットを設定する
  176. * @param null $socket
  177. * @return $this
  178. */
  179. public function databaseSocket($socket = null)
  180. {
  181. if (! empty($socket)) {
  182. $this->configs['socket'] = $socket;
  183. }
  184. return $this;
  185. }
  186.  
  187. /**
  188. * 文字コードを設定する
  189. * @param null $charset
  190. * @return $this
  191. */
  192. public function databaseCharset($charset = null)
  193. {
  194. if (! empty($charset)) {
  195. $this->configs['charset'] = $charset;
  196. }
  197. return $this;
  198. }
  199.  
  200. /**
  201. * データベース名を設定する
  202. * @param null $dbname
  203. * @return $this
  204. */
  205. public function databaseName($dbname = null)
  206. {
  207. if (! empty($dbname)) {
  208. $this->configs['dbname'] = $dbname;
  209. }
  210. return $this;
  211. }
  212.  
  213. /**
  214. * データベースのパスワードを設定する
  215. * @param null $password
  216. * @return $this
  217. */
  218. public function databasePassword($password = null)
  219. {
  220. if (! empty($password)) {
  221. $this->configs['password'] = $password;
  222. }
  223. return $this;
  224. }
  225.  
  226. /**
  227. * データベースユーザー名を設定する
  228. * @param null $username
  229. * @return $this
  230. */
  231. public function databaseUserName($username = null)
  232. {
  233. if (! empty($username)) {
  234. $this->configs['username'] = $username;
  235. }
  236. return $this;
  237. }
  238.  
  239. /**
  240. * データベースのポートを設定する
  241. * @param null $port
  242. * @return $this
  243. */
  244. public function databasePort($port = null)
  245. {
  246. if (! empty($port)) {
  247. $this->configs['port'] = $port;
  248. }
  249. return $this;
  250. }
  251.  
  252. /**
  253. * データベースサーバーを設定する
  254. * @param null $server
  255. * @return $this
  256. */
  257. public function databaseServer($server = null)
  258. {
  259. if (! empty($server)) {
  260. $this->configs['server'] = $server;
  261. }
  262. return $this;
  263. }
  264.  
  265. /**
  266. * データベースの種類を設定する
  267. * @param null $type
  268. * @return $this
  269. */
  270. public function databaseType($type = null)
  271. {
  272. if (! empty($type)) {
  273. $this->configs['type'] = $type;
  274. }
  275. return $this;
  276. }
  277.  
  278. /**
  279. * メソッドチェーンを作成する
  280. * @return Config
  281. */
  282. public static function create()
  283. {
  284. return new self;
  285. }
  286. }
Add Comment
Please, Sign In to add comment