forked from avdg/ppi-framework-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSource.php
49 lines (36 loc) · 1.05 KB
/
DataSource.php
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
<?php
class PPI_DataSource {
protected static $_sources = array();
protected static $_connections = array();
function __construct() {}
function factory($key) {
// Connection Caching
if(isset(self::$_connections[$key])) {
return self::$_connections[$key];
}
// Check that we asked for a valid key
if(!isset(self::$_sources[$key])) {
throw new PPI_Exception('Invalid DataSource Key: ' . $key);
}
// See if re recognise our data source's type
$options = self::$_sources[$key];
if(!isset($options['prefix'])) {
$options['prefix'] = 'PPI_DataSource_';
}
if($options['type'] === 'mongo') {
$suffix = 'Mongo';
} elseif(substr($options['type'], 0, 4) === 'pdo_') {
$suffix = 'PDO';
} else {
$suffix = $options['type'];
}
$adapterName = $options['prefix'] . $suffix;
$adapter = new $adapterName();
$driver = $adapter->getDriver($options);
self::$_connections[$key] = $driver; // Connection Caching
return $driver;
}
static function add($key, array $options) {
self::$_sources[$key] = $options;
}
}