|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Dev\Install; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* This class keeps track of the available database adapters |
|
9
|
|
|
* and provides a meaning of registering community built |
|
10
|
|
|
* adapters in to the installer process. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Tom Rix |
|
13
|
|
|
*/ |
|
14
|
|
|
class DatabaseAdapterRegistry { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Default database connector registration fields |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private static $default_fields = array( |
|
22
|
|
|
'server' => array( |
|
23
|
|
|
'title' => 'Database server', |
|
24
|
|
|
'envVar' => 'SS_DATABASE_SERVER', |
|
25
|
|
|
'default' => 'localhost' |
|
26
|
|
|
), |
|
27
|
|
|
'username' => array( |
|
28
|
|
|
'title' => 'Database username', |
|
29
|
|
|
'envVar' => 'SS_DATABASE_USERNAME', |
|
30
|
|
|
'default' => 'root' |
|
31
|
|
|
), |
|
32
|
|
|
'password' => array( |
|
33
|
|
|
'title' => 'Database password', |
|
34
|
|
|
'envVar' => 'SS_DATABASE_PASSWORD', |
|
35
|
|
|
'default' => 'password' |
|
36
|
|
|
), |
|
37
|
|
|
'database' => array( |
|
38
|
|
|
'title' => 'Database name', |
|
39
|
|
|
'default' => 'SS_mysite', |
|
40
|
|
|
'attributes' => array( |
|
41
|
|
|
"onchange" => "this.value = this.value.replace(/[\/\\:*?"<>|. \t]+/g,'');" |
|
42
|
|
|
) |
|
43
|
|
|
), |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Internal array of registered database adapters |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
private static $adapters = array(); |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Add new adapter to the registry |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $config Associative array of configuration details. This must include: |
|
57
|
|
|
* - title |
|
58
|
|
|
* - class |
|
59
|
|
|
* - helperClass |
|
60
|
|
|
* - supported |
|
61
|
|
|
* This SHOULD include: |
|
62
|
|
|
* - fields |
|
63
|
|
|
* - helperPath (if helperClass can't be autoloaded via psr-4/-0) |
|
64
|
|
|
* - missingExtensionText |
|
65
|
|
|
* - module OR missingModuleText |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function register($config) { |
|
68
|
|
|
// Validate config |
|
69
|
|
|
$missing = array_diff(['title', 'class', 'helperClass', 'supported'], array_keys($config)); |
|
70
|
|
|
if($missing) { |
|
|
|
|
|
|
71
|
|
|
throw new InvalidArgumentException( |
|
72
|
|
|
"Missing database helper config keys: '" . implode("', '", $missing) . "'" |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Guess missing module text if not given |
|
77
|
|
|
if (empty($config['missingModuleText'])) { |
|
78
|
|
|
if (empty($config['module'])) { |
|
79
|
|
|
$moduleText = 'Module for database connector ' . $config['title'] . 'is missing.'; |
|
80
|
|
|
} else { |
|
81
|
|
|
$moduleText = "The SilverStripe module '" . $config['module'] . "' is missing."; |
|
82
|
|
|
} |
|
83
|
|
|
$config['missingModuleText'] = $moduleText |
|
84
|
|
|
. ' Please install it via composer or from http://addons.silverstripe.org/.'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Set missing text |
|
88
|
|
|
if (empty($config['missingExtensionText'])) { |
|
89
|
|
|
$config['missingExtensionText'] = 'The PHP extension is missing, please enable or install it.'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// set default fields if none are defined already |
|
93
|
|
|
if(!isset($config['fields'])) { |
|
94
|
|
|
$config['fields'] = self::$default_fields; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
self::$adapters[$config['class']] = $config; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Unregisters a database connector by classname |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $class |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function unregister($class) { |
|
106
|
|
|
unset(self::$adapters[$class]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Detects all _register_database.php files and invokes them |
|
111
|
|
|
*/ |
|
112
|
|
View Code Duplication |
public static function autodiscover() { |
|
|
|
|
|
|
113
|
|
|
foreach(glob(dirname(__FILE__) . '/../../../*', GLOB_ONLYDIR) as $directory) { |
|
114
|
|
|
if(file_exists($directory . '/_register_database.php')) { |
|
115
|
|
|
include_once($directory . '/_register_database.php'); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Detects all _configure_database.php files and invokes them |
|
122
|
|
|
* Called by ConfigureFromEnv.php |
|
123
|
|
|
*/ |
|
124
|
|
View Code Duplication |
public static function autoconfigure() { |
|
|
|
|
|
|
125
|
|
|
foreach(glob(dirname(__FILE__) . '/../../../*', GLOB_ONLYDIR) as $directory) { |
|
126
|
|
|
if(file_exists($directory . '/_configure_database.php')) { |
|
127
|
|
|
include_once($directory . '/_configure_database.php'); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Return all registered adapters |
|
134
|
|
|
* |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function get_adapters() { |
|
138
|
|
|
return self::$adapters; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns registry data for a class |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $class |
|
145
|
|
|
* @return array List of adapter properties |
|
146
|
|
|
*/ |
|
147
|
|
|
public static function get_adapter($class) { |
|
148
|
|
|
if(isset(self::$adapters[$class])) { |
|
149
|
|
|
return self::$adapters[$class]; |
|
150
|
|
|
} |
|
151
|
|
|
return null; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Retrieves default field configuration |
|
156
|
|
|
* |
|
157
|
|
|
* @return array |
|
158
|
|
|
*/ |
|
159
|
|
|
public static function get_default_fields() { |
|
160
|
|
|
return self::$default_fields; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Build configuration helper for a given class |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $databaseClass Name of class |
|
167
|
|
|
* @return DatabaseConfigurationHelper|null Instance of helper, or null if cannot be loaded |
|
168
|
|
|
*/ |
|
169
|
|
|
public static function getDatabaseConfigurationHelper($databaseClass) { |
|
170
|
|
|
$adapters = static::get_adapters(); |
|
171
|
|
|
if(empty($adapters[$databaseClass]) || empty($adapters[$databaseClass]['helperClass'])) { |
|
172
|
|
|
return null; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// Load if path given |
|
176
|
|
|
if (isset($adapters[$databaseClass]['helperPath'])) { |
|
177
|
|
|
include_once $adapters[$databaseClass]['helperPath']; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
// Construct |
|
181
|
|
|
$class = $adapters[$databaseClass]['helperClass']; |
|
182
|
|
|
return (class_exists($class)) ? new $class() : null; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.