ConnectionManager.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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
  18. * @subpackage cake.cake.libs.model
  19. * @since CakePHP(tm) v 0.10.x.1402
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('DataSource', 'Model/Datasource');
  23. /**
  24. * Manages loaded instances of DataSource objects
  25. *
  26. * @package cake
  27. * @subpackage cake.cake.libs.model
  28. */
  29. class ConnectionManager {
  30. /**
  31. * Holds a loaded instance of the Connections object
  32. *
  33. * @var DATABASE_CONFIG
  34. * @access public
  35. */
  36. public static $config = null;
  37. /**
  38. * Holds instances DataSource objects
  39. *
  40. * @var array
  41. * @access protected
  42. */
  43. protected static $_dataSources = array();
  44. /**
  45. * Contains a list of all file and class names used in Connection settings
  46. *
  47. * @var array
  48. * @access protected
  49. */
  50. protected static $_connectionsEnum = array();
  51. /**
  52. * Indicates if the init code for this class has alredy been executed
  53. *
  54. * @var boolean
  55. */
  56. private static $_init = false;
  57. /**
  58. * Loads connections configuration.
  59. *
  60. */
  61. private static function init() {
  62. include_once CONFIGS . 'database.php';
  63. if (class_exists('DATABASE_CONFIG')) {
  64. self::$config = new DATABASE_CONFIG();
  65. }
  66. register_shutdown_function('ConnectionManager::shutdown');
  67. self::$_init = true;
  68. }
  69. /**
  70. * Gets a reference to a DataSource object
  71. *
  72. * @param string $name The name of the DataSource, as defined in app/config/database.php
  73. * @return object Instance
  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. if (empty(self::$_connectionsEnum[$name])) {
  87. trigger_error(__("ConnectionManager::getDataSource - Non-existent data source %s", $name), E_USER_ERROR);
  88. $null = null;
  89. return $null;
  90. }
  91. $conn = self::$_connectionsEnum[$name];
  92. $class = $conn['classname'];
  93. if (self::loadDataSource($name) === null) {
  94. trigger_error(__("ConnectionManager::getDataSource - Could not load class %s", $class), E_USER_ERROR);
  95. $null = null;
  96. return $null;
  97. }
  98. self::$_dataSources[$name] = new $class(self::$config->{$name});
  99. self::$_dataSources[$name]->configKeyName = $name;
  100. return self::$_dataSources[$name];
  101. }
  102. /**
  103. * Gets the list of available DataSource connections
  104. *
  105. * @return array List of available connections
  106. */
  107. public static function sourceList() {
  108. if (empty(self::$_init)) {
  109. self::init();
  110. }
  111. return array_keys(self::$_dataSources);
  112. }
  113. /**
  114. * Gets a DataSource name from an object reference.
  115. *
  116. * **Warning** this method may cause fatal errors in PHP4.
  117. *
  118. * @param object $source DataSource object
  119. * @return string Datasource name, or null if source is not present
  120. * in the ConnectionManager.
  121. */
  122. public static function getSourceName(&$source) {
  123. if (empty(self::$_init)) {
  124. self::init();
  125. }
  126. foreach (self::$_dataSources as $name => $ds) {
  127. if ($ds == $source) {
  128. return $name;
  129. }
  130. }
  131. return '';
  132. }
  133. /**
  134. * Loads the DataSource class for the given connection name
  135. *
  136. * @param mixed $connName A string name of the connection, as defined in app/config/database.php,
  137. * or an array containing the filename (without extension) and class name of the object,
  138. * to be found in app/models/datasources/ or cake/libs/model/datasources/.
  139. * @return boolean True on success, null on failure or false if the class is already loaded
  140. */
  141. public static function loadDataSource($connName) {
  142. if (empty(self::$_init)) {
  143. self::init();
  144. }
  145. if (is_array($connName)) {
  146. $conn = $connName;
  147. } else {
  148. $conn = self::$_connectionsEnum[$connName];
  149. }
  150. if (class_exists($conn['classname'], false)) {
  151. return false;
  152. }
  153. $plugin = $package = null;
  154. if (!empty($conn['plugin'])) {
  155. $plugin .= '.';
  156. }
  157. if (!empty($conn['package'])) {
  158. $package = '/' . $conn['package'];
  159. }
  160. App::uses($conn['classname'], $plugin . 'Model/Datasource' . $package);
  161. if (!class_exists($conn['classname'])) {
  162. trigger_error(__('ConnectionManager::loadDataSource - Unable to import DataSource class %s', $class), E_USER_ERROR);
  163. return null;
  164. }
  165. return true;
  166. }
  167. /**
  168. * Return a list of connections
  169. *
  170. * @return array An associative array of elements where the key is the connection name
  171. * (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
  172. */
  173. public static function enumConnectionObjects() {
  174. if (empty(self::$_init)) {
  175. self::init();
  176. }
  177. return self::$_connectionsEnum;
  178. }
  179. /**
  180. * Dynamically creates a DataSource object at runtime, with the given name and settings
  181. *
  182. * @param string $name The DataSource name
  183. * @param array $config The DataSource configuration settings
  184. * @return object A reference to the DataSource object, or null if creation failed
  185. */
  186. public static function create($name = '', $config = array()) {
  187. if (empty(self::$_init)) {
  188. self::init();
  189. }
  190. if (empty($name) || empty($config) || array_key_exists($name, self::$_connectionsEnum)) {
  191. $null = null;
  192. return $null;
  193. }
  194. self::$config->{$name} = $config;
  195. self::$_connectionsEnum[$name] = self::_connectionData($config);
  196. $return = self::getDataSource($name);
  197. return $return;
  198. }
  199. /**
  200. * Gets a list of class and file names associated with the user-defined DataSource connections
  201. *
  202. * @return void
  203. */
  204. protected static function _getConnectionObject($name) {
  205. if (!empty(self::$config->{$name})) {
  206. self::$_connectionsEnum[$name] = self::_connectionData(self::$config->{$name});
  207. } else {
  208. throw new MissingConnectionException(array('class' => 'ConnectionManager'));
  209. }
  210. }
  211. /**
  212. * Returns the file, class name, and parent for the given driver.
  213. *
  214. * @return array An indexed array with: filename, classname, plugin and parent
  215. */
  216. private static function _connectionData($config) {
  217. $package = $classname = $plugin = null;
  218. list($plugin, $classname) = pluginSplit($config['datasource']);
  219. if (strpos($classname, '/') !== false) {
  220. $package = dirname($classname);
  221. $classname = basename($classname);
  222. }
  223. return compact('package', 'classname', 'plugin');
  224. }
  225. /**
  226. * Destructor.
  227. *
  228. */
  229. public static function shutdown() {
  230. if (Configure::read('Session.defaults') == 'database' && function_exists('session_write_close')) {
  231. session_write_close();
  232. }
  233. }
  234. }