Capsule
2 years ago
Concerns
2 weeks ago
Connectors
2 weeks ago
Console
2 weeks ago
DBAL
2 weeks ago
Eloquent
2 weeks ago
Events
2 weeks ago
Migrations
2 weeks ago
PDO
2 weeks ago
Query
2 weeks ago
Schema
2 weeks ago
ClassMorphViolationException.php
2 years ago
ConfigurationUrlParser.php
2 years ago
Connection.php
2 weeks ago
ConnectionInterface.php
2 years ago
ConnectionResolver.php
2 weeks ago
ConnectionResolverInterface.php
2 years ago
DatabaseManager.php
2 weeks ago
DatabaseServiceProvider.php
2 weeks ago
DatabaseTransactionRecord.php
2 weeks ago
DatabaseTransactionsManager.php
2 weeks ago
DeadlockException.php
2 weeks ago
DetectsConcurrencyErrors.php
2 years ago
DetectsLostConnections.php
2 weeks ago
Grammar.php
2 weeks ago
LazyLoadingViolationException.php
2 years ago
LostConnectionException.php
2 weeks ago
MigrationServiceProvider.php
2 weeks ago
MultipleColumnsSelectedException.php
2 weeks ago
MultipleRecordsFoundException.php
2 weeks ago
MySqlConnection.php
2 weeks ago
PostgresConnection.php
2 weeks ago
QueryException.php
2 years ago
RecordsNotFoundException.php
2 years ago
SQLiteConnection.php
2 weeks ago
SQLiteDatabaseDoesNotExistException.php
2 weeks ago
Seeder.php
2 weeks ago
SqlServerConnection.php
2 weeks ago
DatabaseManager.php
396 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database; |
| 4 | |
| 5 | use IAWPSCOPED\Doctrine\DBAL\Types\Type; |
| 6 | use IAWPSCOPED\Illuminate\Database\Connectors\ConnectionFactory; |
| 7 | use IAWPSCOPED\Illuminate\Database\Events\ConnectionEstablished; |
| 8 | use IAWPSCOPED\Illuminate\Support\Arr; |
| 9 | use IAWPSCOPED\Illuminate\Support\ConfigurationUrlParser; |
| 10 | use IAWPSCOPED\Illuminate\Support\Str; |
| 11 | use IAWPSCOPED\Illuminate\Support\Traits\Macroable; |
| 12 | use InvalidArgumentException; |
| 13 | use PDO; |
| 14 | use RuntimeException; |
| 15 | /** |
| 16 | * @mixin \Illuminate\Database\Connection |
| 17 | * @internal |
| 18 | */ |
| 19 | class DatabaseManager implements ConnectionResolverInterface |
| 20 | { |
| 21 | use Macroable { |
| 22 | __call as macroCall; |
| 23 | } |
| 24 | /** |
| 25 | * The application instance. |
| 26 | * |
| 27 | * @var \Illuminate\Contracts\Foundation\Application |
| 28 | */ |
| 29 | protected $app; |
| 30 | /** |
| 31 | * The database connection factory instance. |
| 32 | * |
| 33 | * @var \Illuminate\Database\Connectors\ConnectionFactory |
| 34 | */ |
| 35 | protected $factory; |
| 36 | /** |
| 37 | * The active connection instances. |
| 38 | * |
| 39 | * @var array<string, \Illuminate\Database\Connection> |
| 40 | */ |
| 41 | protected $connections = []; |
| 42 | /** |
| 43 | * The custom connection resolvers. |
| 44 | * |
| 45 | * @var array<string, callable> |
| 46 | */ |
| 47 | protected $extensions = []; |
| 48 | /** |
| 49 | * The callback to be executed to reconnect to a database. |
| 50 | * |
| 51 | * @var callable |
| 52 | */ |
| 53 | protected $reconnector; |
| 54 | /** |
| 55 | * The custom Doctrine column types. |
| 56 | * |
| 57 | * @var array<string, array> |
| 58 | */ |
| 59 | protected $doctrineTypes = []; |
| 60 | /** |
| 61 | * Create a new database manager instance. |
| 62 | * |
| 63 | * @param \Illuminate\Contracts\Foundation\Application $app |
| 64 | * @param \Illuminate\Database\Connectors\ConnectionFactory $factory |
| 65 | * @return void |
| 66 | */ |
| 67 | public function __construct($app, ConnectionFactory $factory) |
| 68 | { |
| 69 | $this->app = $app; |
| 70 | $this->factory = $factory; |
| 71 | $this->reconnector = function ($connection) { |
| 72 | $this->reconnect($connection->getNameWithReadWriteType()); |
| 73 | }; |
| 74 | } |
| 75 | /** |
| 76 | * Get a database connection instance. |
| 77 | * |
| 78 | * @param string|null $name |
| 79 | * @return \Illuminate\Database\Connection |
| 80 | */ |
| 81 | public function connection($name = null) |
| 82 | { |
| 83 | [$database, $type] = $this->parseConnectionName($name); |
| 84 | $name = $name ?: $database; |
| 85 | // If we haven't created this connection, we'll create it based on the config |
| 86 | // provided in the application. Once we've created the connections we will |
| 87 | // set the "fetch mode" for PDO which determines the query return types. |
| 88 | if (!isset($this->connections[$name])) { |
| 89 | $this->connections[$name] = $this->configure($this->makeConnection($database), $type); |
| 90 | if ($this->app->bound('events')) { |
| 91 | $this->app['events']->dispatch(new ConnectionEstablished($this->connections[$name])); |
| 92 | } |
| 93 | } |
| 94 | return $this->connections[$name]; |
| 95 | } |
| 96 | /** |
| 97 | * Parse the connection into an array of the name and read / write type. |
| 98 | * |
| 99 | * @param string $name |
| 100 | * @return array |
| 101 | */ |
| 102 | protected function parseConnectionName($name) |
| 103 | { |
| 104 | $name = $name ?: $this->getDefaultConnection(); |
| 105 | return Str::endsWith($name, ['::read', '::write']) ? \explode('::', $name, 2) : [$name, null]; |
| 106 | } |
| 107 | /** |
| 108 | * Make the database connection instance. |
| 109 | * |
| 110 | * @param string $name |
| 111 | * @return \Illuminate\Database\Connection |
| 112 | */ |
| 113 | protected function makeConnection($name) |
| 114 | { |
| 115 | $config = $this->configuration($name); |
| 116 | // First we will check by the connection name to see if an extension has been |
| 117 | // registered specifically for that connection. If it has we will call the |
| 118 | // Closure and pass it the config allowing it to resolve the connection. |
| 119 | if (isset($this->extensions[$name])) { |
| 120 | return \call_user_func($this->extensions[$name], $config, $name); |
| 121 | } |
| 122 | // Next we will check to see if an extension has been registered for a driver |
| 123 | // and will call the Closure if so, which allows us to have a more generic |
| 124 | // resolver for the drivers themselves which applies to all connections. |
| 125 | if (isset($this->extensions[$driver = $config['driver']])) { |
| 126 | return \call_user_func($this->extensions[$driver], $config, $name); |
| 127 | } |
| 128 | return $this->factory->make($config, $name); |
| 129 | } |
| 130 | /** |
| 131 | * Get the configuration for a connection. |
| 132 | * |
| 133 | * @param string $name |
| 134 | * @return array |
| 135 | * |
| 136 | * @throws \InvalidArgumentException |
| 137 | */ |
| 138 | protected function configuration($name) |
| 139 | { |
| 140 | $name = $name ?: $this->getDefaultConnection(); |
| 141 | // To get the database connection configuration, we will just pull each of the |
| 142 | // connection configurations and get the configurations for the given name. |
| 143 | // If the configuration doesn't exist, we'll throw an exception and bail. |
| 144 | $connections = $this->app['config']['database.connections']; |
| 145 | if (\is_null($config = Arr::get($connections, $name))) { |
| 146 | throw new InvalidArgumentException("Database connection [{$name}] not configured."); |
| 147 | } |
| 148 | return (new ConfigurationUrlParser())->parseConfiguration($config); |
| 149 | } |
| 150 | /** |
| 151 | * Prepare the database connection instance. |
| 152 | * |
| 153 | * @param \Illuminate\Database\Connection $connection |
| 154 | * @param string $type |
| 155 | * @return \Illuminate\Database\Connection |
| 156 | */ |
| 157 | protected function configure(Connection $connection, $type) |
| 158 | { |
| 159 | $connection = $this->setPdoForType($connection, $type)->setReadWriteType($type); |
| 160 | // First we'll set the fetch mode and a few other dependencies of the database |
| 161 | // connection. This method basically just configures and prepares it to get |
| 162 | // used by the application. Once we're finished we'll return it back out. |
| 163 | if ($this->app->bound('events')) { |
| 164 | $connection->setEventDispatcher($this->app['events']); |
| 165 | } |
| 166 | if ($this->app->bound('db.transactions')) { |
| 167 | $connection->setTransactionManager($this->app['db.transactions']); |
| 168 | } |
| 169 | // Here we'll set a reconnector callback. This reconnector can be any callable |
| 170 | // so we will set a Closure to reconnect from this manager with the name of |
| 171 | // the connection, which will allow us to reconnect from the connections. |
| 172 | $connection->setReconnector($this->reconnector); |
| 173 | $this->registerConfiguredDoctrineTypes($connection); |
| 174 | return $connection; |
| 175 | } |
| 176 | /** |
| 177 | * Prepare the read / write mode for database connection instance. |
| 178 | * |
| 179 | * @param \Illuminate\Database\Connection $connection |
| 180 | * @param string|null $type |
| 181 | * @return \Illuminate\Database\Connection |
| 182 | */ |
| 183 | protected function setPdoForType(Connection $connection, $type = null) |
| 184 | { |
| 185 | if ($type === 'read') { |
| 186 | $connection->setPdo($connection->getReadPdo()); |
| 187 | } elseif ($type === 'write') { |
| 188 | $connection->setReadPdo($connection->getPdo()); |
| 189 | } |
| 190 | return $connection; |
| 191 | } |
| 192 | /** |
| 193 | * Register custom Doctrine types with the connection. |
| 194 | * |
| 195 | * @param \Illuminate\Database\Connection $connection |
| 196 | * @return void |
| 197 | */ |
| 198 | protected function registerConfiguredDoctrineTypes(Connection $connection) : void |
| 199 | { |
| 200 | foreach ($this->app['config']->get('database.dbal.types', []) as $name => $class) { |
| 201 | $this->registerDoctrineType($class, $name, $name); |
| 202 | } |
| 203 | foreach ($this->doctrineTypes as $name => [$type, $class]) { |
| 204 | $connection->registerDoctrineType($class, $name, $type); |
| 205 | } |
| 206 | } |
| 207 | /** |
| 208 | * Register a custom Doctrine type. |
| 209 | * |
| 210 | * @param string $class |
| 211 | * @param string $name |
| 212 | * @param string $type |
| 213 | * @return void |
| 214 | * |
| 215 | * @throws \Doctrine\DBAL\DBALException |
| 216 | * @throws \RuntimeException |
| 217 | */ |
| 218 | public function registerDoctrineType(string $class, string $name, string $type) : void |
| 219 | { |
| 220 | if (!\class_exists('IAWPSCOPED\\Doctrine\\DBAL\\Connection')) { |
| 221 | throw new RuntimeException('Registering a custom Doctrine type requires Doctrine DBAL (doctrine/dbal).'); |
| 222 | } |
| 223 | if (!Type::hasType($name)) { |
| 224 | Type::addType($name, $class); |
| 225 | } |
| 226 | $this->doctrineTypes[$name] = [$type, $class]; |
| 227 | } |
| 228 | /** |
| 229 | * Disconnect from the given database and remove from local cache. |
| 230 | * |
| 231 | * @param string|null $name |
| 232 | * @return void |
| 233 | */ |
| 234 | public function purge($name = null) |
| 235 | { |
| 236 | $name = $name ?: $this->getDefaultConnection(); |
| 237 | $this->disconnect($name); |
| 238 | unset($this->connections[$name]); |
| 239 | } |
| 240 | /** |
| 241 | * Disconnect from the given database. |
| 242 | * |
| 243 | * @param string|null $name |
| 244 | * @return void |
| 245 | */ |
| 246 | public function disconnect($name = null) |
| 247 | { |
| 248 | if (isset($this->connections[$name = $name ?: $this->getDefaultConnection()])) { |
| 249 | $this->connections[$name]->disconnect(); |
| 250 | } |
| 251 | } |
| 252 | /** |
| 253 | * Reconnect to the given database. |
| 254 | * |
| 255 | * @param string|null $name |
| 256 | * @return \Illuminate\Database\Connection |
| 257 | */ |
| 258 | public function reconnect($name = null) |
| 259 | { |
| 260 | $this->disconnect($name = $name ?: $this->getDefaultConnection()); |
| 261 | if (!isset($this->connections[$name])) { |
| 262 | return $this->connection($name); |
| 263 | } |
| 264 | return $this->refreshPdoConnections($name); |
| 265 | } |
| 266 | /** |
| 267 | * Set the default database connection for the callback execution. |
| 268 | * |
| 269 | * @param string $name |
| 270 | * @param callable $callback |
| 271 | * @return mixed |
| 272 | */ |
| 273 | public function usingConnection($name, callable $callback) |
| 274 | { |
| 275 | $previousName = $this->getDefaultConnection(); |
| 276 | $this->setDefaultConnection($name); |
| 277 | return \IAWPSCOPED\tap($callback(), function () use($previousName) { |
| 278 | $this->setDefaultConnection($previousName); |
| 279 | }); |
| 280 | } |
| 281 | /** |
| 282 | * Refresh the PDO connections on a given connection. |
| 283 | * |
| 284 | * @param string $name |
| 285 | * @return \Illuminate\Database\Connection |
| 286 | */ |
| 287 | protected function refreshPdoConnections($name) |
| 288 | { |
| 289 | [$database, $type] = $this->parseConnectionName($name); |
| 290 | $fresh = $this->configure($this->makeConnection($database), $type); |
| 291 | return $this->connections[$name]->setPdo($fresh->getRawPdo())->setReadPdo($fresh->getRawReadPdo()); |
| 292 | } |
| 293 | /** |
| 294 | * Get the default connection name. |
| 295 | * |
| 296 | * @return string |
| 297 | */ |
| 298 | public function getDefaultConnection() |
| 299 | { |
| 300 | return $this->app['config']['database.default']; |
| 301 | } |
| 302 | /** |
| 303 | * Set the default connection name. |
| 304 | * |
| 305 | * @param string $name |
| 306 | * @return void |
| 307 | */ |
| 308 | public function setDefaultConnection($name) |
| 309 | { |
| 310 | $this->app['config']['database.default'] = $name; |
| 311 | } |
| 312 | /** |
| 313 | * Get all of the support drivers. |
| 314 | * |
| 315 | * @return string[] |
| 316 | */ |
| 317 | public function supportedDrivers() |
| 318 | { |
| 319 | return ['mysql', 'pgsql', 'sqlite', 'sqlsrv']; |
| 320 | } |
| 321 | /** |
| 322 | * Get all of the drivers that are actually available. |
| 323 | * |
| 324 | * @return string[] |
| 325 | */ |
| 326 | public function availableDrivers() |
| 327 | { |
| 328 | return \array_intersect($this->supportedDrivers(), \str_replace('dblib', 'sqlsrv', PDO::getAvailableDrivers())); |
| 329 | } |
| 330 | /** |
| 331 | * Register an extension connection resolver. |
| 332 | * |
| 333 | * @param string $name |
| 334 | * @param callable $resolver |
| 335 | * @return void |
| 336 | */ |
| 337 | public function extend($name, callable $resolver) |
| 338 | { |
| 339 | $this->extensions[$name] = $resolver; |
| 340 | } |
| 341 | /** |
| 342 | * Remove an extension connection resolver. |
| 343 | * |
| 344 | * @param string $name |
| 345 | * @return void |
| 346 | */ |
| 347 | public function forgetExtension($name) |
| 348 | { |
| 349 | unset($this->extensions[$name]); |
| 350 | } |
| 351 | /** |
| 352 | * Return all of the created connections. |
| 353 | * |
| 354 | * @return array<string, \Illuminate\Database\Connection> |
| 355 | */ |
| 356 | public function getConnections() |
| 357 | { |
| 358 | return $this->connections; |
| 359 | } |
| 360 | /** |
| 361 | * Set the database reconnector callback. |
| 362 | * |
| 363 | * @param callable $reconnector |
| 364 | * @return void |
| 365 | */ |
| 366 | public function setReconnector(callable $reconnector) |
| 367 | { |
| 368 | $this->reconnector = $reconnector; |
| 369 | } |
| 370 | /** |
| 371 | * Set the application instance used by the manager. |
| 372 | * |
| 373 | * @param \Illuminate\Contracts\Foundation\Application $app |
| 374 | * @return $this |
| 375 | */ |
| 376 | public function setApplication($app) |
| 377 | { |
| 378 | $this->app = $app; |
| 379 | return $this; |
| 380 | } |
| 381 | /** |
| 382 | * Dynamically pass methods to the default connection. |
| 383 | * |
| 384 | * @param string $method |
| 385 | * @param array $parameters |
| 386 | * @return mixed |
| 387 | */ |
| 388 | public function __call($method, $parameters) |
| 389 | { |
| 390 | if (static::hasMacro($method)) { |
| 391 | return $this->macroCall($method, $parameters); |
| 392 | } |
| 393 | return $this->connection()->{$method}(...$parameters); |
| 394 | } |
| 395 | } |
| 396 |