use PDO; use Illuminate\Container\Container; use Illuminate\Database\MySqlConnection; use Illuminate\Database\SQLiteConnection; use Illuminate\Database\PostgresConnection; use Illuminate\Database\SqlServerConnection;
class ConnectionFactory {
/** * The IoC container instance. * * @var \Illuminate\Container\Container */ protected $container;
/** * Create a new connection factory instance. * * @param \Illuminate\Container\Container $container * @return void */ public function __construct(Container $container) { $this->container = $container; }
/** * Establish a PDO connection based on the configuration. * * @param array $config * @param string $name * @return \Illuminate\Database\Connection */ public function make(array $config, $name = null) { $config = $this->parseConfig($config, $name);
if (isset($config['read'])) { return $this->createReadWriteConnection($config); }
return $this->createSingleConnection($config); }
/** * Create a single database connection instance. * * @param array $config * @return \Illuminate\Database\Connection */ protected function createSingleConnection(array $config) { $pdo = $this->createConnector($config)->connect($config);
/** * Register the primary database bindings. * * @return void */ protected function registerConnectionServices() { // The connection factory is used to create the actual connection instances on // the database. We will inject the factory into the manager so that it may // make the connections while they are actually needed and not of before. $this->app->singleton('db.factory', function ($app) { return new ConnectionFactory($app); });
// The database manager is used to resolve various connections, since multiple // connections might be managed. It also implements the connection resolver // interface which may be used by other components requiring connections. $this->app->singleton('db', function ($app) { return new DatabaseManager($app, $app['db.factory']); });
$this->app->bind('db.connection', function ($app) { return $app['db']->connection(); }); }
/** * Get the root object behind the facade. * * @return mixed */ public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); }
/** * Handle dynamic, static calls to the object. * * @param string $method * @param array$args * @return mixed * * @throws \RuntimeException */ public static function __callStatic($method, $args) { $instance = static::getFacadeRoot();
if (! $instance) { throw new RuntimeException('A facade root has not been set.'); }