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 |