Manager.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Illuminate\Database\Capsule;
  3. use Illuminate\Container\Container;
  4. use Illuminate\Contracts\Events\Dispatcher;
  5. use Illuminate\Database\Connectors\ConnectionFactory;
  6. use Illuminate\Database\DatabaseManager;
  7. use Illuminate\Database\Eloquent\Model as Eloquent;
  8. use Illuminate\Support\Traits\CapsuleManagerTrait;
  9. use PDO;
  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 \Closure|\Illuminate\Database\Query\Builder|string $table
  68. * @param string|null $as
  69. * @param string|null $connection
  70. * @return \Illuminate\Database\Query\Builder
  71. */
  72. public static function table($table, $as = null, $connection = null)
  73. {
  74. return static::$instance->connection($connection)->table($table, $as);
  75. }
  76. /**
  77. * Get a schema builder instance.
  78. *
  79. * @param string|null $connection
  80. * @return \Illuminate\Database\Schema\Builder
  81. */
  82. public static function schema($connection = null)
  83. {
  84. return static::$instance->connection($connection)->getSchemaBuilder();
  85. }
  86. /**
  87. * Get a registered connection instance.
  88. *
  89. * @param string|null $name
  90. * @return \Illuminate\Database\Connection
  91. */
  92. public function getConnection($name = null)
  93. {
  94. return $this->manager->connection($name);
  95. }
  96. /**
  97. * Register a connection with the manager.
  98. *
  99. * @param array $config
  100. * @param string $name
  101. * @return void
  102. */
  103. public function addConnection(array $config, $name = 'default')
  104. {
  105. $connections = $this->container['config']['database.connections'];
  106. $connections[$name] = $config;
  107. $this->container['config']['database.connections'] = $connections;
  108. }
  109. /**
  110. * Bootstrap Eloquent so it is ready for usage.
  111. *
  112. * @return void
  113. */
  114. public function bootEloquent()
  115. {
  116. Eloquent::setConnectionResolver($this->manager);
  117. // If we have an event dispatcher instance, we will go ahead and register it
  118. // with the Eloquent ORM, allowing for model callbacks while creating and
  119. // updating "model" instances; however, it is not necessary to operate.
  120. if ($dispatcher = $this->getEventDispatcher()) {
  121. Eloquent::setEventDispatcher($dispatcher);
  122. }
  123. }
  124. /**
  125. * Set the fetch mode for the database connections.
  126. *
  127. * @param int $fetchMode
  128. * @return $this
  129. */
  130. public function setFetchMode($fetchMode)
  131. {
  132. $this->container['config']['database.fetch'] = $fetchMode;
  133. return $this;
  134. }
  135. /**
  136. * Get the database manager instance.
  137. *
  138. * @return \Illuminate\Database\DatabaseManager
  139. */
  140. public function getDatabaseManager()
  141. {
  142. return $this->manager;
  143. }
  144. /**
  145. * Get the current event dispatcher instance.
  146. *
  147. * @return \Illuminate\Contracts\Events\Dispatcher|null
  148. */
  149. public function getEventDispatcher()
  150. {
  151. if ($this->container->bound('events')) {
  152. return $this->container['events'];
  153. }
  154. }
  155. /**
  156. * Set the event dispatcher instance to be used by connections.
  157. *
  158. * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
  159. * @return void
  160. */
  161. public function setEventDispatcher(Dispatcher $dispatcher)
  162. {
  163. $this->container->instance('events', $dispatcher);
  164. }
  165. /**
  166. * Dynamically pass methods to the default connection.
  167. *
  168. * @param string $method
  169. * @param array $parameters
  170. * @return mixed
  171. */
  172. public static function __callStatic($method, $parameters)
  173. {
  174. return static::connection()->$method(...$parameters);
  175. }
  176. }