123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
-
- namespace Illuminate\Database\Connectors;
-
- use PDOException;
- use Illuminate\Support\Arr;
- use InvalidArgumentException;
- use Illuminate\Database\Connection;
- use Illuminate\Database\MySqlConnection;
- use Illuminate\Database\SQLiteConnection;
- use Illuminate\Database\PostgresConnection;
- use Illuminate\Database\SqlServerConnection;
- use Illuminate\Contracts\Container\Container;
- use Illuminate\Contracts\Debug\ExceptionHandler;
-
- class ConnectionFactory
- {
- /**
- * The IoC container instance.
- *
- * @var \Illuminate\Contracts\Container\Container
- */
- protected $container;
-
- /**
- * Create a new connection factory instance.
- *
- * @param \Illuminate\Contracts\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);
- }
-
- /**
- * Parse and prepare the database configuration.
- *
- * @param array $config
- * @param string $name
- * @return array
- */
- protected function parseConfig(array $config, $name)
- {
- return Arr::add(Arr::add($config, 'prefix', ''), 'name', $name);
- }
-
- /**
- * Create a single database connection instance.
- *
- * @param array $config
- * @return \Illuminate\Database\Connection
- */
- protected function createSingleConnection(array $config)
- {
- $pdo = $this->createPdoResolver($config);
-
- return $this->createConnection(
- $config['driver'], $pdo, $config['database'], $config['prefix'], $config
- );
- }
-
- /**
- * Create a single database connection instance.
- *
- * @param array $config
- * @return \Illuminate\Database\Connection
- */
- protected function createReadWriteConnection(array $config)
- {
- $connection = $this->createSingleConnection($this->getWriteConfig($config));
-
- return $connection->setReadPdo($this->createReadPdo($config));
- }
-
- /**
- * Create a new PDO instance for reading.
- *
- * @param array $config
- * @return \Closure
- */
- protected function createReadPdo(array $config)
- {
- return $this->createPdoResolver($this->getReadConfig($config));
- }
-
- /**
- * Get the read configuration for a read / write connection.
- *
- * @param array $config
- * @return array
- */
- protected function getReadConfig(array $config)
- {
- return $this->mergeReadWriteConfig(
- $config, $this->getReadWriteConfig($config, 'read')
- );
- }
-
- /**
- * Get the read configuration for a read / write connection.
- *
- * @param array $config
- * @return array
- */
- protected function getWriteConfig(array $config)
- {
- return $this->mergeReadWriteConfig(
- $config, $this->getReadWriteConfig($config, 'write')
- );
- }
-
- /**
- * Get a read / write level configuration.
- *
- * @param array $config
- * @param string $type
- * @return array
- */
- protected function getReadWriteConfig(array $config, $type)
- {
- return isset($config[$type][0])
- ? Arr::random($config[$type])
- : $config[$type];
- }
-
- /**
- * Merge a configuration for a read / write connection.
- *
- * @param array $config
- * @param array $merge
- * @return array
- */
- protected function mergeReadWriteConfig(array $config, array $merge)
- {
- return Arr::except(array_merge($config, $merge), ['read', 'write']);
- }
-
- /**
- * Create a new Closure that resolves to a PDO instance.
- *
- * @param array $config
- * @return \Closure
- */
- protected function createPdoResolver(array $config)
- {
- return array_key_exists('host', $config)
- ? $this->createPdoResolverWithHosts($config)
- : $this->createPdoResolverWithoutHosts($config);
- }
-
- /**
- * Create a new Closure that resolves to a PDO instance with a specific host or an array of hosts.
- *
- * @param array $config
- * @return \Closure
- */
- protected function createPdoResolverWithHosts(array $config)
- {
- return function () use ($config) {
- foreach (Arr::shuffle($hosts = $this->parseHosts($config)) as $key => $host) {
- $config['host'] = $host;
-
- try {
- return $this->createConnector($config)->connect($config);
- } catch (PDOException $e) {
- if (count($hosts) - 1 === $key && $this->container->bound(ExceptionHandler::class)) {
- $this->container->make(ExceptionHandler::class)->report($e);
- }
- }
- }
-
- throw $e;
- };
- }
-
- /**
- * Parse the hosts configuration item into an array.
- *
- * @param array $config
- * @return array
- */
- protected function parseHosts(array $config)
- {
- $hosts = Arr::wrap($config['host']);
-
- if (empty($hosts)) {
- throw new InvalidArgumentException('Database hosts array is empty.');
- }
-
- return $hosts;
- }
-
- /**
- * Create a new Closure that resolves to a PDO instance where there is no configured host.
- *
- * @param array $config
- * @return \Closure
- */
- protected function createPdoResolverWithoutHosts(array $config)
- {
- return function () use ($config) {
- return $this->createConnector($config)->connect($config);
- };
- }
-
- /**
- * Create a connector instance based on the configuration.
- *
- * @param array $config
- * @return \Illuminate\Database\Connectors\ConnectorInterface
- *
- * @throws \InvalidArgumentException
- */
- public function createConnector(array $config)
- {
- if (! isset($config['driver'])) {
- throw new InvalidArgumentException('A driver must be specified.');
- }
-
- if ($this->container->bound($key = "db.connector.{$config['driver']}")) {
- return $this->container->make($key);
- }
-
- switch ($config['driver']) {
- case 'mysql':
- return new MySqlConnector;
- case 'pgsql':
- return new PostgresConnector;
- case 'sqlite':
- return new SQLiteConnector;
- case 'sqlsrv':
- return new SqlServerConnector;
- }
-
- throw new InvalidArgumentException("Unsupported driver [{$config['driver']}]");
- }
-
- /**
- * Create a new connection instance.
- *
- * @param string $driver
- * @param \PDO|\Closure $connection
- * @param string $database
- * @param string $prefix
- * @param array $config
- * @return \Illuminate\Database\Connection
- *
- * @throws \InvalidArgumentException
- */
- protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
- {
- if ($resolver = Connection::getResolver($driver)) {
- return $resolver($connection, $database, $prefix, $config);
- }
-
- switch ($driver) {
- case 'mysql':
- return new MySqlConnection($connection, $database, $prefix, $config);
- case 'pgsql':
- return new PostgresConnection($connection, $database, $prefix, $config);
- case 'sqlite':
- return new SQLiteConnection($connection, $database, $prefix, $config);
- case 'sqlsrv':
- return new SqlServerConnection($connection, $database, $prefix, $config);
- }
-
- throw new InvalidArgumentException("Unsupported driver [{$driver}]");
- }
- }
|