DatabaseManager.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace Illuminate\Database;
  3. use PDO;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Str;
  6. use InvalidArgumentException;
  7. use Illuminate\Database\Connectors\ConnectionFactory;
  8. /**
  9. * @mixin \Illuminate\Database\Connection
  10. */
  11. class DatabaseManager implements ConnectionResolverInterface
  12. {
  13. /**
  14. * The application instance.
  15. *
  16. * @var \Illuminate\Foundation\Application
  17. */
  18. protected $app;
  19. /**
  20. * The database connection factory instance.
  21. *
  22. * @var \Illuminate\Database\Connectors\ConnectionFactory
  23. */
  24. protected $factory;
  25. /**
  26. * The active connection instances.
  27. *
  28. * @var array
  29. */
  30. protected $connections = [];
  31. /**
  32. * The custom connection resolvers.
  33. *
  34. * @var array
  35. */
  36. protected $extensions = [];
  37. /**
  38. * Create a new database manager instance.
  39. *
  40. * @param \Illuminate\Foundation\Application $app
  41. * @param \Illuminate\Database\Connectors\ConnectionFactory $factory
  42. * @return void
  43. */
  44. public function __construct($app, ConnectionFactory $factory)
  45. {
  46. $this->app = $app;
  47. $this->factory = $factory;
  48. }
  49. /**
  50. * Get a database connection instance.
  51. *
  52. * @param string $name
  53. * @return \Illuminate\Database\Connection
  54. */
  55. public function connection($name = null)
  56. {
  57. list($database, $type) = $this->parseConnectionName($name);
  58. $name = $name ?: $database;
  59. // If we haven't created this connection, we'll create it based on the config
  60. // provided in the application. Once we've created the connections we will
  61. // set the "fetch mode" for PDO which determines the query return types.
  62. if (! isset($this->connections[$name])) {
  63. $this->connections[$name] = $this->configure(
  64. $this->makeConnection($database), $type
  65. );
  66. }
  67. return $this->connections[$name];
  68. }
  69. /**
  70. * Parse the connection into an array of the name and read / write type.
  71. *
  72. * @param string $name
  73. * @return array
  74. */
  75. protected function parseConnectionName($name)
  76. {
  77. $name = $name ?: $this->getDefaultConnection();
  78. return Str::endsWith($name, ['::read', '::write'])
  79. ? explode('::', $name, 2) : [$name, null];
  80. }
  81. /**
  82. * Make the database connection instance.
  83. *
  84. * @param string $name
  85. * @return \Illuminate\Database\Connection
  86. */
  87. protected function makeConnection($name)
  88. {
  89. $config = $this->configuration($name);
  90. // First we will check by the connection name to see if an extension has been
  91. // registered specifically for that connection. If it has we will call the
  92. // Closure and pass it the config allowing it to resolve the connection.
  93. if (isset($this->extensions[$name])) {
  94. return call_user_func($this->extensions[$name], $config, $name);
  95. }
  96. // Next we will check to see if an extension has been registered for a driver
  97. // and will call the Closure if so, which allows us to have a more generic
  98. // resolver for the drivers themselves which applies to all connections.
  99. if (isset($this->extensions[$driver = $config['driver']])) {
  100. return call_user_func($this->extensions[$driver], $config, $name);
  101. }
  102. return $this->factory->make($config, $name);
  103. }
  104. /**
  105. * Get the configuration for a connection.
  106. *
  107. * @param string $name
  108. * @return array
  109. *
  110. * @throws \InvalidArgumentException
  111. */
  112. protected function configuration($name)
  113. {
  114. $name = $name ?: $this->getDefaultConnection();
  115. // To get the database connection configuration, we will just pull each of the
  116. // connection configurations and get the configurations for the given name.
  117. // If the configuration doesn't exist, we'll throw an exception and bail.
  118. $connections = $this->app['config']['database.connections'];
  119. if (is_null($config = Arr::get($connections, $name))) {
  120. throw new InvalidArgumentException("Database [{$name}] not configured.");
  121. }
  122. return $config;
  123. }
  124. /**
  125. * Prepare the database connection instance.
  126. *
  127. * @param \Illuminate\Database\Connection $connection
  128. * @param string $type
  129. * @return \Illuminate\Database\Connection
  130. */
  131. protected function configure(Connection $connection, $type)
  132. {
  133. $connection = $this->setPdoForType($connection, $type);
  134. // First we'll set the fetch mode and a few other dependencies of the database
  135. // connection. This method basically just configures and prepares it to get
  136. // used by the application. Once we're finished we'll return it back out.
  137. if ($this->app->bound('events')) {
  138. $connection->setEventDispatcher($this->app['events']);
  139. }
  140. // Here we'll set a reconnector callback. This reconnector can be any callable
  141. // so we will set a Closure to reconnect from this manager with the name of
  142. // the connection, which will allow us to reconnect from the connections.
  143. $connection->setReconnector(function ($connection) {
  144. $this->reconnect($connection->getName());
  145. });
  146. return $connection;
  147. }
  148. /**
  149. * Prepare the read / write mode for database connection instance.
  150. *
  151. * @param \Illuminate\Database\Connection $connection
  152. * @param string $type
  153. * @return \Illuminate\Database\Connection
  154. */
  155. protected function setPdoForType(Connection $connection, $type = null)
  156. {
  157. if ($type == 'read') {
  158. $connection->setPdo($connection->getReadPdo());
  159. } elseif ($type == 'write') {
  160. $connection->setReadPdo($connection->getPdo());
  161. }
  162. return $connection;
  163. }
  164. /**
  165. * Disconnect from the given database and remove from local cache.
  166. *
  167. * @param string $name
  168. * @return void
  169. */
  170. public function purge($name = null)
  171. {
  172. $name = $name ?: $this->getDefaultConnection();
  173. $this->disconnect($name);
  174. unset($this->connections[$name]);
  175. }
  176. /**
  177. * Disconnect from the given database.
  178. *
  179. * @param string $name
  180. * @return void
  181. */
  182. public function disconnect($name = null)
  183. {
  184. if (isset($this->connections[$name = $name ?: $this->getDefaultConnection()])) {
  185. $this->connections[$name]->disconnect();
  186. }
  187. }
  188. /**
  189. * Reconnect to the given database.
  190. *
  191. * @param string $name
  192. * @return \Illuminate\Database\Connection
  193. */
  194. public function reconnect($name = null)
  195. {
  196. $this->disconnect($name = $name ?: $this->getDefaultConnection());
  197. if (! isset($this->connections[$name])) {
  198. return $this->connection($name);
  199. }
  200. return $this->refreshPdoConnections($name);
  201. }
  202. /**
  203. * Refresh the PDO connections on a given connection.
  204. *
  205. * @param string $name
  206. * @return \Illuminate\Database\Connection
  207. */
  208. protected function refreshPdoConnections($name)
  209. {
  210. $fresh = $this->makeConnection($name);
  211. return $this->connections[$name]
  212. ->setPdo($fresh->getPdo())
  213. ->setReadPdo($fresh->getReadPdo());
  214. }
  215. /**
  216. * Get the default connection name.
  217. *
  218. * @return string
  219. */
  220. public function getDefaultConnection()
  221. {
  222. return $this->app['config']['database.default'];
  223. }
  224. /**
  225. * Set the default connection name.
  226. *
  227. * @param string $name
  228. * @return void
  229. */
  230. public function setDefaultConnection($name)
  231. {
  232. $this->app['config']['database.default'] = $name;
  233. }
  234. /**
  235. * Get all of the support drivers.
  236. *
  237. * @return array
  238. */
  239. public function supportedDrivers()
  240. {
  241. return ['mysql', 'pgsql', 'sqlite', 'sqlsrv'];
  242. }
  243. /**
  244. * Get all of the drivers that are actually available.
  245. *
  246. * @return array
  247. */
  248. public function availableDrivers()
  249. {
  250. return array_intersect(
  251. $this->supportedDrivers(),
  252. str_replace('dblib', 'sqlsrv', PDO::getAvailableDrivers())
  253. );
  254. }
  255. /**
  256. * Register an extension connection resolver.
  257. *
  258. * @param string $name
  259. * @param callable $resolver
  260. * @return void
  261. */
  262. public function extend($name, callable $resolver)
  263. {
  264. $this->extensions[$name] = $resolver;
  265. }
  266. /**
  267. * Return all of the created connections.
  268. *
  269. * @return array
  270. */
  271. public function getConnections()
  272. {
  273. return $this->connections;
  274. }
  275. /**
  276. * Dynamically pass methods to the default connection.
  277. *
  278. * @param string $method
  279. * @param array $parameters
  280. * @return mixed
  281. */
  282. public function __call($method, $parameters)
  283. {
  284. return $this->connection()->$method(...$parameters);
  285. }
  286. }