|
1 | | -<?php |
2 | | - |
3 | | -namespace Zyimm\dbStructSync\connector; |
4 | | - |
5 | | -use Exception; |
6 | | -use PDO; |
7 | | - |
8 | | -/** |
9 | | - * Class Mysql |
10 | | - * |
11 | | - * @package Zyimm\dbStructSync\connector |
12 | | - */ |
13 | | -class Mysql |
14 | | -{ |
15 | | - /** |
16 | | - * @var PDO |
17 | | - */ |
18 | | - private $localConnection; |
19 | | - |
20 | | - /** |
21 | | - * @var PDO |
22 | | - */ |
23 | | - private $devConnection; |
24 | | - |
25 | | - public $localDb = []; |
26 | | - |
27 | | - public $devDb = []; |
28 | | - |
29 | | - /** |
30 | | - * connectionMap |
31 | | - * |
32 | | - * @return string[] |
33 | | - */ |
34 | | - public static function connectionMap() |
35 | | - { |
36 | | - return [ |
37 | | - 'local' => 'localConnection', |
38 | | - 'dev' => 'devConnection' |
39 | | - ]; |
40 | | - } |
41 | | - |
42 | | - /** |
43 | | - * Mysql constructor. |
44 | | - * |
45 | | - * @param array $config |
46 | | - * @throws Exception |
47 | | - */ |
48 | | - public function __construct(array $config = []) |
49 | | - { |
50 | | - $config = empty($config) ? $this->getConfig() : $config; |
51 | | - foreach (static::connectionMap() as $key => $connect) { |
52 | | - $this->{$key.'Db'} = $config[$key]; |
53 | | - $dsn = $this->getDsn($config[$key]['host'], $config[$key]['dbname']); |
54 | | - $user = $config[$key]['username']; |
55 | | - $password = $config[$key]['passwd']; |
56 | | - $this->{$connect} = new PDO($dsn, $user, $password, |
57 | | - [PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"]); |
58 | | - } |
59 | | - } |
60 | | - |
61 | | - /** |
62 | | - * getConfig |
63 | | - * |
64 | | - * @return false|string |
65 | | - * @throws Exception |
66 | | - */ |
67 | | - public function getConfig() |
68 | | - { |
69 | | - $file = dirname(__DIR__).'/config.php'; |
70 | | - if (is_file($file)) { |
71 | | - $config = file_get_contents($file); |
72 | | - } |
73 | | - if (empty($config)) { |
74 | | - throw new Exception('db config exception'); |
75 | | - } |
76 | | - return $config; |
77 | | - } |
78 | | - |
79 | | - /** |
80 | | - * getDsn |
81 | | - * |
82 | | - * @param string $host |
83 | | - * @param string $db_name |
84 | | - * @return string |
85 | | - */ |
86 | | - public function getDsn($host = '', $db_name = '') |
87 | | - { |
88 | | - return "mysql:host={$host};dbname={$db_name}"; |
89 | | - } |
90 | | - |
91 | | - /** |
92 | | - * getDevConnection |
93 | | - * |
94 | | - * @return PDO |
95 | | - */ |
96 | | - public function getLocalConnection() |
97 | | - { |
98 | | - return $this->localConnection; |
99 | | - } |
100 | | - |
101 | | - /** |
102 | | - * getDevConnection |
103 | | - * |
104 | | - * @return PDO |
105 | | - */ |
106 | | - public function getDevConnection() |
107 | | - { |
108 | | - return $this->devConnection; |
109 | | - } |
110 | | -} |
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Zyimm\dbStructSync\connector; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use PDO; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class Mysql |
| 10 | + * |
| 11 | + * @package Zyimm\dbStructSync\connector |
| 12 | + */ |
| 13 | +class Mysql |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var PDO |
| 17 | + */ |
| 18 | + private $localConnection; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var PDO |
| 22 | + */ |
| 23 | + private $devConnection; |
| 24 | + |
| 25 | + public $localDb = []; |
| 26 | + |
| 27 | + public $devDb = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * connectionMap |
| 31 | + * |
| 32 | + * @return string[] |
| 33 | + */ |
| 34 | + public static function connectionMap() |
| 35 | + { |
| 36 | + return [ |
| 37 | + 'local' => 'localConnection', |
| 38 | + 'dev' => 'devConnection' |
| 39 | + ]; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Mysql constructor. |
| 44 | + * |
| 45 | + * @param array $config |
| 46 | + * @throws Exception |
| 47 | + */ |
| 48 | + public function __construct(array $config = []) |
| 49 | + { |
| 50 | + $config = empty($config) ? $this->getConfig() : $config; |
| 51 | + foreach (static::connectionMap() as $key => $connect) { |
| 52 | + $this->{$key.'Db'} = $config[$key]; |
| 53 | + $dsn = $this->getDsn($config[$key]['host'], $config[$key]['dbname']); |
| 54 | + $user = $config[$key]['username']; |
| 55 | + $password = $config[$key]['passwd']; |
| 56 | + $this->{$connect} = new PDO($dsn, $user, $password, |
| 57 | + [PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"]); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * getConfig |
| 63 | + * |
| 64 | + * @return string |
| 65 | + * @throws Exception |
| 66 | + */ |
| 67 | + public function getConfig() |
| 68 | + { |
| 69 | + $file = dirname(__DIR__).'/config.php'; |
| 70 | + if (is_file($file)) { |
| 71 | + $config = file_get_contents($file); |
| 72 | + } |
| 73 | + if (empty($config)) { |
| 74 | + throw new Exception('db config exception'); |
| 75 | + } |
| 76 | + return $config; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * getDsn |
| 81 | + * |
| 82 | + * @param string $host |
| 83 | + * @param string $db_name |
| 84 | + * |
| 85 | + * @return string |
| 86 | + */ |
| 87 | + public function getDsn($host = '', $db_name = '') |
| 88 | + { |
| 89 | + return "mysql:host=$host;dbname=$db_name"; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * getDevConnection |
| 94 | + * |
| 95 | + * @return PDO |
| 96 | + */ |
| 97 | + public function getLocalConnection() |
| 98 | + { |
| 99 | + return $this->localConnection; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * getDevConnection |
| 104 | + * |
| 105 | + * @return PDO |
| 106 | + */ |
| 107 | + public function getDevConnection() |
| 108 | + { |
| 109 | + return $this->devConnection; |
| 110 | + } |
| 111 | +} |
0 commit comments