Manager.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Illuminate\Database\Capsule;
  3. use PDO;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Database\DatabaseManager;
  6. use Illuminate\Contracts\Events\Dispatcher;
  7. use Illuminate\Support\Traits\CapsuleManagerTrait;
  8. use Illuminate\Database\Eloquent\Model as Eloquent;
  9. use Illuminate\Database\Connectors\ConnectionFactory;
  10. class Manager
  11. {
  12. use CapsuleManagerTrait;
  13. /**
  14. * The database manager instance.
  15. *
  16. * @var \Illuminate\Database\DatabaseManager
  17. */
  18. protected $manager;
  19. /**
  20. * Create a new database capsule manager.
  21. *
  22. * @param \Illuminate\Container\Container|null $container
  23. * @return void
  24. */
  25. public function __construct(Container $container = null)
  26. {
  27. $this->setupContainer($container ?: new Container);
  28. // Once we have the container setup, we will setup the default configuration
  29. // options in the container "config" binding. This will make the database
  30. // manager work correctly out of the box without extreme configuration.
  31. $this->setupDefaultConfiguration();
  32. $this->setupManager();
  33. }
  34. /**
  35. * Setup the default database configuration options.
  36. *
  37. * @return void
  38. */
  39. protected function setupDefaultConfiguration()
  40. {
  41. $this->container['config']['database.fetch'] = PDO::FETCH_OBJ;
  42. $this->container['config']['database.default'] = 'default';
  43. }
  44. /**
  45. * Build the database manager instance.
  46. *
  47. * @return void
  48. */
  49. protected function setupManager()
  50. {
  51. $factory = new ConnectionFactory($this->container);
  52. $this->manager = new DatabaseManager($this->container, $factory);
  53. }
  54. /**
  55. * Get a connection instance from the global manager.
  56. *
  57. * @param string|null $connection
  58. * @return \Illuminate\Database\Connection
  59. */
  60. public static function connection($connection = null)
  61. {
  62. return static::$instance->getConnection($connection);
  63. }
  64. /**
  65. * Get a fluent query builder instance.
  66. *
  67. * @param string $table
  68. * @param string|null $connection
  69. * @return \Illuminate\Database\Query\Builder
  70. */
  71. public static function table($table, $connection = null)
  72. {
  73. return static::$instance->connection($connection)->table($table);
  74. }
  75. /**
  76. * Get a schema builder instance.
  77. *
  78. * @param string|null $connection
  79. * @return \Illuminate\Database\Schema\Builder
  80. */
  81. public static function schema($connection = null)
  82. {
  83. return static::$instance->connection($connection)->getSchemaBuilder();
  84. }
  85. /**
  86. * Get a registered connection instance.
  87. *
  88. * @param string|null $name
  89. * @return \Illuminate\Database\Connection
  90. */
  91. public function getConnection($name = null)
  92. {
  93. return $this->manager->connection($name);
  94. }
  95. /**
  96. * Register a connection with the manager.
  97. *
  98. * @param array $config
  99. * @param string $name
  100. * @return void
  101. */
  102. public function addConnection(array $config, $name = 'default')
  103. {
  104. $connections = $this->container['config']['database.connections'];
  105. $connections[$name] = $config;
  106. $this->container['config']['database.connections'] = $connections;
  107. }
  108. /**
  109. * Bootstrap Eloquent so it is ready for usage.
  110. *
  111. * @return void
  112. */
  113. public function bootEloquent()
  114. {
  115. Eloquent::setConnectionResolver($this->manager);
  116. // If we have an event dispatcher instance, we will go ahead and register it
  117. // with the Eloquent ORM, allowing for model callbacks while creating and
  118. // updating "model" instances; however, it is not necessary to operate.
  119. if ($dispatcher = $this->getEventDispatcher()) {
  120. Eloquent::setEventDispatcher($dispatcher);
  121. }
  122. }
  123. /**
  124. * Set the fetch mode for the database connections.
  125. *
  126. * @param int $fetchMode
  127. * @return $this
  128. */
  129. public function setFetchMode($fetchMode)
  130. {
  131. $this->container['config']['database.fetch'] = $fetchMode;
  132. return $this;
  133. }
  134. /**
  135. * Get the database manager instance.
  136. *
  137. * @return \Illuminate\Database\DatabaseManager
  138. */
  139. public function getDatabaseManager()
  140. {
  141. return $this->manager;
  142. }
  143. /**
  144. * Get the current event dispatcher instance.
  145. *
  146. * @return \Illuminate\Contracts\Events\Dispatcher|null
  147. */
  148. public function getEventDispatcher()
  149. {
  150. if ($this->container->bound('events')) {
  151. return $this->container['events'];
  152. }
  153. }
  154. /**
  155. * Set the event dispatcher instance to be used by connections.
  156. *
  157. * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
  158. * @return void
  159. */
  160. public function setEventDispatcher(Dispatcher $dispatcher)
  161. {
  162. $this->container->instance('events', $dispatcher);
  163. }
  164. /**
  165. * Dynamically pass methods to the default connection.
  166. *
  167. * @param string $method
  168. * @param array $parameters
  169. * @return mixed
  170. */
  171. public static function __callStatic($method, $parameters)
  172. {
  173. return static::connection()->$method(...$parameters);
  174. }
  175. }