DatabaseAdapterRegistry.php
2.32 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
<?php
/**
 * This class keeps track of the available database adapters
 * and provides a meaning of registering community built
 * adapters in to the installer process.
 *
 * @package framework
 * @author Tom Rix
 */
class DatabaseAdapterRegistry {
	
	private static $default_fields = array(
		'server' => array(
			'title' => 'Database server', 
			'envVar' => 'SS_DATABASE_SERVER', 
			'default' => 'localhost'
		),
		'username' => array(
			'title' => 'Database username', 
			'envVar' => 'SS_DATABASE_USERNAME', 
			'default' => 'root'
		),
		'password' => array(
			'title' => 'Database password', 
			'envVar' => 'SS_DATABASE_PASSWORD', 
			'default' => 'password'
		),
		'database' => array(
			'title' => 'Database name', 
			'default' => 'SS_mysite',
			'attributes' => array(
				"onchange" => "this.value = this.value.replace(/[\/\\:*?"<>|. \t]+/g,'');"
			)
		),
	);
	
	/**
	 * Internal array of registered database adapters
	 */
	private static $adapters = array();
	
	/**
	 * Add new adapter to the registry
	 * @param array $config Associative array of configuration details
	 */
	public static function register($config) {
		$missingExtensionText = isset($config['missingExtensionText'])
			? $config['missingExtensionText']
			: 'The PHP extension is missing, please enable or install it.';
		$path = explode('/', $config['helperPath']);
		$moduleName = array_shift($path);
		$missingModuleText = isset($config['missingModuleText'])
			? $config['missingModuleText']
			: 'The SilverStripe module, '.$moduleName.', is missing or incomplete.'
				. ' Please <a href="http://silverstripe.org/modules">download it</a>.';
		
		$config['missingModuleText'] = $missingModuleText;
		$config['missingExtensionText'] = $missingExtensionText;
		
		// set default fields if none are defined already
		if(!isset($config['fields'])) $config['fields'] = self::$default_fields;
		
		self::$adapters[$config['class']] = $config;
	}
	
	public static function unregister($class) {
		if(isset($adapters[$class])) unset($adapters[$class]);
	}
	
	public static function autodiscover() {
		foreach(glob(dirname(__FILE__) . '/../../../*', GLOB_ONLYDIR) as $directory) {
			if(file_exists($directory . '/_register_database.php')) {
				include_once($directory . '/_register_database.php');
			}
		}
	}
	
	public static function get_adapters() {
		return self::$adapters;
	}
}