manager.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php namespace Laravel\Database;
  2. use Laravel\Config;
  3. class Manager {
  4. /**
  5. * The established database connections.
  6. *
  7. * @var array
  8. */
  9. protected static $connections = array();
  10. /**
  11. * Get a database connection.
  12. *
  13. * If no database name is specified, the default connection will be returned.
  14. *
  15. * Note: Database connections are managed as singletons.
  16. *
  17. * <code>
  18. * // Get the default database connection for the application
  19. * $connection = DB::connection();
  20. *
  21. * // Get a specific connection by passing the connection name
  22. * $connection = DB::connection('mysql');
  23. * </code>
  24. *
  25. * @param string $connection
  26. * @return Connection
  27. */
  28. public static function connection($connection = null)
  29. {
  30. if (is_null($connection)) $connection = Config::get('database.default');
  31. if ( ! array_key_exists($connection, static::$connections))
  32. {
  33. $config = Config::get("database.connections.{$connection}");
  34. if (is_null($config))
  35. {
  36. throw new \Exception("Database connection configuration is not defined for connection [$connection].");
  37. }
  38. static::$connections[$connection] = new Connection(static::connect($config), $config);
  39. }
  40. return static::$connections[$connection];
  41. }
  42. /**
  43. * Get a PDO database connection for a given database configuration.
  44. *
  45. * @param array $config
  46. * @return PDO
  47. */
  48. protected static function connect($config)
  49. {
  50. if (isset($config['connector'])) { return call_user_func($config['connector'], $config); }
  51. switch ($config['driver'])
  52. {
  53. case 'sqlite':
  54. $connector = new Connectors\SQLite;
  55. break;
  56. case 'mysql':
  57. $connector = new Connectors\MySQL;
  58. break;
  59. case 'pgsql':
  60. $connector = new Connectors\Postgres;
  61. break;
  62. default:
  63. throw new \Exception("Database driver [{$config['driver']}] is not supported.");
  64. }
  65. return $connector->connect($config);
  66. }
  67. /**
  68. * Begin a fluent query against a table.
  69. *
  70. * @param string $table
  71. * @param string $connection
  72. * @return Queries\Query
  73. */
  74. public static function table($table, $connection = null)
  75. {
  76. return static::connection($connection)->table($table);
  77. }
  78. /**
  79. * Magic Method for calling methods on the default database connection.
  80. *
  81. * This provides a convenient API for querying or examining the default database connection.
  82. */
  83. public static function __callStatic($method, $parameters)
  84. {
  85. return call_user_func_array(array(static::connection(), $method), $parameters);
  86. }
  87. }