Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Set a custom connection resolver using static setters #17248

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ class Connection implements ConnectionInterface
*/
protected $doctrineConnection;

/**
* The connection resolvers.
*
* @var array
*/
protected static $resolvers = [];

/**
* Create a new database connection instance.
*
Expand Down Expand Up @@ -937,6 +944,30 @@ public function setReconnector(callable $reconnector)
return $this;
}

/**
* Register a connection resolver.
*
* @param string $driver
* @param \Closure $callback
* @return void
*/
public static function resolveConnection($driver, Closure $callback)
{
static::$resolvers[$driver] = $callback;
}

/**
* Get the connection resolver for the given driver.
*
* @param string $driver
* @return mixed
*/
public static function getResolver($driver)
{
return isset(static::$resolvers[$driver]) ?
static::$resolvers[$driver] : null;
}

/**
* Get the database connection name.
*
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PDOException;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Illuminate\Database\Connection;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Database\PostgresConnection;
Expand Down Expand Up @@ -267,8 +268,8 @@ public function createConnector(array $config)
*/
protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
{
if ($this->container->bound($key = "db.connection.{$driver}")) {
return $this->container->make($key, [$connection, $database, $prefix, $config]);
if ($resolver = Connection::getResolver($driver)) {
return $resolver($connection, $database, $prefix, $config);
}

switch ($driver) {
Expand Down