2010-05-25 06:24:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class keeps track of the available database adapters
|
|
|
|
* and provides a meaning of registering community built
|
|
|
|
* adapters in to the installer process.
|
|
|
|
*
|
2012-06-20 23:59:16 +02:00
|
|
|
* @package framework
|
2010-05-25 06:24:14 +02:00
|
|
|
* @author Tom Rix
|
|
|
|
*/
|
|
|
|
class DatabaseAdapterRegistry {
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default database connector registration fields
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_fields = array(
|
2010-10-04 06:43:12 +02:00
|
|
|
'server' => array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'title' => 'Database server',
|
|
|
|
'envVar' => 'SS_DATABASE_SERVER',
|
2010-10-04 06:43:12 +02:00
|
|
|
'default' => 'localhost'
|
|
|
|
),
|
|
|
|
'username' => array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'title' => 'Database username',
|
|
|
|
'envVar' => 'SS_DATABASE_USERNAME',
|
2010-10-04 06:43:12 +02:00
|
|
|
'default' => 'root'
|
|
|
|
),
|
|
|
|
'password' => array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'title' => 'Database password',
|
|
|
|
'envVar' => 'SS_DATABASE_PASSWORD',
|
2010-10-04 06:43:12 +02:00
|
|
|
'default' => 'password'
|
|
|
|
),
|
|
|
|
'database' => array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'title' => 'Database name',
|
2010-10-04 06:43:12 +02:00
|
|
|
'default' => 'SS_mysite',
|
|
|
|
'attributes' => array(
|
|
|
|
"onchange" => "this.value = this.value.replace(/[\/\\:*?"<>|. \t]+/g,'');"
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2010-05-25 06:24:14 +02:00
|
|
|
/**
|
|
|
|
* Internal array of registered database adapters
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2010-05-25 06:24:14 +02:00
|
|
|
*/
|
|
|
|
private static $adapters = array();
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2010-05-25 06:24:14 +02:00
|
|
|
/**
|
|
|
|
* Add new adapter to the registry
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-04 06:16:16 +02:00
|
|
|
* @param array $config Associative array of configuration details
|
2010-05-25 06:24:14 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function register($config) {
|
2010-10-04 06:16:16 +02:00
|
|
|
$missingExtensionText = isset($config['missingExtensionText'])
|
|
|
|
? $config['missingExtensionText']
|
|
|
|
: 'The PHP extension is missing, please enable or install it.';
|
|
|
|
|
2010-12-05 09:39:31 +01:00
|
|
|
$path = explode('/', $config['helperPath']);
|
|
|
|
$moduleName = array_shift($path);
|
2010-10-04 06:16:16 +02:00
|
|
|
$missingModuleText = isset($config['missingModuleText'])
|
|
|
|
? $config['missingModuleText']
|
2012-09-26 23:34:00 +02:00
|
|
|
: 'The SilverStripe module, '.$moduleName.', is missing or incomplete.'
|
|
|
|
. ' Please <a href="http://silverstripe.org/modules">download it</a>.';
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2010-10-04 06:16:16 +02:00
|
|
|
$config['missingModuleText'] = $missingModuleText;
|
|
|
|
$config['missingExtensionText'] = $missingExtensionText;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2010-10-04 06:43:12 +02:00
|
|
|
// set default fields if none are defined already
|
|
|
|
if(!isset($config['fields'])) $config['fields'] = self::$default_fields;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2010-10-04 06:16:16 +02:00
|
|
|
self::$adapters[$config['class']] = $config;
|
2010-05-25 06:24:14 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregisters a database connector by classname
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function unregister($class) {
|
2013-06-21 00:32:08 +02:00
|
|
|
unset(self::$adapters[$class]);
|
2010-10-04 06:17:01 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Detects all _register_database.php files and invokes them
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function autodiscover() {
|
2010-10-04 06:16:16 +02:00
|
|
|
foreach(glob(dirname(__FILE__) . '/../../../*', GLOB_ONLYDIR) as $directory) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(file_exists($directory . '/_register_database.php')) {
|
|
|
|
include_once($directory . '/_register_database.php');
|
|
|
|
}
|
2010-05-25 06:24:14 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Detects all _configure_database.php files and invokes them
|
|
|
|
* Called by ConfigureFromEnv.php
|
|
|
|
*/
|
|
|
|
public static function autoconfigure() {
|
|
|
|
foreach(glob(dirname(__FILE__) . '/../../../*', GLOB_ONLYDIR) as $directory) {
|
|
|
|
if(file_exists($directory . '/_configure_database.php')) {
|
|
|
|
include_once($directory . '/_configure_database.php');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all registered adapters
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_adapters() {
|
2010-05-25 06:24:14 +02:00
|
|
|
return self::$adapters;
|
|
|
|
}
|
2010-10-04 06:16:16 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Returns registry data for a class
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @return array List of adapter properties
|
|
|
|
*/
|
|
|
|
public static function get_adapter($class) {
|
|
|
|
if(isset(self::$adapters[$class])) return self::$adapters[$class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves default field configuration
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-07-11 05:23:44 +02:00
|
|
|
public static function get_default_fields() {
|
2013-06-21 00:32:08 +02:00
|
|
|
return self::$default_fields;
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|