Skip to content

Commit

Permalink
Defer resolving the PDO connection for both read and write connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickcarlohickman committed Aug 25, 2016
1 parent 0b6d521 commit 4c43262
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,10 @@ public function getReadPdo()
return $this->getPdo();
}

if ($this->readPdo instanceof Closure) {
return $this->readPdo = call_user_func($this->readPdo);
}

return $this->readPdo ?: $this->getPdo();
}

Expand Down
21 changes: 15 additions & 6 deletions src/Illuminate/Database/Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public function make(array $config, $name = null)
*/
protected function createSingleConnection(array $config)
{
$pdo = function () use ($config) {
return $this->createConnector($config)->connect($config);
};
$pdo = $this->createPdoResolver($config);

return $this->createConnection($config['driver'], $pdo, $config['database'], $config['prefix'], $config);
}
Expand All @@ -81,13 +79,24 @@ protected function createReadWriteConnection(array $config)
* Create a new PDO instance for reading.
*
* @param array $config
* @return \PDO
* @return \Closure
*/
protected function createReadPdo(array $config)
{
$readConfig = $this->getReadConfig($config);
return $this->createPdoResolver($this->getReadConfig($config));
}

return $this->createConnector($readConfig)->connect($readConfig);
/**
* Create a new Closure that resolves to a PDO instance.
*
* @param array $config
* @return \Closure
*/
protected function createPdoResolver(array $config)
{
return function () use ($config) {
return $this->createConnector($config)->connect($config);
};
}

/**
Expand Down

0 comments on commit 4c43262

Please sign in to comment.