ConnectionManager.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * Datasource connection manager
  4. *
  5. * Provides an interface for loading and enumerating connections defined in app/config/database.php
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake.libs.model
  18. * @since CakePHP(tm) v 0.10.x.1402
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('DataSource', 'Model/Datasource');
  22. config('database');
  23. /**
  24. * Manages loaded instances of DataSource objects
  25. *
  26. * @package cake.libs.model
  27. */
  28. class ConnectionManager {
  29. /**
  30. * Holds a loaded instance of the Connections object
  31. *
  32. * @var DATABASE_CONFIG
  33. * @access public
  34. */
  35. public static $config = null;
  36. /**
  37. * Holds instances DataSource objects
  38. *
  39. * @var array
  40. * @access protected
  41. */
  42. protected static $_dataSources = array();
  43. /**
  44. * Contains a list of all file and class names used in Connection settings
  45. *
  46. * @var array
  47. * @access protected
  48. */
  49. protected static $_connectionsEnum = array();
  50. /**
  51. * Indicates if the init code for this class has alredy been executed
  52. *
  53. * @var boolean
  54. */
  55. private static $_init = false;
  56. /**
  57. * Loads connections configuration.
  58. *
  59. */
  60. private static function init() {
  61. include_once APP . 'Config' . DS . 'database.php';
  62. if (class_exists('DATABASE_CONFIG')) {
  63. self::$config = new DATABASE_CONFIG();
  64. }
  65. register_shutdown_function('ConnectionManager::shutdown');
  66. self::$_init = true;
  67. }
  68. /**
  69. * Gets a reference to a DataSource object
  70. *
  71. * @param string $name The name of the DataSource, as defined in app/config/database.php
  72. * @return object Instance
  73. * @throws MissingDatasourceConfigException
  74. * @throws MissingDatasourceFileException
  75. */
  76. public static function getDataSource($name) {
  77. if (empty(self::$_init)) {
  78. self::init();
  79. }
  80. if (!empty(self::$_dataSources[$name])) {
  81. $return = self::$_dataSources[$name];
  82. return $return;
  83. }
  84. if (empty(self::$_connectionsEnum[$name])) {
  85. self::_getConnectionObject($name);
  86. }
  87. self::loadDataSource($name);
  88. $conn = self::$_connectionsEnum[$name];
  89. $class = $conn['classname'];
  90. self::$_dataSources[$name] = new $class(self::$config->{$name});
  91. self::$_dataSources[$name]->configKeyName = $name;
  92. return self::$_dataSources[$name];
  93. }
  94. /**
  95. * Gets the list of available DataSource connections
  96. *
  97. * @return array List of available connections
  98. */
  99. public static function sourceList() {
  100. if (empty(self::$_init)) {
  101. self::init();
  102. }
  103. return array_keys(self::$_dataSources);
  104. }
  105. /**
  106. * Gets a DataSource name from an object reference.
  107. *
  108. * @param object $source DataSource object
  109. * @return string Datasource name, or null if source is not present
  110. * in the ConnectionManager.
  111. */
  112. public static function getSourceName($source) {
  113. if (empty(self::$_init)) {
  114. self::init();
  115. }
  116. foreach (self::$_dataSources as $name => $ds) {
  117. if ($ds === $source) {
  118. return $name;
  119. }
  120. }
  121. return '';
  122. }
  123. /**
  124. * Loads the DataSource class for the given connection name
  125. *
  126. * @param mixed $connName A string name of the connection, as defined in app/config/database.php,
  127. * or an array containing the filename (without extension) and class name of the object,
  128. * to be found in app/models/datasources/ or cake/libs/model/datasources/.
  129. * @return boolean True on success, null on failure or false if the class is already loaded
  130. * @throws MissingDatasourceFileException
  131. */
  132. public static function loadDataSource($connName) {
  133. if (empty(self::$_init)) {
  134. self::init();
  135. }
  136. if (is_array($connName)) {
  137. $conn = $connName;
  138. } else {
  139. $conn = self::$_connectionsEnum[$connName];
  140. }
  141. if (class_exists($conn['classname'], false)) {
  142. return false;
  143. }
  144. $plugin = $package = null;
  145. if (!empty($conn['plugin'])) {
  146. $plugin = $conn['plugin'] . '.';
  147. }
  148. if (!empty($conn['package'])) {
  149. $package = '/' . $conn['package'];
  150. }
  151. App::uses($conn['classname'], $plugin . 'Model/Datasource' . $package);
  152. if (!class_exists($conn['classname'])) {
  153. throw new MissingDatasourceFileException(array('class' => $conn['classname'], 'plugin' => $plugin));
  154. }
  155. return true;
  156. }
  157. /**
  158. * Return a list of connections
  159. *
  160. * @return array An associative array of elements where the key is the connection name
  161. * (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
  162. */
  163. public static function enumConnectionObjects() {
  164. if (empty(self::$_init)) {
  165. self::init();
  166. }
  167. return (array) self::$config;
  168. }
  169. /**
  170. * Dynamically creates a DataSource object at runtime, with the given name and settings
  171. *
  172. * @param string $name The DataSource name
  173. * @param array $config The DataSource configuration settings
  174. * @return object A reference to the DataSource object, or null if creation failed
  175. */
  176. public static function create($name = '', $config = array()) {
  177. if (empty(self::$_init)) {
  178. self::init();
  179. }
  180. if (empty($name) || empty($config) || array_key_exists($name, self::$_connectionsEnum)) {
  181. return null;
  182. }
  183. self::$config->{$name} = $config;
  184. self::$_connectionsEnum[$name] = self::_connectionData($config);
  185. $return = self::getDataSource($name);
  186. return $return;
  187. }
  188. /**
  189. * Removes a connection configuration at runtime given its name
  190. *
  191. * @param string $name the connection name as it was created
  192. * @return boolean success if connection was removed, false if it does not exist
  193. */
  194. public static function drop($name) {
  195. if (empty(self::$_init)) {
  196. self::init();
  197. }
  198. if (!isset(self::$config->{$name})) {
  199. return false;
  200. }
  201. unset(self::$_connectionsEnum[$name], self::$_dataSources[$name], self::$config->{$name});
  202. return true;
  203. }
  204. /**
  205. * Gets a list of class and file names associated with the user-defined DataSource connections
  206. *
  207. * @return void
  208. */
  209. protected static function _getConnectionObject($name) {
  210. if (!empty(self::$config->{$name})) {
  211. self::$_connectionsEnum[$name] = self::_connectionData(self::$config->{$name});
  212. } else {
  213. throw new MissingDatasourceConfigException(array('config' => $name));
  214. }
  215. }
  216. /**
  217. * Returns the file, class name, and parent for the given driver.
  218. *
  219. * @return array An indexed array with: filename, classname, plugin and parent
  220. */
  221. private static function _connectionData($config) {
  222. $package = $classname = $plugin = null;
  223. list($plugin, $classname) = pluginSplit($config['datasource']);
  224. if (strpos($classname, '/') !== false) {
  225. $package = dirname($classname);
  226. $classname = basename($classname);
  227. }
  228. return compact('package', 'classname', 'plugin');
  229. }
  230. /**
  231. * Destructor.
  232. *
  233. */
  234. public static function shutdown() {
  235. if (Configure::read('Session.defaults') == 'database' && function_exists('session_write_close')) {
  236. session_write_close();
  237. }
  238. }
  239. }