You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DatabaseManager.php 9.3KB

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