Commit 3e874867 authored by Taylor Otwell's avatar Taylor Otwell

database refactoring for dependency injection and other general refactoring.

parent c2f5e7ee
...@@ -45,7 +45,7 @@ return array( ...@@ -45,7 +45,7 @@ return array(
{ {
$config = $container->resolve('laravel.config'); $config = $container->resolve('laravel.config');
return new Database\Manager($config->get('database.connections'), $config->get('database.default')); return new Database\Manager(new Database\Connector\Factory, $config->get('database.connections'), $config->get('database.default'));
}), }),
......
...@@ -40,15 +40,19 @@ class Connection { ...@@ -40,15 +40,19 @@ class Connection {
/** /**
* Create a new Connection instance. * Create a new Connection instance.
* *
* @param Connector $connector
* @param Query\Factory $factory
* @param Compiler\Factory $compiler
* @param string $name * @param string $name
* @param array $config * @param array $config
* @param Connector $connector
* @return void * @return void
*/ */
public function __construct($name, $config, Connector $connector) public function __construct(Connector $connector, Query\Factory $query, Query\Compiler\Factory $compiler, $name, $config)
{ {
$this->name = $name; $this->name = $name;
$this->query = $query;
$this->config = $config; $this->config = $config;
$this->compiler = $compiler;
$this->connector = $connector; $this->connector = $connector;
} }
...@@ -152,7 +156,7 @@ class Connection { ...@@ -152,7 +156,7 @@ class Connection {
*/ */
public function table($table) public function table($table)
{ {
return Query\Factory::make($table, $this, Query\Compiler\Factory::make($this)); return $this->query->make($this, $this->compiler->make($this), $table);
} }
/** /**
...@@ -167,4 +171,12 @@ class Connection { ...@@ -167,4 +171,12 @@ class Connection {
return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
} }
/**
* Magic Method for dynamically beginning queries on database tables.
*/
public function __call($method, $parameters)
{
return $this->table($method);
}
} }
\ No newline at end of file
...@@ -22,7 +22,7 @@ class Manager { ...@@ -22,7 +22,7 @@ class Manager {
* *
* @var Connector\Factory * @var Connector\Factory
*/ */
protected $factory; protected $connector;
/** /**
* The database connection configurations. * The database connection configurations.
...@@ -41,16 +41,16 @@ class Manager { ...@@ -41,16 +41,16 @@ class Manager {
/** /**
* Create a new database manager instance. * Create a new database manager instance.
* *
* @param Connector\Factory $factory * @param Connector\Factory $connector
* @param array $config * @param array $config
* @param string $default * @param string $default
* @return void * @return void
*/ */
public function __construct(Connector\Factory $factory, $config, $default) public function __construct(Connector\Factory $connector, $config, $default)
{ {
$this->config = $config; $this->config = $config;
$this->factory = $factory;
$this->default = $default; $this->default = $default;
$this->connector = $connector;
} }
/** /**
...@@ -73,9 +73,9 @@ class Manager { ...@@ -73,9 +73,9 @@ class Manager {
throw new \Exception("Database connection [$connection] is not defined."); throw new \Exception("Database connection [$connection] is not defined.");
} }
$connector = $this->factory->make($this->config[$connection]); list($connector, $query, $compiler) = array($this->connector->make($this->config[$connection]), new Query\Factory, new Query\Compiler\Factory);
static::$connections[$connection] = new Connection($connection, $this->config[$connection], $connector); $this->connections[$connection] = new Connection($connector, $query, $compiler, $connection, $this->config[$connection]);
} }
return $this->connections[$connection]; return $this->connections[$connection];
......
...@@ -90,12 +90,12 @@ class Query { ...@@ -90,12 +90,12 @@ class Query {
/** /**
* Create a new query instance. * Create a new query instance.
* *
* @param string $table
* @param Connection $connection * @param Connection $connection
* @param Compiler $compiler * @param Compiler $compiler
* @param string $table
* @return void * @return void
*/ */
public function __construct($table, Connection $connection, Query\Compiler $compiler) public function __construct(Connection $connection, Query\Compiler $compiler, $table)
{ {
$this->table = $table; $this->table = $table;
$this->compiler = $compiler; $this->compiler = $compiler;
......
...@@ -8,20 +8,20 @@ class Factory { ...@@ -8,20 +8,20 @@ class Factory {
/** /**
* Create a new query instance based on the connection driver. * Create a new query instance based on the connection driver.
* *
* @param string $table
* @param Connection $connection * @param Connection $connection
* @param Compiler $compiler * @param Compiler $compiler
* @param string $table
* @return Query * @return Query
*/ */
public static function make($table, Connection $connection, Compiler $compiler) public function make(Connection $connection, Compiler $compiler, $table)
{ {
switch ($connection->driver()) switch ($connection->driver())
{ {
case 'pgsql': case 'pgsql':
return new Postgres($table, $connection, $compiler); return new Postgres($connection, $compiler, $table);
default: default:
return new Query($table, $connection, $compiler); return new Query($connection, $compiler, $table);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment