ConnectionManager.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * For full copyright and license information, please see the LICENSE.txt
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP(tm) Project
  18. * @package Cake.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. * Provides an interface for loading and enumerating connections defined in
  27. * app/Config/database.php
  28. *
  29. * @package Cake.Model
  30. */
  31. class ConnectionManager {
  32. /**
  33. * Holds a loaded instance of the Connections object
  34. *
  35. * @var DATABASE_CONFIG
  36. */
  37. public static $config = null;
  38. /**
  39. * Holds instances DataSource objects
  40. *
  41. * @var array
  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. */
  49. protected static $_connectionsEnum = array();
  50. /**
  51. * Indicates if the init code for this class has already been executed
  52. *
  53. * @var boolean
  54. */
  55. protected static $_init = false;
  56. /**
  57. * Loads connections configuration.
  58. *
  59. * @return void
  60. */
  61. protected static function _init() {
  62. include_once APP . 'Config' . DS . 'database.php';
  63. if (class_exists('DATABASE_CONFIG')) {
  64. self::$config = new DATABASE_CONFIG();
  65. }
  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 DataSource Instance
  73. * @throws MissingDatasourceConfigException
  74. * @throws MissingDatasourceException
  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. $instance = new $class(self::$config->{$name});
  91. $instance->configKeyName = $name;
  92. if (!$instance instanceof Datasource) {
  93. throw new MissingDatasourceException(array(
  94. 'class' => $class,
  95. 'plugin' => null,
  96. 'message' => 'Only classes extending Datasource can be used as datasources.'
  97. ));
  98. }
  99. self::$_dataSources[$name] = $instance;
  100. return self::$_dataSources[$name];
  101. }
  102. /**
  103. * Gets the list of available DataSource connections
  104. * This will only return the datasources instantiated by this manager
  105. * It differs from enumConnectionObjects, since the latter will return all configured connections
  106. *
  107. * @return array List of available connections
  108. */
  109. public static function sourceList() {
  110. if (empty(self::$_init)) {
  111. self::_init();
  112. }
  113. return array_keys(self::$_dataSources);
  114. }
  115. /**
  116. * Gets a DataSource name from an object reference.
  117. *
  118. * @param DataSource $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 null;
  132. }
  133. /**
  134. * Loads the DataSource class for the given connection name
  135. *
  136. * @param string|array $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/Model/Datasource/ or lib/Cake/Model/Datasource/.
  139. * @return boolean True on success, null on failure or false if the class is already loaded
  140. * @throws MissingDatasourceException
  141. */
  142. public static function loadDataSource($connName) {
  143. if (empty(self::$_init)) {
  144. self::_init();
  145. }
  146. if (is_array($connName)) {
  147. $conn = $connName;
  148. } else {
  149. $conn = self::$_connectionsEnum[$connName];
  150. }
  151. if (class_exists($conn['classname'], false)) {
  152. return false;
  153. }
  154. $plugin = $package = null;
  155. if (!empty($conn['plugin'])) {
  156. $plugin = $conn['plugin'] . '.';
  157. }
  158. if (!empty($conn['package'])) {
  159. $package = '/' . $conn['package'];
  160. }
  161. App::uses($conn['classname'], $plugin . 'Model/Datasource' . $package);
  162. if (!class_exists($conn['classname'])) {
  163. throw new MissingDatasourceException(array(
  164. 'class' => $conn['classname'],
  165. 'plugin' => substr($plugin, 0, -1)
  166. ));
  167. }
  168. return true;
  169. }
  170. /**
  171. * Return a list of connections
  172. *
  173. * @return array An associative array of elements where the key is the connection name
  174. * (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
  175. */
  176. public static function enumConnectionObjects() {
  177. if (empty(self::$_init)) {
  178. self::_init();
  179. }
  180. return (array)self::$config;
  181. }
  182. /**
  183. * Dynamically creates a DataSource object at runtime, with the given name and settings
  184. *
  185. * @param string $name The DataSource name
  186. * @param array $config The DataSource configuration settings
  187. * @return DataSource A reference to the DataSource object, or null if creation failed
  188. */
  189. public static function create($name = '', $config = array()) {
  190. if (empty(self::$_init)) {
  191. self::_init();
  192. }
  193. if (empty($name) || empty($config) || array_key_exists($name, self::$_connectionsEnum)) {
  194. return null;
  195. }
  196. self::$config->{$name} = $config;
  197. self::$_connectionsEnum[$name] = self::_connectionData($config);
  198. $return = self::getDataSource($name);
  199. return $return;
  200. }
  201. /**
  202. * Removes a connection configuration at runtime given its name
  203. *
  204. * @param string $name the connection name as it was created
  205. * @return boolean success if connection was removed, false if it does not exist
  206. */
  207. public static function drop($name) {
  208. if (empty(self::$_init)) {
  209. self::_init();
  210. }
  211. if (!isset(self::$config->{$name})) {
  212. return false;
  213. }
  214. unset(self::$_connectionsEnum[$name], self::$_dataSources[$name], self::$config->{$name});
  215. return true;
  216. }
  217. /**
  218. * Gets a list of class and file names associated with the user-defined DataSource connections
  219. *
  220. * @param string $name Connection name
  221. * @return void
  222. * @throws MissingDatasourceConfigException
  223. */
  224. protected static function _getConnectionObject($name) {
  225. if (!empty(self::$config->{$name})) {
  226. self::$_connectionsEnum[$name] = self::_connectionData(self::$config->{$name});
  227. } else {
  228. throw new MissingDatasourceConfigException(array('config' => $name));
  229. }
  230. }
  231. /**
  232. * Returns the file, class name, and parent for the given driver.
  233. *
  234. * @param array $config Array with connection configuration. Key 'datasource' is required
  235. * @return array An indexed array with: filename, classname, plugin and parent
  236. */
  237. protected static function _connectionData($config) {
  238. $package = $classname = $plugin = null;
  239. list($plugin, $classname) = pluginSplit($config['datasource']);
  240. if (strpos($classname, '/') !== false) {
  241. $package = dirname($classname);
  242. $classname = basename($classname);
  243. }
  244. return compact('package', 'classname', 'plugin');
  245. }
  246. }