manager.php 2.71 KB
Newer Older
1
<?php namespace Laravel\Database;
Taylor Otwell committed
2

3
use Laravel\IoC;
4 5
use Laravel\Config;

6
class Manager {
Taylor Otwell committed
7 8 9 10 11 12

	/**
	 * The established database connections.
	 *
	 * @var array
	 */
13
	protected static $connections = array();
Taylor Otwell committed
14 15

	/**
16
	 * Get a database connection.
17
	 *
18
	 * If no database name is specified, the default connection will be returned.
Taylor Otwell committed
19
	 *
Taylor Otwell committed
20 21 22 23 24 25 26 27
	 * <code>
	 *		// Get the default database connection for the application
	 *		$connection = DB::connection();
	 *
	 *		// Get a specific connection by passing the connection name
	 *		$connection = DB::connection('mysql');
	 * </code>
	 *
Taylor Otwell committed
28 29
	 * @param  string      $connection
	 * @return Connection
Taylor Otwell committed
30
	 */
31
	public static function connection($connection = null)
Taylor Otwell committed
32
	{
33
		if (is_null($connection)) $connection = Config::get('database.default');
Taylor Otwell committed
34

35
		if ( ! array_key_exists($connection, static::$connections))
Taylor Otwell committed
36
		{
37
			$config = Config::get("database.connections.{$connection}");
38 39

			if (is_null($config))
Taylor Otwell committed
40
			{
41
				throw new \Exception("Database connection is not defined for connection [$connection].");
Taylor Otwell committed
42 43
			}

44
			static::$connections[$connection] = new Connection(static::connect($config), $config);
Taylor Otwell committed
45 46
		}

47
		return static::$connections[$connection];
Taylor Otwell committed
48 49 50
	}

	/**
51 52 53 54 55
	 * Get a PDO database connection for a given database configuration.
	 *
	 * @param  array  $config
	 * @return PDO
	 */
56
	protected static function connect($config)
57
	{
Taylor Otwell committed
58 59 60 61 62
		// We allow the developer to place a "connector" option in the database
		// configuration, which should have a Closure value. If the connector
		// is present, we will use the Closure to retrieve the PDO connection
		// to the database. This allows the flexiblity to connect to database
		// systems that are not officially supported by the the framework.
63
		if (isset($config['connector']))
64
		{
65
			return call_user_func($config['connector'], $config);
66 67
		}

68
		return IoC::container()->core("database.connectors.{$config['driver']}")->connect($config);
69 70 71
	}

	/**
Taylor Otwell committed
72 73
	 * Begin a fluent query against a table.
	 *
74 75
	 * @param  string          $table
	 * @param  string          $connection
76
	 * @return Queries\Query
Taylor Otwell committed
77
	 */
78
	public static function table($table, $connection = null)
Taylor Otwell committed
79
	{
80
		return static::connection($connection)->table($table);
Taylor Otwell committed
81 82 83
	}

	/**
84 85 86 87 88 89 90 91 92 93 94 95 96
	 * Create a new database expression instance.
	 *
	 * Database expressions are used to inject raw SQL into a fluent query.
	 *
	 * @param  string      $value
	 * @return Expression
	 */
	public static function raw($value)
	{
		return new Expression($value);
	}

	/**
Taylor Otwell committed
97
	 * Magic Method for calling methods on the default database connection.
Taylor Otwell committed
98 99
	 *
	 * This provides a convenient API for querying or examining the default database connection.
Taylor Otwell committed
100
	 */
101
	public static function __callStatic($method, $parameters)
Taylor Otwell committed
102
	{
103
		return call_user_func_array(array(static::connection(), $method), $parameters);
Taylor Otwell committed
104 105 106
	}

}