DatabaseManager.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php namespace Illuminate\Database;
  2. use Illuminate\Support\Manager;
  3. use Illuminate\Database\Connectors\ConnectionFactory;
  4. class DatabaseManager implements ConnectionResolverInterface {
  5. /**
  6. * The application instance.
  7. *
  8. * @var \Illuminate\Foundation\Application
  9. */
  10. protected $app;
  11. /**
  12. * The database connection factory instance.
  13. *
  14. * @var \Illuminate\Database\Connectors\ConnectionFactory
  15. */
  16. protected $factory;
  17. /**
  18. * The active connection instances.
  19. *
  20. * @var array
  21. */
  22. protected $connections = array();
  23. /**
  24. * The custom connection resolvers.
  25. *
  26. * @var array
  27. */
  28. protected $extensions = array();
  29. /**
  30. * Create a new database manager instance.
  31. *
  32. * @param \Illuminate\Foundation\Application $app
  33. * @param \Illuminate\Database\Connectors\ConnectionFactory $factory
  34. * @return void
  35. */
  36. public function __construct($app, ConnectionFactory $factory)
  37. {
  38. $this->app = $app;
  39. $this->factory = $factory;
  40. }
  41. /**
  42. * Get a database connection instance.
  43. *
  44. * @param string $name
  45. * @return \Illuminate\Database\Connection
  46. */
  47. public function connection($name = null)
  48. {
  49. $name = $name ?: $this->getDefaultConnection();
  50. // If we haven't created this connection, we'll create it based on the config
  51. // provided in the application. Once we've created the connections we will
  52. // set the "fetch mode" for PDO which determines the query return types.
  53. if ( ! isset($this->connections[$name]))
  54. {
  55. $connection = $this->makeConnection($name);
  56. $this->connections[$name] = $this->prepare($connection);
  57. }
  58. return $this->connections[$name];
  59. }
  60. /**
  61. * Reconnect to the given database.
  62. *
  63. * @param string $name
  64. * @return \Illuminate\Database\Connection
  65. */
  66. public function reconnect($name = null)
  67. {
  68. $name = $name ?: $this->getDefaultConnection();
  69. unset($this->connections[$name]);
  70. return $this->connection($name);
  71. }
  72. /**
  73. * Make the database connection instance.
  74. *
  75. * @param string $name
  76. * @return \Illuminate\Database\Connection
  77. */
  78. protected function makeConnection($name)
  79. {
  80. $config = $this->getConfig($name);
  81. // First we will check by the connection name to see if an extension has been
  82. // registered specifically for that connection. If it has we will call the
  83. // Closure and pass it the config allowing it to resolve the connection.
  84. if (isset($this->extensions[$name]))
  85. {
  86. return call_user_func($this->extensions[$name], $config);
  87. }
  88. $driver = $config['driver'];
  89. // Next we will check to see if an extension has been registered for a driver
  90. // and will call the Closure if so, which allows us to have a more generic
  91. // resolver for the drivers themselves which applies to all connections.
  92. if (isset($this->extensions[$driver]))
  93. {
  94. return call_user_func($this->extensions[$driver], $config);
  95. }
  96. return $this->factory->make($config, $name);
  97. }
  98. /**
  99. * Prepare the database connection instance.
  100. *
  101. * @param \Illuminate\Database\Connection $connection
  102. * @return \Illuminate\Database\Connection
  103. */
  104. protected function prepare(Connection $connection)
  105. {
  106. $connection->setFetchMode($this->app['config']['database.fetch']);
  107. if ($this->app->bound('events'))
  108. {
  109. $connection->setEventDispatcher($this->app['events']);
  110. }
  111. // The database connection can also utilize a cache manager instance when cache
  112. // functionality is used on queries, which provides an expressive interface
  113. // to caching both fluent queries and Eloquent queries that are executed.
  114. $app = $this->app;
  115. $connection->setCacheManager(function() use ($app)
  116. {
  117. return $app['cache'];
  118. });
  119. // We will setup a Closure to resolve the paginator instance on the connection
  120. // since the Paginator isn't sued on every request and needs quite a few of
  121. // our dependencies. It'll be more efficient to lazily resolve instances.
  122. $connection->setPaginator(function() use ($app)
  123. {
  124. return $app['paginator'];
  125. });
  126. return $connection;
  127. }
  128. /**
  129. * Get the configuration for a connection.
  130. *
  131. * @param string $name
  132. * @return array
  133. */
  134. protected function getConfig($name)
  135. {
  136. $name = $name ?: $this->getDefaultConnection();
  137. // To get the database connection configuration, we will just pull each of the
  138. // connection configurations and get the configurations for the given name.
  139. // If the configuration doesn't exist, we'll throw an exception and bail.
  140. $connections = $this->app['config']['database.connections'];
  141. if (is_null($config = array_get($connections, $name)))
  142. {
  143. throw new \InvalidArgumentException("Database [$name] not configured.");
  144. }
  145. return $config;
  146. }
  147. /**
  148. * Get the default connection name.
  149. *
  150. * @return string
  151. */
  152. public function getDefaultConnection()
  153. {
  154. return $this->app['config']['database.default'];
  155. }
  156. /**
  157. * Set the default connection name.
  158. *
  159. * @param string $name
  160. * @return void
  161. */
  162. public function setDefaultConnection($name)
  163. {
  164. $this->app['config']['database.default'] = $name;
  165. }
  166. /**
  167. * Register an extension connection resolver.
  168. *
  169. * @param string $name
  170. * @param callable $resolver
  171. * @return void
  172. */
  173. public function extend($name, $resolver)
  174. {
  175. $this->extensions[$name] = $resolver;
  176. }
  177. /**
  178. * Dynamically pass methods to the default connection.
  179. *
  180. * @param string $method
  181. * @param array $parameters
  182. * @return mixed
  183. */
  184. public function __call($method, $parameters)
  185. {
  186. return call_user_func_array(array($this->connection(), $method), $parameters);
  187. }
  188. }