ConnectionManager.php 7.1 KB

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