1 | <?php |
||||
2 | |||||
3 | namespace Nip\Database\Manager; |
||||
4 | |||||
5 | use InvalidArgumentException; |
||||
6 | use Nip\Container\Container; |
||||
7 | use Nip\Database\Connections\Connection; |
||||
8 | use Nip\Utility\Arr; |
||||
9 | |||||
10 | /** |
||||
11 | * Trait HasConnections |
||||
12 | * @package Nip\Database\Manager |
||||
13 | */ |
||||
14 | trait HasConnections |
||||
15 | { |
||||
16 | protected $connections = []; |
||||
17 | |||||
18 | /** |
||||
19 | * Get a database connection instance. |
||||
20 | * |
||||
21 | * @param string $name |
||||
22 | * @return Connection |
||||
23 | */ |
||||
24 | public function connection($name = null) |
||||
25 | { |
||||
26 | $connectionName = $this->parseConnectionName($name); |
||||
27 | if (is_array($connectionName)) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
28 | list($database, $type) = $connectionName; |
||||
29 | } else { |
||||
30 | $database = $connectionName; |
||||
31 | $type = null; |
||||
32 | } |
||||
33 | $name = $name ?: $database; |
||||
34 | |||||
35 | // If we haven't created this connection, we'll create it based on the config |
||||
36 | // provided in the application. Once we've created the connections we will |
||||
37 | // set the "fetch mode" for PDO which determines the query return types. |
||||
38 | if (!isset($this->connections[$name])) { |
||||
39 | $connection = $this->configure($this->makeConnection($name), $type); |
||||
40 | $this->setConnection($connection, $name); |
||||
41 | } |
||||
42 | |||||
43 | return $this->connections[$name]; |
||||
44 | } |
||||
45 | |||||
46 | /** |
||||
47 | * @param Connection $connection |
||||
48 | * @param string $name |
||||
49 | */ |
||||
50 | public function setConnection($connection, $name) |
||||
51 | { |
||||
52 | $this->connections[$name] = $connection; |
||||
53 | } |
||||
54 | |||||
55 | /** |
||||
56 | * @return array |
||||
57 | */ |
||||
58 | public function getConnections(): array |
||||
59 | { |
||||
60 | return $this->connections; |
||||
61 | } |
||||
62 | |||||
63 | /** |
||||
64 | * Get the default connection name. |
||||
65 | * |
||||
66 | * @return string |
||||
67 | */ |
||||
68 | public function getDefaultConnection() |
||||
69 | { |
||||
70 | if (!function_exists('config')) { |
||||
71 | return 'main'; |
||||
72 | } |
||||
73 | |||||
74 | if (!Container::getInstance() || !Container::getInstance()->has('config')) { |
||||
75 | return 'main'; |
||||
76 | } |
||||
77 | |||||
78 | if (Container::getInstance() && config()->has('database.default')) { |
||||
79 | return config()->get('database.default'); |
||||
80 | } |
||||
81 | |||||
82 | return 'main'; |
||||
83 | } |
||||
84 | |||||
85 | /** |
||||
86 | * Parse the connection into an array of the name and read / write type. |
||||
87 | * |
||||
88 | * @param string $name |
||||
89 | * @return string |
||||
90 | */ |
||||
91 | protected function parseConnectionName($name) |
||||
92 | { |
||||
93 | $name = $name ?: $this->getDefaultConnection(); |
||||
94 | |||||
95 | return $name; |
||||
96 | } |
||||
97 | |||||
98 | /** |
||||
99 | * Prepare the database connection instance. |
||||
100 | * |
||||
101 | * @param Connection $connection |
||||
102 | * @param string $type |
||||
103 | * @return Connection |
||||
104 | */ |
||||
105 | protected function configure(Connection $connection, $type) |
||||
0 ignored issues
–
show
The parameter
$type is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
106 | { |
||||
107 | // $connection = $this->setPdoForType($connection, $type); |
||||
108 | // First we'll set the fetch mode and a few other dependencies of the database |
||||
109 | // connection. This method basically just configures and prepares it to get |
||||
110 | // used by the application. Once we're finished we'll return it back out. |
||||
111 | // if ($this->app->bound('events')) { |
||||
112 | // $connection->setEventDispatcher($this->app['events']); |
||||
113 | // } |
||||
114 | // Here we'll set a reconnector callback. This reconnector can be any callable |
||||
115 | // so we will set a Closure to reconnect from this manager with the name of |
||||
116 | // the connection, which will allow us to reconnect from the connections. |
||||
117 | // $connection->setReconnector(function ($connection) { |
||||
118 | // $this->reconnect($connection->getName()); |
||||
119 | // }); |
||||
120 | return $connection; |
||||
121 | } |
||||
122 | |||||
123 | /** |
||||
124 | * Make the database connection instance. |
||||
125 | * |
||||
126 | * @param string $name |
||||
127 | * @return Connection |
||||
128 | */ |
||||
129 | protected function makeConnection($name) |
||||
130 | { |
||||
131 | $config = $this->configuration($name); |
||||
132 | |||||
133 | // First we will check by the connection name to see if an extension has been |
||||
134 | // registered specifically for that connection. If it has we will call the |
||||
135 | // Closure and pass it the config allowing it to resolve the connection. |
||||
136 | // if (isset($this->extensions[$name])) { |
||||
137 | // return call_user_func($this->extensions[$name], $config, $name); |
||||
138 | // } |
||||
139 | |||||
140 | // Next we will check to see if an extension has been registered for a driver |
||||
141 | // and will call the Closure if so, which allows us to have a more generic |
||||
142 | // resolver for the drivers themselves which applies to all connections. |
||||
143 | // if (isset($this->extensions[$driver = $config['driver']])) { |
||||
144 | // return call_user_func($this->extensions[$driver], $config, $name); |
||||
145 | // } |
||||
146 | return $this->factory->make($config, $name); |
||||
147 | } |
||||
148 | |||||
149 | /** |
||||
150 | * Get the configuration for a connection. |
||||
151 | * |
||||
152 | * @param string $name |
||||
153 | * @return array |
||||
154 | * |
||||
155 | * @throws \InvalidArgumentException |
||||
156 | */ |
||||
157 | protected function configuration($name) |
||||
158 | { |
||||
159 | $name = $name ?: $this->getDefaultConnection(); |
||||
160 | |||||
161 | // To get the database connection configuration, we will just pull each of the |
||||
162 | // connection configurations and get the configurations for the given name. |
||||
163 | // If the configuration doesn't exist, we'll throw an exception and bail. |
||||
164 | |||||
165 | $connections = config('database.connections'); |
||||
166 | if (is_null($config = Arr::get($connections, $name))) { |
||||
167 | throw new InvalidArgumentException("Database [$name] not configured."); |
||||
168 | } |
||||
169 | |||||
170 | if ($config['user']) { |
||||
171 | $config['username'] = $config['user']; |
||||
172 | } |
||||
173 | |||||
174 | if ($config['name']) { |
||||
175 | $config['database'] = $config['name']; |
||||
176 | } |
||||
177 | |||||
178 | return $config; |
||||
0 ignored issues
–
show
|
|||||
179 | } |
||||
180 | } |
||||
181 |