refactoring database and connector classes.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
<?php namespace System\DB;
|
||||
|
||||
use System\Config;
|
||||
|
||||
class Connector {
|
||||
|
||||
/**
|
||||
@@ -17,11 +19,13 @@ class Connector {
|
||||
/**
|
||||
* Establish a PDO database connection.
|
||||
*
|
||||
* @param object $config
|
||||
* @param string $connection
|
||||
* @return PDO
|
||||
*/
|
||||
public static function connect($config)
|
||||
public static function connect($connection)
|
||||
{
|
||||
$config = static::configuration($connection);
|
||||
|
||||
if ($config->driver == 'sqlite')
|
||||
{
|
||||
return static::connect_to_sqlite($config);
|
||||
@@ -37,14 +41,14 @@ class Connector {
|
||||
/**
|
||||
* Establish a PDO connection to a SQLite database.
|
||||
*
|
||||
* SQLite database paths can be specified either relative to the application/db
|
||||
* directory, or as an absolute path to any location on the file system.
|
||||
*
|
||||
* @param array $config
|
||||
* @return PDO
|
||||
*/
|
||||
private static function connect_to_sqlite($config)
|
||||
{
|
||||
// Database paths can either be specified relative to the application/storage/db
|
||||
// directory, or as an absolute path.
|
||||
|
||||
if (file_exists($path = APP_PATH.'storage/db/'.$config->database.'.sqlite'))
|
||||
{
|
||||
return new \PDO('sqlite:'.$path, null, null, static::$options);
|
||||
@@ -84,4 +88,22 @@ class Connector {
|
||||
return $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the configuration options for a database connection.
|
||||
*
|
||||
* @param string $connection
|
||||
* @return object
|
||||
*/
|
||||
private static function configuration($connection)
|
||||
{
|
||||
$config = Config::get('db.connections');
|
||||
|
||||
if ( ! array_key_exists($connection, $config))
|
||||
{
|
||||
throw new \Exception("Database connection [$connection] is not defined.");
|
||||
}
|
||||
|
||||
return (object) $config[$connection];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user