manager.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * <code>
  17. * // Get the default database connection for the application
  18. * $connection = DB::connection();
  19. *
  20. * // Get a specific connection by passing the connection name
  21. * $connection = DB::connection('mysql');
  22. * </code>
  23. *
  24. * @param string $connection
  25. * @return Connection
  26. */
  27. public static function connection($connection = null)
  28. {
  29. if (is_null($connection)) $connection = Config::get('database.default');
  30. if ( ! array_key_exists($connection, static::$connections))
  31. {
  32. $config = Config::get("database.connections.{$connection}");
  33. if (is_null($config))
  34. {
  35. throw new \Exception("Database connection is not defined for connection [$connection].");
  36. }
  37. static::$connections[$connection] = new Connection(static::connect($config), $config);
  38. }
  39. return static::$connections[$connection];
  40. }
  41. /**
  42. * Get a PDO database connection for a given database configuration.
  43. *
  44. * @param array $config
  45. * @return PDO
  46. */
  47. protected static function connect($config)
  48. {
  49. // We allow the developer to place a "connector" option in the database
  50. // configuration, which should have a Closure value. If the connector
  51. // is present, we will use the Closure to retrieve the PDO connection
  52. // to the database. This allows the flexiblity to connect to database
  53. // systems that are not officially supported by the the framework.
  54. if (isset($config['connector']))
  55. {
  56. return call_user_func($config['connector'], $config);
  57. }
  58. return IoC::container()->core("database.connectors.{$config['driver']}")->connect($config);
  59. }
  60. /**
  61. * Begin a fluent query against a table.
  62. *
  63. * @param string $table
  64. * @param string $connection
  65. * @return Queries\Query
  66. */
  67. public static function table($table, $connection = null)
  68. {
  69. return static::connection($connection)->table($table);
  70. }
  71. /**
  72. * Create a new database expression instance.
  73. *
  74. * Database expressions are used to inject raw SQL into a fluent query.
  75. *
  76. * @param string $value
  77. * @return Expression
  78. */
  79. public static function raw($value)
  80. {
  81. return new Expression($value);
  82. }
  83. /**
  84. * Magic Method for calling methods on the default database connection.
  85. *
  86. * This provides a convenient API for querying or examining the default database connection.
  87. */
  88. public static function __callStatic($method, $parameters)
  89. {
  90. return call_user_func_array(array(static::connection(), $method), $parameters);
  91. }
  92. }