manager.php 2.4 KB

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