|
1
|
|
|
<?php |
|
2
|
|
|
namespace Darya\Database; |
|
3
|
|
|
|
|
4
|
|
|
use Darya\Database\Connection; |
|
5
|
|
|
use UnexpectedValueException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Darya's database connection factory. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Chris Andrew <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class Factory { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The class name or database type name to use by default |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $default = 'mysql'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* A map of database type names to connection implementation classes |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $map = array( |
|
27
|
|
|
'mysql' => 'Darya\Database\Connection\MySql', |
|
28
|
|
|
'mssql' => 'Darya\Database\Connection\SqlServer', |
|
29
|
|
|
'sqlserver' => 'Darya\Database\Connection\SqlServer', |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Instantiate a new database factory. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $default |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($default = null) { |
|
38
|
|
|
$this->default = $default ?: $this->default; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Prepare an options array by ensuring the existence of expected keys. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $options |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function prepareOptions(array $options) { |
|
48
|
|
|
return array_merge(array( |
|
49
|
|
|
'hostname' => 'localhost', |
|
50
|
|
|
'username' => null, |
|
51
|
|
|
'password' => null, |
|
52
|
|
|
'database' => null, |
|
53
|
|
|
'port' => null |
|
54
|
|
|
), $options); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Resolve a class name from the given string. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $string |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function resolveClass($string) { |
|
64
|
|
|
if (class_exists($string)) { |
|
65
|
|
|
return $string; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (!isset($this->map[$string])) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->map[$string]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Create a new database connection using the given name/class and options. |
|
77
|
|
|
* |
|
78
|
|
|
* @param class|string $name |
|
79
|
|
|
* @param array $options Expects keys 'hostname', 'username', 'password', |
|
80
|
|
|
* 'database' and optionally 'port' |
|
81
|
|
|
* @return Connection |
|
82
|
|
|
*/ |
|
83
|
|
|
public function create($name = null, array $options = array()) { |
|
84
|
|
|
$name = $name ?: $this->default; |
|
85
|
|
|
$class = $this->resolveClass($name); |
|
86
|
|
|
$options = $this->prepareOptions($options); |
|
87
|
|
|
|
|
88
|
|
|
if (!class_exists($class) || !is_subclass_of($class, 'Darya\Database\Connection')) { |
|
89
|
|
|
throw new UnexpectedValueException("Couldn't resolve a valid database connection instance for type '$name'"); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return new $class( |
|
93
|
|
|
$options['hostname'], |
|
94
|
|
|
$options['username'], |
|
95
|
|
|
$options['password'], |
|
96
|
|
|
$options['database'], |
|
97
|
|
|
$options['port'] |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |