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

78 lines
2.0 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Database;
2011-08-01 17:58:20 -05:00
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
/**
* Create a new database manager instance.
*
* @param array $config
* @return void
2011-08-26 21:42:04 -05:00
*/
2011-09-15 23:03:47 -05:00
public function __construct($config)
{
$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['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
{
if ( ! isset($this->config['connectors'][$connection]))
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
}
// Database connections are established by developer configurable connector closures.
// This provides the developer the maximum amount of freedom in establishing their
// database connections, and allows the framework to remain agonstic to ugly database
// specific PDO connection details. Less code. Less bugs.
2011-09-16 19:59:20 -05:00
$pdo = call_user_func($this->config['connectors'][$connection]);
$this->connections[$connection] = new Connection($pdo, $this->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
}
/**
* 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
}
}