130 lines
3 KiB
PHP
130 lines
3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Datastore Class File for PICKLES
|
|
*
|
|
* PHP version 5
|
|
*
|
|
* Licensed under The MIT License
|
|
* Redistribution of these files must retain the above copyright notice.
|
|
*
|
|
* @author Josh Sherman <pickles@joshtronic.com>
|
|
* @copyright Copyright 2007-2013, Josh Sherman
|
|
* @license http://www.opensource.org/licenses/mit-license.html
|
|
* @package PICKLES
|
|
* @link https://github.com/joshtronic/pickles
|
|
*/
|
|
|
|
/**
|
|
* Datastore Factory
|
|
*
|
|
* Generic class to simplify connecting to a datastore.
|
|
*/
|
|
class Datastore extends Object
|
|
{
|
|
/**
|
|
* Constructor
|
|
*
|
|
* Attempts to get an instance of the passed datastore name or attempts
|
|
* to use a default specified in the config.
|
|
*
|
|
* @param string $name optional name of the connection to use
|
|
*/
|
|
public function __construct(String $name = null)
|
|
{
|
|
parent::__construct();
|
|
|
|
return Datastore::getInstance($name);
|
|
}
|
|
|
|
/**
|
|
* Get Instance
|
|
*
|
|
* Looks up the datasource using the passed name and gets an instance
|
|
* of it. Allows for easy sharing of certain classes within the system
|
|
* to avoid the extra overhead of creating new objects each time. Also
|
|
* avoids the hassle of passing around variables (yeah I know, very
|
|
* global-ish, don't just me).
|
|
*
|
|
* @static
|
|
* @param string $name name of the datasource
|
|
* @return object instance of the class
|
|
*/
|
|
public static function getInstance($name = null)
|
|
{
|
|
$config = Config::getInstance();
|
|
|
|
// Checks if we have a default
|
|
if ($name == null)
|
|
{
|
|
// Checks the config for a default
|
|
if (isset($config->pickles['datasource']))
|
|
{
|
|
$name = $config->pickles['datasource'];
|
|
}
|
|
// Tries to use the first defined datasource
|
|
elseif (is_array($config->datasources))
|
|
{
|
|
$datasources = $config->datasources;
|
|
$name = key($datasources);
|
|
}
|
|
}
|
|
|
|
// If we have a name try to set up a connection
|
|
if ($name != null)
|
|
{
|
|
if (isset($config->datasources[$name]))
|
|
{
|
|
$datasource = $config->datasources[$name];
|
|
|
|
if (!isset($datasource['driver']))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
$datasource['driver'] = strtolower($datasource['driver']);
|
|
|
|
if (!isset(self::$instances[$name]))
|
|
{
|
|
// Checks the driver is legit and scrubs the name
|
|
switch ($datasource['driver'])
|
|
{
|
|
case 'pdo_mysql': $class = 'PDO_MySQL'; break;
|
|
case 'pdo_pgsql': $class = 'PDO_PostgreSQL'; break;
|
|
case 'pdo_sqlite': $class = 'PDO_SQLite'; break;
|
|
|
|
default:
|
|
throw new Exception('Datasource driver "' . $datasource['driver'] . '" is invalid');
|
|
break;
|
|
}
|
|
|
|
// Instantiates our datastore class
|
|
$class = 'Datastore_' . $class;
|
|
$instance = new $class();
|
|
|
|
// Sets our database parameters
|
|
if (is_array($datasource))
|
|
{
|
|
foreach ($datasource as $variable => $value)
|
|
{
|
|
$instance->$variable = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Caches the instance for possible reuse later
|
|
if (isset($instance))
|
|
{
|
|
self::$instances[$name] = $instance;
|
|
}
|
|
|
|
// Returns the instance
|
|
return self::$instances[$name];
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
?>
|