ConnectionFactory.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace Illuminate\Database\Connectors;
  3. use PDOException;
  4. use Illuminate\Support\Arr;
  5. use InvalidArgumentException;
  6. use Illuminate\Database\Connection;
  7. use Illuminate\Database\MySqlConnection;
  8. use Illuminate\Database\SQLiteConnection;
  9. use Illuminate\Database\PostgresConnection;
  10. use Illuminate\Database\SqlServerConnection;
  11. use Illuminate\Contracts\Container\Container;
  12. use Illuminate\Contracts\Debug\ExceptionHandler;
  13. class ConnectionFactory
  14. {
  15. /**
  16. * The IoC container instance.
  17. *
  18. * @var \Illuminate\Contracts\Container\Container
  19. */
  20. protected $container;
  21. /**
  22. * Create a new connection factory instance.
  23. *
  24. * @param \Illuminate\Contracts\Container\Container $container
  25. * @return void
  26. */
  27. public function __construct(Container $container)
  28. {
  29. $this->container = $container;
  30. }
  31. /**
  32. * Establish a PDO connection based on the configuration.
  33. *
  34. * @param array $config
  35. * @param string $name
  36. * @return \Illuminate\Database\Connection
  37. */
  38. public function make(array $config, $name = null)
  39. {
  40. $config = $this->parseConfig($config, $name);
  41. if (isset($config['read'])) {
  42. return $this->createReadWriteConnection($config);
  43. }
  44. return $this->createSingleConnection($config);
  45. }
  46. /**
  47. * Parse and prepare the database configuration.
  48. *
  49. * @param array $config
  50. * @param string $name
  51. * @return array
  52. */
  53. protected function parseConfig(array $config, $name)
  54. {
  55. return Arr::add(Arr::add($config, 'prefix', ''), 'name', $name);
  56. }
  57. /**
  58. * Create a single database connection instance.
  59. *
  60. * @param array $config
  61. * @return \Illuminate\Database\Connection
  62. */
  63. protected function createSingleConnection(array $config)
  64. {
  65. $pdo = $this->createPdoResolver($config);
  66. return $this->createConnection(
  67. $config['driver'], $pdo, $config['database'], $config['prefix'], $config
  68. );
  69. }
  70. /**
  71. * Create a single database connection instance.
  72. *
  73. * @param array $config
  74. * @return \Illuminate\Database\Connection
  75. */
  76. protected function createReadWriteConnection(array $config)
  77. {
  78. $connection = $this->createSingleConnection($this->getWriteConfig($config));
  79. return $connection->setReadPdo($this->createReadPdo($config));
  80. }
  81. /**
  82. * Create a new PDO instance for reading.
  83. *
  84. * @param array $config
  85. * @return \Closure
  86. */
  87. protected function createReadPdo(array $config)
  88. {
  89. return $this->createPdoResolver($this->getReadConfig($config));
  90. }
  91. /**
  92. * Get the read configuration for a read / write connection.
  93. *
  94. * @param array $config
  95. * @return array
  96. */
  97. protected function getReadConfig(array $config)
  98. {
  99. return $this->mergeReadWriteConfig(
  100. $config, $this->getReadWriteConfig($config, 'read')
  101. );
  102. }
  103. /**
  104. * Get the read configuration for a read / write connection.
  105. *
  106. * @param array $config
  107. * @return array
  108. */
  109. protected function getWriteConfig(array $config)
  110. {
  111. return $this->mergeReadWriteConfig(
  112. $config, $this->getReadWriteConfig($config, 'write')
  113. );
  114. }
  115. /**
  116. * Get a read / write level configuration.
  117. *
  118. * @param array $config
  119. * @param string $type
  120. * @return array
  121. */
  122. protected function getReadWriteConfig(array $config, $type)
  123. {
  124. return isset($config[$type][0])
  125. ? Arr::random($config[$type])
  126. : $config[$type];
  127. }
  128. /**
  129. * Merge a configuration for a read / write connection.
  130. *
  131. * @param array $config
  132. * @param array $merge
  133. * @return array
  134. */
  135. protected function mergeReadWriteConfig(array $config, array $merge)
  136. {
  137. return Arr::except(array_merge($config, $merge), ['read', 'write']);
  138. }
  139. /**
  140. * Create a new Closure that resolves to a PDO instance.
  141. *
  142. * @param array $config
  143. * @return \Closure
  144. */
  145. protected function createPdoResolver(array $config)
  146. {
  147. return array_key_exists('host', $config)
  148. ? $this->createPdoResolverWithHosts($config)
  149. : $this->createPdoResolverWithoutHosts($config);
  150. }
  151. /**
  152. * Create a new Closure that resolves to a PDO instance with a specific host or an array of hosts.
  153. *
  154. * @param array $config
  155. * @return \Closure
  156. */
  157. protected function createPdoResolverWithHosts(array $config)
  158. {
  159. return function () use ($config) {
  160. foreach (Arr::shuffle($hosts = $this->parseHosts($config)) as $key => $host) {
  161. $config['host'] = $host;
  162. try {
  163. return $this->createConnector($config)->connect($config);
  164. } catch (PDOException $e) {
  165. if (count($hosts) - 1 === $key && $this->container->bound(ExceptionHandler::class)) {
  166. $this->container->make(ExceptionHandler::class)->report($e);
  167. }
  168. }
  169. }
  170. throw $e;
  171. };
  172. }
  173. /**
  174. * Parse the hosts configuration item into an array.
  175. *
  176. * @param array $config
  177. * @return array
  178. */
  179. protected function parseHosts(array $config)
  180. {
  181. $hosts = Arr::wrap($config['host']);
  182. if (empty($hosts)) {
  183. throw new InvalidArgumentException('Database hosts array is empty.');
  184. }
  185. return $hosts;
  186. }
  187. /**
  188. * Create a new Closure that resolves to a PDO instance where there is no configured host.
  189. *
  190. * @param array $config
  191. * @return \Closure
  192. */
  193. protected function createPdoResolverWithoutHosts(array $config)
  194. {
  195. return function () use ($config) {
  196. return $this->createConnector($config)->connect($config);
  197. };
  198. }
  199. /**
  200. * Create a connector instance based on the configuration.
  201. *
  202. * @param array $config
  203. * @return \Illuminate\Database\Connectors\ConnectorInterface
  204. *
  205. * @throws \InvalidArgumentException
  206. */
  207. public function createConnector(array $config)
  208. {
  209. if (! isset($config['driver'])) {
  210. throw new InvalidArgumentException('A driver must be specified.');
  211. }
  212. if ($this->container->bound($key = "db.connector.{$config['driver']}")) {
  213. return $this->container->make($key);
  214. }
  215. switch ($config['driver']) {
  216. case 'mysql':
  217. return new MySqlConnector;
  218. case 'pgsql':
  219. return new PostgresConnector;
  220. case 'sqlite':
  221. return new SQLiteConnector;
  222. case 'sqlsrv':
  223. return new SqlServerConnector;
  224. }
  225. throw new InvalidArgumentException("Unsupported driver [{$config['driver']}]");
  226. }
  227. /**
  228. * Create a new connection instance.
  229. *
  230. * @param string $driver
  231. * @param \PDO|\Closure $connection
  232. * @param string $database
  233. * @param string $prefix
  234. * @param array $config
  235. * @return \Illuminate\Database\Connection
  236. *
  237. * @throws \InvalidArgumentException
  238. */
  239. protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
  240. {
  241. if ($resolver = Connection::getResolver($driver)) {
  242. return $resolver($connection, $database, $prefix, $config);
  243. }
  244. switch ($driver) {
  245. case 'mysql':
  246. return new MySqlConnection($connection, $database, $prefix, $config);
  247. case 'pgsql':
  248. return new PostgresConnection($connection, $database, $prefix, $config);
  249. case 'sqlite':
  250. return new SQLiteConnection($connection, $database, $prefix, $config);
  251. case 'sqlsrv':
  252. return new SqlServerConnection($connection, $database, $prefix, $config);
  253. }
  254. throw new InvalidArgumentException("Unsupported driver [{$driver}]");
  255. }
  256. }