1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
<?php
/**
* DB Class ORM.
*
* @package Silla.IO
* @subpackage Core\Modules\DB
* @author Kalin Stefanov <kalin@athlonsofia.com>
* @copyright Copyright (c) 2015, Silla.io
* @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3.0 (GPLv3)
*/
namespace Core\Modules\DB;
use Core;
/**
* DB Class ORM definition.
*/
final class DB
{
/**
* Instance of the singleton.
*
* @var DB
* @static
* @access public
*/
public static $instance = null;
/**
* Stores all executed queries.
*
* @var array
* @static
* @access public
*/
public static $queries = array();
/**
* Stores all instances.
*
* @var array
* @static
* @access private
*/
private static $instances = array();
/**
* Cloning of DB is disallowed.
*
* @access public
*
* @return void
*/
public function __clone()
{
trigger_error(__CLASS__ . ' cannot be cloned! It is a singleton.', E_USER_ERROR);
}
/**
* Constructor, does nothing.
*
* @access private
*/
private function __construct()
{
}
/**
* If we have instance return it, else create it.
*
* @param array $dsn Array containing all the credentials.
*
* @throws \LogicException Cannot establish a database connection.
* @access public
* @static
* @final
*
* @return Adapters\PdoMySql|Adapters\MySQLi|Adapters\MySQL|Adapters\SQLite Adapter Instance.
*/
final public static function getInstance(array $dsn)
{
if (null === self::$instance) {
try {
self::$instance = self::createInstance($dsn);
} catch (\Exception $e) {
throw new \LogicException('Cannot establish a database connection');
}
}
return self::$instance;
}
/**
* Creates a DB instance object.
*
* @param array $dsn Array containing all of the credentials.
*
* @static
* @throws \DomainException Unsupported Database adapter.
*
* @return Adapters\PdoMySql|Adapters\MySQLi|Adapters\MySQL|Adapters\SQLite Adapter Instance.
*/
private static function createInstance(array $dsn)
{
$hash = md5(serialize($dsn));
$instance = null;
if (!isset(self::$instances[$hash])) {
switch ($dsn['adapter']) {
case 'pdo_mysql':
$instance = new Adapters\PdoMySql(
'mysql:host=' . $dsn['host'] . ';dbname=' . $dsn['name'],
$dsn['user'],
$dsn['password']
);
$instance->setCharset();
$instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
break;
case 'mysqli':
$instance = new Adapters\MySQLi($dsn['host'], $dsn['name'], $dsn['user'], $dsn['password']);
$instance->setCharset();
break;
case 'mysql':
$instance = new Adapters\MySQL($dsn['host'], $dsn['name'], $dsn['user'], $dsn['password']);
$instance->setCharset();
break;
case 'sqlite':
$instance = new Adapters\SQLite($dsn['host']);
$instance->setCharset();
break;
default:
throw new \DomainException('Unsupported Database adapter: ' . $dsn['adapter']);
break;
}
self::$instances[$hash] = $instance;
$instance->cache = new DbCache($dsn);
}
return self::$instances[$hash];
}
/**
* Retrieves cache.
*
* @param array $dsn Array containing all the credentials.
*
* @static
* @final
* @access public
*
* @return mixed
*/
final public static function getCache(array $dsn)
{
if (!self::$instance) {
self::$instance = self::createInstance($dsn);
}
return self::$instance->cache;
}
/**
* Create a closure with different DSN.
*
* @param array $dsn Array containing all the credentials.
* @param mixed $scope Scope of execution.
*
* @access public
* @static
* @final
*
* @return void
*/
final public static function with(array $dsn, $scope)
{
$default_instance = self::$instance;
self::$instance = self::createInstance($dsn);
if (is_callable($scope)) {
$scope(self::$instance);
}
self::$instance = $default_instance;
}
}