ConnectionResolver.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Illuminate\Database;
  3. class ConnectionResolver implements ConnectionResolverInterface
  4. {
  5. /**
  6. * All of the registered connections.
  7. *
  8. * @var array
  9. */
  10. protected $connections = [];
  11. /**
  12. * The default connection name.
  13. *
  14. * @var string
  15. */
  16. protected $default;
  17. /**
  18. * Create a new connection resolver instance.
  19. *
  20. * @param array $connections
  21. * @return void
  22. */
  23. public function __construct(array $connections = [])
  24. {
  25. foreach ($connections as $name => $connection) {
  26. $this->addConnection($name, $connection);
  27. }
  28. }
  29. /**
  30. * Get a database connection instance.
  31. *
  32. * @param string $name
  33. * @return \Illuminate\Database\ConnectionInterface
  34. */
  35. public function connection($name = null)
  36. {
  37. if (is_null($name)) {
  38. $name = $this->getDefaultConnection();
  39. }
  40. return $this->connections[$name];
  41. }
  42. /**
  43. * Add a connection to the resolver.
  44. *
  45. * @param string $name
  46. * @param \Illuminate\Database\ConnectionInterface $connection
  47. * @return void
  48. */
  49. public function addConnection($name, ConnectionInterface $connection)
  50. {
  51. $this->connections[$name] = $connection;
  52. }
  53. /**
  54. * Check if a connection has been registered.
  55. *
  56. * @param string $name
  57. * @return bool
  58. */
  59. public function hasConnection($name)
  60. {
  61. return isset($this->connections[$name]);
  62. }
  63. /**
  64. * Get the default connection name.
  65. *
  66. * @return string
  67. */
  68. public function getDefaultConnection()
  69. {
  70. return $this->default;
  71. }
  72. /**
  73. * Set the default connection name.
  74. *
  75. * @param string $name
  76. * @return void
  77. */
  78. public function setDefaultConnection($name)
  79. {
  80. $this->default = $name;
  81. }
  82. }