From 3e874867b8b684a86cf20921dbefd9b8187cd322 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 4 Sep 2011 21:45:37 -0500 Subject: [PATCH] database refactoring for dependency injection and other general refactoring. --- laravel/config/container.php | 2 +- laravel/database/connection.php | 22 +++++++++++++++++----- laravel/database/manager.php | 12 ++++++------ laravel/database/query.php | 4 ++-- laravel/database/query/factory.php | 8 ++++---- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/laravel/config/container.php b/laravel/config/container.php index d0e0cf41..8de3e0f0 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -45,7 +45,7 @@ return array( { $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')); }), diff --git a/laravel/database/connection.php b/laravel/database/connection.php index e1247388..0ef33f67 100644 --- a/laravel/database/connection.php +++ b/laravel/database/connection.php @@ -40,15 +40,19 @@ class Connection { /** * Create a new Connection instance. * - * @param string $name - * @param array $config - * @param Connector $connector + * @param Connector $connector + * @param Query\Factory $factory + * @param Compiler\Factory $compiler + * @param string $name + * @param array $config * @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->query = $query; $this->config = $config; + $this->compiler = $compiler; $this->connector = $connector; } @@ -152,7 +156,7 @@ class Connection { */ 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 { 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 diff --git a/laravel/database/manager.php b/laravel/database/manager.php index 92e66df3..6457e10a 100644 --- a/laravel/database/manager.php +++ b/laravel/database/manager.php @@ -22,7 +22,7 @@ class Manager { * * @var Connector\Factory */ - protected $factory; + protected $connector; /** * The database connection configurations. @@ -41,16 +41,16 @@ class Manager { /** * Create a new database manager instance. * - * @param Connector\Factory $factory + * @param Connector\Factory $connector * @param array $config * @param string $default * @return void */ - public function __construct(Connector\Factory $factory, $config, $default) + public function __construct(Connector\Factory $connector, $config, $default) { $this->config = $config; - $this->factory = $factory; $this->default = $default; + $this->connector = $connector; } /** @@ -73,9 +73,9 @@ class Manager { 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]; diff --git a/laravel/database/query.php b/laravel/database/query.php index 803c3880..44939856 100644 --- a/laravel/database/query.php +++ b/laravel/database/query.php @@ -90,12 +90,12 @@ class Query { /** * Create a new query instance. * - * @param string $table * @param Connection $connection * @param Compiler $compiler + * @param string $table * @return void */ - public function __construct($table, Connection $connection, Query\Compiler $compiler) + public function __construct(Connection $connection, Query\Compiler $compiler, $table) { $this->table = $table; $this->compiler = $compiler; diff --git a/laravel/database/query/factory.php b/laravel/database/query/factory.php index c68821c9..d7fd4c13 100644 --- a/laravel/database/query/factory.php +++ b/laravel/database/query/factory.php @@ -8,20 +8,20 @@ class Factory { /** * Create a new query instance based on the connection driver. * - * @param string $table * @param Connection $connection * @param Compiler $compiler + * @param string $table * @return Query */ - public static function make($table, Connection $connection, Compiler $compiler) + public function make(Connection $connection, Compiler $compiler, $table) { switch ($connection->driver()) { case 'pgsql': - return new Postgres($table, $connection, $compiler); + return new Postgres($connection, $compiler, $table); default: - return new Query($table, $connection, $compiler); + return new Query($connection, $compiler, $table); } }