Skip to content

Commit

Permalink
set a customer connection resolver using a static setter
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Jan 10, 2017
1 parent c9672a2 commit 2e46846
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
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

0 comments on commit 2e46846

Please sign in to comment.