2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-19 14:10:13 -07:00
|
|
|
* Datasource connection manager
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-19 14:10:13 -07:00
|
|
|
* Provides an interface for loading and enumerating connections defined in app/config/database.php
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 12:38:58 -04:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 17:46:59 +11:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 14:18:20 -05:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 14:18:20 -05:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 17:00:11 +11:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model
|
|
|
|
* @since CakePHP(tm) v 0.10.x.1402
|
2009-11-06 17:51:51 +11:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
|
2010-12-04 11:16:42 -04:30
|
|
|
App::uses('DataSource', 'Model/Datasource');
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Manages loaded instances of DataSource objects
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-05 23:08:25 -04:00
|
|
|
class ConnectionManager {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds a loaded instance of the Connections object
|
|
|
|
*
|
2009-03-19 14:10:13 -07:00
|
|
|
* @var DATABASE_CONFIG
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-12-06 22:47:43 -04:30
|
|
|
public static $config = null;
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds instances DataSource objects
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
*/
|
2010-12-06 22:47:43 -04:30
|
|
|
protected static $_dataSources = array();
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains a list of all file and class names used in Connection settings
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
*/
|
2010-12-06 22:47:43 -04:30
|
|
|
protected static $_connectionsEnum = array();
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-06 22:47:43 -04:30
|
|
|
* Indicates if the init code for this class has alredy been executed
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-12-06 22:47:43 -04:30
|
|
|
* @var boolean
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-06 22:47:43 -04:30
|
|
|
private static $_init = false;
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-06 22:47:43 -04:30
|
|
|
* Loads connections configuration.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
*/
|
2010-12-06 22:47:43 -04:30
|
|
|
private static function init() {
|
|
|
|
include_once CONFIGS . 'database.php';
|
|
|
|
if (class_exists('DATABASE_CONFIG')) {
|
|
|
|
self::$config = new DATABASE_CONFIG();
|
|
|
|
self::_getConnectionObjects();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-12-06 22:47:43 -04:30
|
|
|
register_shutdown_function('ConnectionManager::shutdown');
|
|
|
|
self::$_init = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets a reference to a DataSource object
|
|
|
|
*
|
2009-03-19 14:10:13 -07:00
|
|
|
* @param string $name The name of the DataSource, as defined in app/config/database.php
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return object Instance
|
|
|
|
*/
|
2010-12-06 22:47:43 -04:30
|
|
|
public static function getDataSource($name) {
|
|
|
|
if (empty(self::$_init)) {
|
|
|
|
self::init();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-12-06 22:47:43 -04:30
|
|
|
if (!empty(self::$_dataSources[$name])) {
|
|
|
|
$return = self::$_dataSources[$name];
|
2008-08-07 15:36:26 +00:00
|
|
|
return $return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-09-24 22:56:02 -04:00
|
|
|
|
2010-12-06 22:47:43 -04:30
|
|
|
if (empty(self::$_connectionsEnum[$name])) {
|
2010-12-04 23:37:13 -02:00
|
|
|
trigger_error(__("ConnectionManager::getDataSource - Non-existent data source %s", $name), E_USER_ERROR);
|
2009-09-24 19:29:35 -04:00
|
|
|
$null = null;
|
|
|
|
return $null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-12-06 22:47:43 -04:30
|
|
|
$conn = self::$_connectionsEnum[$name];
|
2009-09-24 23:48:52 -04:00
|
|
|
$class = $conn['classname'];
|
2009-09-24 22:56:02 -04:00
|
|
|
|
2010-12-06 22:47:43 -04:30
|
|
|
if (self::loadDataSource($name) === null) {
|
2010-12-04 23:37:13 -02:00
|
|
|
trigger_error(__("ConnectionManager::getDataSource - Could not load class %s", $class), E_USER_ERROR);
|
2009-09-24 22:56:02 -04:00
|
|
|
$null = null;
|
|
|
|
return $null;
|
|
|
|
}
|
2010-12-06 22:47:43 -04:30
|
|
|
self::$_dataSources[$name] = new $class(self::$config->{$name});
|
|
|
|
self::$_dataSources[$name]->configKeyName = $name;
|
2009-09-24 22:56:02 -04:00
|
|
|
|
2010-12-06 22:47:43 -04:30
|
|
|
return self::$_dataSources[$name];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the list of available DataSource connections
|
|
|
|
*
|
|
|
|
* @return array List of available connections
|
|
|
|
*/
|
2010-07-05 23:08:25 -04:00
|
|
|
public static function sourceList() {
|
2010-12-06 22:47:43 -04:30
|
|
|
if (empty(self::$_init)) {
|
|
|
|
self::init();
|
|
|
|
}
|
|
|
|
return array_keys(self::$_dataSources);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-21 13:18:00 -05:00
|
|
|
* Gets a DataSource name from an object reference.
|
|
|
|
*
|
|
|
|
* **Warning** this method may cause fatal errors in PHP4.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param object $source DataSource object
|
2009-09-24 23:48:52 -04:00
|
|
|
* @return string Datasource name, or null if source is not present
|
2010-01-21 13:18:00 -05:00
|
|
|
* in the ConnectionManager.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-05 23:08:25 -04:00
|
|
|
public static function getSourceName(&$source) {
|
2010-12-06 22:47:43 -04:30
|
|
|
if (empty(self::$_init)) {
|
|
|
|
self::init();
|
|
|
|
}
|
|
|
|
foreach (self::$_dataSources as $name => $ds) {
|
2010-01-11 09:48:53 -05:00
|
|
|
if ($ds == $source) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Loads the DataSource class for the given connection name
|
|
|
|
*
|
2009-03-19 14:10:13 -07:00
|
|
|
* @param mixed $connName A string name of the connection, as defined in app/config/database.php,
|
|
|
|
* or an array containing the filename (without extension) and class name of the object,
|
|
|
|
* to be found in app/models/datasources/ or cake/libs/model/datasources/.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean True on success, null on failure or false if the class is already loaded
|
|
|
|
*/
|
2010-07-05 23:08:25 -04:00
|
|
|
public static function loadDataSource($connName) {
|
2010-12-06 22:47:43 -04:30
|
|
|
if (empty(self::$_init)) {
|
|
|
|
self::init();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (is_array($connName)) {
|
|
|
|
$conn = $connName;
|
|
|
|
} else {
|
2010-12-06 22:47:43 -04:30
|
|
|
$conn = self::$_connectionsEnum[$connName];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-12-04 12:24:15 -04:30
|
|
|
if (class_exists($conn['classname'], false)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-04 12:24:15 -04:30
|
|
|
$plugin = $package = null;
|
|
|
|
if (!empty($conn['plugin'])) {
|
|
|
|
$plugin .= '.';
|
|
|
|
}
|
|
|
|
if (!empty($conn['package'])) {
|
|
|
|
$package = '/' . $conn['package'];
|
|
|
|
}
|
2009-09-24 22:56:02 -04:00
|
|
|
|
2010-12-04 12:24:15 -04:30
|
|
|
App::uses($conn['classname'], $plugin . 'Model/Datasource' . $package);
|
|
|
|
if (!class_exists($conn['classname'])) {
|
2010-12-04 23:37:13 -02:00
|
|
|
trigger_error(__('ConnectionManager::loadDataSource - Unable to import DataSource class %s', $class), E_USER_ERROR);
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2009-09-24 16:23:24 -04:00
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-16 15:03:13 -02:00
|
|
|
* Return a list of connections
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return array An associative array of elements where the key is the connection name
|
|
|
|
* (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
|
|
|
|
*/
|
2010-07-05 23:08:25 -04:00
|
|
|
public static function enumConnectionObjects() {
|
2010-12-06 22:47:43 -04:30
|
|
|
if (empty(self::$_init)) {
|
|
|
|
self::init();
|
|
|
|
}
|
|
|
|
return self::$_connectionsEnum;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Dynamically creates a DataSource object at runtime, with the given name and settings
|
|
|
|
*
|
|
|
|
* @param string $name The DataSource name
|
|
|
|
* @param array $config The DataSource configuration settings
|
|
|
|
* @return object A reference to the DataSource object, or null if creation failed
|
|
|
|
*/
|
2010-12-06 22:47:43 -04:30
|
|
|
public static function create($name = '', $config = array()) {
|
|
|
|
if (empty(self::$_init)) {
|
|
|
|
self::init();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-12-06 22:47:43 -04:30
|
|
|
if (empty($name) || empty($config) || array_key_exists($name, self::$_connectionsEnum)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$null = null;
|
|
|
|
return $null;
|
|
|
|
}
|
2010-12-06 22:47:43 -04:30
|
|
|
self::$config->{$name} = $config;
|
|
|
|
self::$_connectionsEnum[$name] = self::_connectionData($config);
|
|
|
|
$return = self::getDataSource($name);
|
2008-08-07 15:36:26 +00:00
|
|
|
return $return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2010-01-16 15:03:13 -02:00
|
|
|
/**
|
|
|
|
* Gets a list of class and file names associated with the user-defined DataSource connections
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-12-06 22:47:43 -04:30
|
|
|
protected static function _getConnectionObjects() {
|
|
|
|
$connections = get_object_vars(self::$config);
|
2010-01-16 15:03:13 -02:00
|
|
|
|
|
|
|
if ($connections != null) {
|
|
|
|
foreach ($connections as $name => $config) {
|
2010-12-06 22:47:43 -04:30
|
|
|
self::$_connectionsEnum[$name] = self::_connectionData($config);
|
2010-01-16 15:03:13 -02:00
|
|
|
}
|
|
|
|
} else {
|
2010-08-29 21:37:25 -04:00
|
|
|
throw new MissingConnectionException(array('class' => 'ConnectionManager'));
|
2010-01-16 15:03:13 -02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the file, class name, and parent for the given driver.
|
|
|
|
*
|
2009-09-25 01:02:41 -04:00
|
|
|
* @return array An indexed array with: filename, classname, plugin and parent
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-06 22:47:43 -04:30
|
|
|
private static function _connectionData($config) {
|
2010-12-04 12:24:15 -04:30
|
|
|
$package = $classname = $plugin = null;
|
2010-02-16 22:35:16 -05:00
|
|
|
|
2010-12-04 12:24:15 -04:30
|
|
|
list($plugin, $classname) = pluginSplit($config['datasource']);
|
|
|
|
if (strpos($classname, '/') !== false) {
|
|
|
|
$package = dirname($classname);
|
|
|
|
$classname = basename($classname);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-12-04 12:24:15 -04:30
|
|
|
return compact('package', 'classname', 'plugin');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*
|
|
|
|
*/
|
2010-12-06 22:47:43 -04:30
|
|
|
public static function shutdown() {
|
2010-09-06 01:10:45 -04:00
|
|
|
if (Configure::read('Session.defaults') == 'database' && function_exists('session_write_close')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
session_write_close();
|
|
|
|
}
|
|
|
|
}
|
2010-08-28 00:08:35 -04:00
|
|
|
}
|