manager.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 ( ! isset(static::$connections[$connection]))
  31. {
  32. $config = Config::get("database.connections.{$connection}");
  33. if (is_null($config))
  34. {
  35. throw new \OutOfBoundsException("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 static::connector($config['driver'])->connect($config);
  59. }
  60. /**
  61. * Create a new database connector instance.
  62. *
  63. * @param string $driver
  64. * @return Connector
  65. */
  66. protected static function connector($driver)
  67. {
  68. switch ($driver)
  69. {
  70. case 'sqlite':
  71. return new Connectors\SQLite(DATABASE_PATH);
  72. case 'mysql':
  73. return new Connectors\MySQL;
  74. case 'pgsql':
  75. return new Connectors\Postgres;
  76. default:
  77. throw new \DomainException("Database driver [$driver] is not supported.");
  78. }
  79. }
  80. /**
  81. * Begin a fluent query against a table.
  82. *
  83. * @param string $table
  84. * @param string $connection
  85. * @return Queries\Query
  86. */
  87. public static function table($table, $connection = null)
  88. {
  89. return static::connection($connection)->table($table);
  90. }
  91. /**
  92. * Create a new database expression instance.
  93. *
  94. * Database expressions are used to inject raw SQL into a fluent query.
  95. *
  96. * @param string $value
  97. * @return Expression
  98. */
  99. public static function raw($value)
  100. {
  101. return new Expression($value);
  102. }
  103. /**
  104. * Magic Method for calling methods on the default database connection.
  105. *
  106. * This provides a convenient API for querying or examining the default database connection.
  107. */
  108. public static function __callStatic($method, $parameters)
  109. {
  110. return call_user_func_array(array(static::connection(), $method), $parameters);
  111. }
  112. }