manager.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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("Connection is not defined for [$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. * The database connectors are responsible for simply establishing a PDO
  64. * database connection given a configuration. This allows us to easily
  65. * drop in support for new database systems by writing a connector.
  66. *
  67. * @param string $driver
  68. * @return Connector
  69. */
  70. protected static function connector($driver)
  71. {
  72. switch ($driver)
  73. {
  74. case 'sqlite':
  75. return new Connectors\SQLite(DATABASE_PATH);
  76. case 'mysql':
  77. return new Connectors\MySQL;
  78. case 'pgsql':
  79. return new Connectors\Postgres;
  80. default:
  81. throw new \DomainException("Database driver [$driver] is not supported.");
  82. }
  83. }
  84. /**
  85. * Begin a fluent query against a table.
  86. *
  87. * @param string $table
  88. * @param string $connection
  89. * @return Queries\Query
  90. */
  91. public static function table($table, $connection = null)
  92. {
  93. return static::connection($connection)->table($table);
  94. }
  95. /**
  96. * Create a new database expression instance.
  97. *
  98. * Database expressions are used to inject raw SQL into a fluent query.
  99. *
  100. * @param string $value
  101. * @return Expression
  102. */
  103. public static function raw($value)
  104. {
  105. return new Expression($value);
  106. }
  107. /**
  108. * Magic Method for calling methods on the default database connection.
  109. *
  110. * This provides a convenient API for querying or examining the default database connection.
  111. */
  112. public static function __callStatic($method, $parameters)
  113. {
  114. return call_user_func_array(array(static::connection(), $method), $parameters);
  115. }
  116. }