Files
spa-api/laravel/database/manager.php

114 lines
2.4 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Database;
2011-08-01 17:58:20 -05:00
use Laravel\Config;
class Manager {
2011-08-01 17:58:20 -05:00
/**
* The established database connections.
*
* @var array
*/
protected $connections = array();
2011-08-26 21:42:04 -05:00
/**
* The configuration manager instance.
*
* @var Config
*/
protected $config;
2011-08-26 21:42:04 -05:00
/**
* Create a new database manager instance.
*
* @param Connector $connector
* @return void
2011-08-26 21:42:04 -05:00
*/
public function __construct(Config $config)
2011-09-15 23:03:47 -05:00
{
$this->config = $config;
}
2011-08-01 17:58:20 -05:00
/**
2011-09-16 19:59:20 -05:00
* Get a database connection.
*
2011-09-16 19:59:20 -05:00
* If no database name is specified, the default connection will be returned.
2011-08-01 17:58:20 -05:00
*
* Note: Database connections are managed as singletons.
*
2011-09-15 23:03:47 -05:00
* @param string $connection
* @return Connection
2011-08-01 17:58:20 -05:00
*/
2011-08-26 21:42:04 -05:00
public function connection($connection = null)
2011-08-01 17:58:20 -05:00
{
if (is_null($connection)) $connection = $this->config->get('database.default');
2011-08-01 17:58:20 -05:00
2011-08-26 21:42:04 -05:00
if ( ! array_key_exists($connection, $this->connections))
2011-08-01 17:58:20 -05:00
{
$config = $this->config->get("database.connections.{$connection}");
if (is_null($config))
2011-08-01 17:58:20 -05:00
{
throw new \Exception("Database connection configuration is not defined for connection [$connection].");
2011-08-01 17:58:20 -05:00
}
$this->connections[$connection] = new Connection($this->connect($config), $config);
2011-08-01 17:58:20 -05:00
}
2011-08-26 21:42:04 -05:00
return $this->connections[$connection];
2011-08-01 17:58:20 -05:00
}
/**
* Get a PDO database connection for a given database configuration.
*
* @param array $config
* @return PDO
*/
protected function connect($config)
{
if (isset($config['connector'])) { return call_user_func($config['connector'], $config); }
switch ($config['driver'])
{
case 'sqlite':
$connector = new Connectors\SQLite;
break;
case 'mysql':
$connector = new Connectors\MySQL;
break;
case 'pgsql':
$connector = new Connectors\Postgres;
break;
default:
throw new \Exception("Database driver [{$config['driver']}] is not supported.");
}
return $connector->connect($config);
}
2011-08-01 17:58:20 -05:00
/**
* Begin a fluent query against a table.
*
* @param string $table
* @param string $connection
* @return Queries\Query
2011-08-01 17:58:20 -05:00
*/
2011-08-26 21:42:04 -05:00
public function table($table, $connection = null)
2011-08-01 17:58:20 -05:00
{
2011-08-26 21:42:04 -05:00
return $this->connection($connection)->table($table);
2011-08-01 17:58:20 -05:00
}
/**
* Magic Method for calling methods on the default database connection.
2011-08-19 20:12:39 -05:00
*
* This provides a convenient API for querying or examining the default database connection.
2011-08-01 17:58:20 -05:00
*/
2011-08-26 21:42:04 -05:00
public function __call($method, $parameters)
2011-08-01 17:58:20 -05:00
{
2011-08-26 21:42:04 -05:00
return call_user_func_array(array($this->connection(), $method), $parameters);
2011-08-01 17:58:20 -05:00
}
}