PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.4
Independent Analytics – WordPress Analytics Plugin v2.15.4
2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / illuminate / database / Connectors / ConnectionFactory.php
independent-analytics / vendor / illuminate / database / Connectors Last commit date
ConnectionFactory.php 3 weeks ago Connector.php 2 years ago ConnectorInterface.php 2 years ago MySqlConnector.php 2 years ago PostgresConnector.php 3 weeks ago SQLiteConnector.php 3 weeks ago SqlServerConnector.php 3 weeks ago
ConnectionFactory.php
241 lines
1 <?php
2
3 namespace IAWPSCOPED\Illuminate\Database\Connectors;
4
5 use IAWPSCOPED\Illuminate\Contracts\Container\Container;
6 use IAWPSCOPED\Illuminate\Database\Connection;
7 use IAWPSCOPED\Illuminate\Database\MySqlConnection;
8 use IAWPSCOPED\Illuminate\Database\PostgresConnection;
9 use IAWPSCOPED\Illuminate\Database\SQLiteConnection;
10 use IAWPSCOPED\Illuminate\Database\SqlServerConnection;
11 use IAWPSCOPED\Illuminate\Support\Arr;
12 use InvalidArgumentException;
13 use PDOException;
14 /** @internal */
15 class ConnectionFactory
16 {
17 /**
18 * The IoC container instance.
19 *
20 * @var \Illuminate\Contracts\Container\Container
21 */
22 protected $container;
23 /**
24 * Create a new connection factory instance.
25 *
26 * @param \Illuminate\Contracts\Container\Container $container
27 * @return void
28 */
29 public function __construct(Container $container)
30 {
31 $this->container = $container;
32 }
33 /**
34 * Establish a PDO connection based on the configuration.
35 *
36 * @param array $config
37 * @param string|null $name
38 * @return \Illuminate\Database\Connection
39 */
40 public function make(array $config, $name = null)
41 {
42 $config = $this->parseConfig($config, $name);
43 if (isset($config['read'])) {
44 return $this->createReadWriteConnection($config);
45 }
46 return $this->createSingleConnection($config);
47 }
48 /**
49 * Parse and prepare the database configuration.
50 *
51 * @param array $config
52 * @param string $name
53 * @return array
54 */
55 protected function parseConfig(array $config, $name)
56 {
57 return Arr::add(Arr::add($config, 'prefix', ''), 'name', $name);
58 }
59 /**
60 * Create a single database connection instance.
61 *
62 * @param array $config
63 * @return \Illuminate\Database\Connection
64 */
65 protected function createSingleConnection(array $config)
66 {
67 $pdo = $this->createPdoResolver($config);
68 return $this->createConnection($config['driver'], $pdo, $config['database'], $config['prefix'], $config);
69 }
70 /**
71 * Create a read / write database connection instance.
72 *
73 * @param array $config
74 * @return \Illuminate\Database\Connection
75 */
76 protected function createReadWriteConnection(array $config)
77 {
78 $connection = $this->createSingleConnection($this->getWriteConfig($config));
79 return $connection->setReadPdo($this->createReadPdo($config));
80 }
81 /**
82 * Create a new PDO instance for reading.
83 *
84 * @param array $config
85 * @return \Closure
86 */
87 protected function createReadPdo(array $config)
88 {
89 return $this->createPdoResolver($this->getReadConfig($config));
90 }
91 /**
92 * Get the read configuration for a read / write connection.
93 *
94 * @param array $config
95 * @return array
96 */
97 protected function getReadConfig(array $config)
98 {
99 return $this->mergeReadWriteConfig($config, $this->getReadWriteConfig($config, 'read'));
100 }
101 /**
102 * Get the write configuration for a read / write connection.
103 *
104 * @param array $config
105 * @return array
106 */
107 protected function getWriteConfig(array $config)
108 {
109 return $this->mergeReadWriteConfig($config, $this->getReadWriteConfig($config, 'write'));
110 }
111 /**
112 * Get a read / write level configuration.
113 *
114 * @param array $config
115 * @param string $type
116 * @return array
117 */
118 protected function getReadWriteConfig(array $config, $type)
119 {
120 return isset($config[$type][0]) ? Arr::random($config[$type]) : $config[$type];
121 }
122 /**
123 * Merge a configuration for a read / write connection.
124 *
125 * @param array $config
126 * @param array $merge
127 * @return array
128 */
129 protected function mergeReadWriteConfig(array $config, array $merge)
130 {
131 return Arr::except(\array_merge($config, $merge), ['read', 'write']);
132 }
133 /**
134 * Create a new Closure that resolves to a PDO instance.
135 *
136 * @param array $config
137 * @return \Closure
138 */
139 protected function createPdoResolver(array $config)
140 {
141 return \array_key_exists('host', $config) ? $this->createPdoResolverWithHosts($config) : $this->createPdoResolverWithoutHosts($config);
142 }
143 /**
144 * Create a new Closure that resolves to a PDO instance with a specific host or an array of hosts.
145 *
146 * @param array $config
147 * @return \Closure
148 *
149 * @throws \PDOException
150 */
151 protected function createPdoResolverWithHosts(array $config)
152 {
153 return function () use($config) {
154 foreach (Arr::shuffle($this->parseHosts($config)) as $host) {
155 $config['host'] = $host;
156 try {
157 return $this->createConnector($config)->connect($config);
158 } catch (PDOException $e) {
159 continue;
160 }
161 }
162 throw $e;
163 };
164 }
165 /**
166 * Parse the hosts configuration item into an array.
167 *
168 * @param array $config
169 * @return array
170 *
171 * @throws \InvalidArgumentException
172 */
173 protected function parseHosts(array $config)
174 {
175 $hosts = Arr::wrap($config['host']);
176 if (empty($hosts)) {
177 throw new InvalidArgumentException('Database hosts array is empty.');
178 }
179 return $hosts;
180 }
181 /**
182 * Create a new Closure that resolves to a PDO instance where there is no configured host.
183 *
184 * @param array $config
185 * @return \Closure
186 */
187 protected function createPdoResolverWithoutHosts(array $config)
188 {
189 return fn() => $this->createConnector($config)->connect($config);
190 }
191 /**
192 * Create a connector instance based on the configuration.
193 *
194 * @param array $config
195 * @return \Illuminate\Database\Connectors\ConnectorInterface
196 *
197 * @throws \InvalidArgumentException
198 */
199 public function createConnector(array $config)
200 {
201 if (!isset($config['driver'])) {
202 throw new InvalidArgumentException('A driver must be specified.');
203 }
204 if ($this->container->bound($key = "db.connector.{$config['driver']}")) {
205 return $this->container->make($key);
206 }
207 return match ($config['driver']) {
208 'mysql' => new MySqlConnector(),
209 'pgsql' => new PostgresConnector(),
210 'sqlite' => new SQLiteConnector(),
211 'sqlsrv' => new SqlServerConnector(),
212 default => throw new InvalidArgumentException("Unsupported driver [{$config['driver']}]."),
213 };
214 }
215 /**
216 * Create a new connection instance.
217 *
218 * @param string $driver
219 * @param \PDO|\Closure $connection
220 * @param string $database
221 * @param string $prefix
222 * @param array $config
223 * @return \Illuminate\Database\Connection
224 *
225 * @throws \InvalidArgumentException
226 */
227 protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
228 {
229 if ($resolver = Connection::getResolver($driver)) {
230 return $resolver($connection, $database, $prefix, $config);
231 }
232 return match ($driver) {
233 'mysql' => new MySqlConnection($connection, $database, $prefix, $config),
234 'pgsql' => new PostgresConnection($connection, $database, $prefix, $config),
235 'sqlite' => new SQLiteConnection($connection, $database, $prefix, $config),
236 'sqlsrv' => new SqlServerConnection($connection, $database, $prefix, $config),
237 default => throw new InvalidArgumentException("Unsupported driver [{$driver}]."),
238 };
239 }
240 }
241