2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-19 21:10:13 +00:00
|
|
|
* Datasource connection manager
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-06-20 00:28:40 +00:00
|
|
|
* Provides an interface for loading and enumerating connections defined in app/Config/database.php
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.x.1402
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
|
2010-12-04 15:46:42 +00:00
|
|
|
App::uses('DataSource', 'Model/Datasource');
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Manages loaded instances of DataSource objects
|
|
|
|
*
|
2012-07-18 01:55:29 +00:00
|
|
|
* Provides an interface for loading and enumerating connections defined in
|
2011-12-08 15:35:02 +00:00
|
|
|
* app/Config/database.php
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
class ConnectionManager {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds a loaded instance of the Connections object
|
|
|
|
*
|
2009-03-19 21:10:13 +00:00
|
|
|
* @var DATABASE_CONFIG
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-07 03:17:43 +00:00
|
|
|
public static $config = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds instances DataSource objects
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-12-07 03:17:43 +00:00
|
|
|
protected static $_dataSources = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains a list of all file and class names used in Connection settings
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-12-07 03:17:43 +00:00
|
|
|
protected static $_connectionsEnum = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-05-27 06:00:42 +00:00
|
|
|
* Indicates if the init code for this class has already been executed
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected static $_init = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-07 03:17:43 +00:00
|
|
|
* Loads connections configuration.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-30 22:38:57 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected static function _init() {
|
2011-04-17 11:13:02 +00:00
|
|
|
include_once APP . 'Config' . DS . 'database.php';
|
2010-12-07 03:17:43 +00:00
|
|
|
if (class_exists('DATABASE_CONFIG')) {
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$config = new DATABASE_CONFIG();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$_init = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets a reference to a DataSource object
|
|
|
|
*
|
2011-06-20 00:28:40 +00:00
|
|
|
* @param string $name The name of the DataSource, as defined in app/Config/database.php
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return DataSource Instance
|
2011-10-15 18:06:31 +00:00
|
|
|
* @throws MissingDatasourceException
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-07 03:17:43 +00:00
|
|
|
public static function getDataSource($name) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (empty(static::$_init)) {
|
|
|
|
static::_init();
|
2010-12-07 03:17:43 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
if (!empty(static::$_dataSources[$name])) {
|
|
|
|
return static::$_dataSources[$name];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-09-25 02:56:02 +00:00
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
if (empty(static::$_connectionsEnum[$name])) {
|
|
|
|
static::_getConnectionObject($name);
|
2010-12-08 00:29:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
static::loadDataSource($name);
|
|
|
|
$conn = static::$_connectionsEnum[$name];
|
2009-09-25 03:48:52 +00:00
|
|
|
$class = $conn['classname'];
|
2009-09-25 02:56:02 +00:00
|
|
|
|
2013-03-13 16:52:47 +00:00
|
|
|
if (strpos(App::location($class), 'Datasource') === false) {
|
2013-03-13 01:56:08 +00:00
|
|
|
throw new MissingDatasourceException(array(
|
|
|
|
'class' => $class,
|
|
|
|
'plugin' => null,
|
2013-03-13 16:52:47 +00:00
|
|
|
'message' => 'Datasource is not found in Model/Datasource package.'
|
2013-03-13 01:56:08 +00:00
|
|
|
));
|
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$_dataSources[$name] = new $class(static::$config->{$name});
|
|
|
|
static::$_dataSources[$name]->configKeyName = $name;
|
2013-03-13 16:52:47 +00:00
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::$_dataSources[$name];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the list of available DataSource connections
|
2011-05-27 06:58:38 +00:00
|
|
|
* This will only return the datasources instantiated by this manager
|
|
|
|
* It differs from enumConnectionObjects, since the latter will return all configured connections
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return array List of available connections
|
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
public static function sourceList() {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (empty(static::$_init)) {
|
|
|
|
static::_init();
|
2010-12-07 03:17:43 +00:00
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
return array_keys(static::$_dataSources);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-21 18:18:00 +00:00
|
|
|
* Gets a DataSource name from an object reference.
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param DataSource $source DataSource object
|
2014-11-05 12:03:27 +00:00
|
|
|
* @return string|null Datasource name, or null if source is not present
|
2010-01-21 18:18:00 +00:00
|
|
|
* in the ConnectionManager.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-06 02:45:17 +00:00
|
|
|
public static function getSourceName($source) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (empty(static::$_init)) {
|
|
|
|
static::_init();
|
2010-12-07 03:17:43 +00:00
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
foreach (static::$_dataSources as $name => $ds) {
|
2011-01-06 02:45:17 +00:00
|
|
|
if ($ds === $source) {
|
2010-01-11 14:48:53 +00:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
2011-05-27 06:43:35 +00:00
|
|
|
return null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Loads the DataSource class for the given connection name
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $connName A string name of the connection, as defined in app/Config/database.php,
|
2014-11-05 12:03:27 +00:00
|
|
|
* or an array containing the filename (without extension) and class name of the object,
|
|
|
|
* to be found in app/Model/Datasource/ or lib/Cake/Model/Datasource/.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True on success, null on failure or false if the class is already loaded
|
2011-10-15 18:06:31 +00:00
|
|
|
* @throws MissingDatasourceException
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
public static function loadDataSource($connName) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (empty(static::$_init)) {
|
|
|
|
static::_init();
|
2010-12-07 03:17:43 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (is_array($connName)) {
|
|
|
|
$conn = $connName;
|
|
|
|
} else {
|
2015-07-21 08:22:53 +00:00
|
|
|
$conn = static::$_connectionsEnum[$connName];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-12-04 16:54:15 +00:00
|
|
|
if (class_exists($conn['classname'], false)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-04 16:54:15 +00:00
|
|
|
$plugin = $package = null;
|
|
|
|
if (!empty($conn['plugin'])) {
|
2011-01-06 02:45:17 +00:00
|
|
|
$plugin = $conn['plugin'] . '.';
|
2010-12-04 16:54:15 +00:00
|
|
|
}
|
|
|
|
if (!empty($conn['package'])) {
|
|
|
|
$package = '/' . $conn['package'];
|
|
|
|
}
|
2009-09-25 02:56:02 +00:00
|
|
|
|
2010-12-04 16:54:15 +00:00
|
|
|
App::uses($conn['classname'], $plugin . 'Model/Datasource' . $package);
|
|
|
|
if (!class_exists($conn['classname'])) {
|
2011-10-15 18:06:31 +00:00
|
|
|
throw new MissingDatasourceException(array(
|
|
|
|
'class' => $conn['classname'],
|
2011-10-16 11:40:36 +00:00
|
|
|
'plugin' => substr($plugin, 0, -1)
|
2011-10-15 18:06:31 +00:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-09-24 20:23:24 +00:00
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2014-11-05 12:03:27 +00:00
|
|
|
* Returns 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
|
2014-11-05 12:03:27 +00:00
|
|
|
* (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
public static function enumConnectionObjects() {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (empty(static::$_init)) {
|
|
|
|
static::_init();
|
2010-12-07 03:17:43 +00:00
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
return (array)static::$config;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00: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
|
2014-11-05 12:03:27 +00:00
|
|
|
* @return DataSource|null A reference to the DataSource object, or null if creation failed
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-07 03:17:43 +00:00
|
|
|
public static function create($name = '', $config = array()) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (empty(static::$_init)) {
|
|
|
|
static::_init();
|
2010-12-07 03:17:43 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
if (empty($name) || empty($config) || array_key_exists($name, static::$_connectionsEnum)) {
|
2011-04-17 03:27:05 +00:00
|
|
|
return null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$config->{$name} = $config;
|
|
|
|
static::$_connectionsEnum[$name] = static::_connectionData($config);
|
|
|
|
$return = static::getDataSource($name);
|
2008-08-07 15:36:26 +00:00
|
|
|
return $return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-04-17 03:27:05 +00:00
|
|
|
/**
|
|
|
|
* Removes a connection configuration at runtime given its name
|
|
|
|
*
|
|
|
|
* @param string $name the connection name as it was created
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool success if connection was removed, false if it does not exist
|
2011-04-17 03:27:05 +00:00
|
|
|
*/
|
|
|
|
public static function drop($name) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (empty(static::$_init)) {
|
|
|
|
static::_init();
|
2011-04-17 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
if (!isset(static::$config->{$name})) {
|
2011-04-17 03:27:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
unset(static::$_connectionsEnum[$name], static::$_dataSources[$name], static::$config->{$name});
|
2011-04-17 03:27:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-16 17:03:13 +00:00
|
|
|
/**
|
|
|
|
* Gets a list of class and file names associated with the user-defined DataSource connections
|
|
|
|
*
|
2011-07-30 22:38:57 +00:00
|
|
|
* @param string $name Connection name
|
2010-01-16 17:03:13 +00:00
|
|
|
* @return void
|
2011-07-30 22:38:57 +00:00
|
|
|
* @throws MissingDatasourceConfigException
|
2010-01-16 17:03:13 +00:00
|
|
|
*/
|
2010-12-08 00:29:07 +00:00
|
|
|
protected static function _getConnectionObject($name) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (!empty(static::$config->{$name})) {
|
|
|
|
static::$_connectionsEnum[$name] = static::_connectionData(static::$config->{$name});
|
2010-01-16 17:03:13 +00:00
|
|
|
} else {
|
2011-01-22 05:41:22 +00:00
|
|
|
throw new MissingDatasourceConfigException(array('config' => $name));
|
2010-01-16 17:03:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the file, class name, and parent for the given driver.
|
|
|
|
*
|
2011-07-30 22:38:57 +00:00
|
|
|
* @param array $config Array with connection configuration. Key 'datasource' is required
|
2009-09-25 05:02:41 +00:00
|
|
|
* @return array An indexed array with: filename, classname, plugin and parent
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected static function _connectionData($config) {
|
2010-12-04 16:54:15 +00:00
|
|
|
$package = $classname = $plugin = null;
|
2010-02-17 03:35:16 +00:00
|
|
|
|
2010-12-04 16:54:15 +00:00
|
|
|
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 16:54:15 +00:00
|
|
|
return compact('package', 'classname', 'plugin');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-03-04 19:18:04 +00:00
|
|
|
|
2011-04-17 11:13:02 +00:00
|
|
|
}
|