ConnectionManager.php 7.3 KB

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