forked from avdg/ppi-framework-old
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSource.php
More file actions
102 lines (86 loc) · 2.07 KB
/
DataSource.php
File metadata and controls
102 lines (86 loc) · 2.07 KB
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
<?php
/**
* @author Paul Dragoonis <dragoonis@php.net>
* @license http://opensource.org/licenses/mit-license.php MIT
* @package DataSource
* @link www.ppiframework.com
*/
class PPI_DataSource {
/**
* List of configuration sets
*
* @var array
*/
protected $_config = array();
/**
* List of connections to return via singleton-like
*
* @var array
*/
protected $_handles = array();
/**
* The constructor, taking in options which are currently
*
* @param array $options
*/
function __construct(array $options = array()) {
$this->_config = $options;
}
/**
* Create a new instance of ourself.
*
* @static
* @param array $options
* @return PPI_DataSource
*/
static function create(array $options = array()) {
return new self($options);
}
/**
* The DataSource Factory - this is where we manufacture our drivers
*
* @throws PPI_Exception
* @param string $key
* @return object
*/
function factory(array $options) {
// Apply our default prefix
if(!isset($options['prefix'])) {
$options['prefix'] = 'PPI_DataSource_';
}
// Lets get our suffix, to load up the right adapter, (PPI_DataSource_[PDO|Mongo])
if($options['type'] === 'mongo') {
$suffix = 'Mongo';
} elseif(substr($options['type'], 0, 4) === 'pdo_') {
$suffix = 'PDO';
} else {
$suffix = $options['type'];
}
// Lets instantiate up and get our driver
$adapterName = $options['prefix'] . $suffix;
$adapter = new $adapterName();
$driver = $adapter->getDriver($options);
return $driver;
}
/**
* Return the connection from the factory
*
* @throws PPI_Exception
* @param string $key
* @return object
*/
function getConnection($key) {
// Connection Caching
if(isset($this->_handles[$key])) {
return $this->_handles[$key];
}
// Check that we asked for a valid key
if(!isset($this->_config[$key])) {
throw new PPI_Exception('Invalid DataSource Key: ' . $key);
}
$conn = $this->factory($this->_config[$key]);
// Connection Caching
$this->_handles[$key] = $conn;
return $conn;
}
}