/lib/Cake/Model/ConnectionManager.php

https://bitbucket.org/koronios/cakephp · PHP · 273 lines · 129 code · 29 blank · 115 comment · 25 complexity · 44bdda2e05d242df623ad1646816f2a0 MD5 · raw file

  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 MissingDatasourceException
  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. if (strpos(App::location($class), 'Datasource') === false) {
  90. throw new MissingDatasourceException(array(
  91. 'class' => $class,
  92. 'plugin' => null,
  93. 'message' => 'Datasource is not found in Model/Datasource package.'
  94. ));
  95. }
  96. self::$_dataSources[$name] = new $class(self::$config->{$name});
  97. self::$_dataSources[$name]->configKeyName = $name;
  98. return self::$_dataSources[$name];
  99. }
  100. /**
  101. * Gets the list of available DataSource connections
  102. * This will only return the datasources instantiated by this manager
  103. * It differs from enumConnectionObjects, since the latter will return all configured 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. * @param DataSource $source DataSource object
  117. * @return string Datasource name, or null if source is not present
  118. * in the ConnectionManager.
  119. */
  120. public static function getSourceName($source) {
  121. if (empty(self::$_init)) {
  122. self::_init();
  123. }
  124. foreach (self::$_dataSources as $name => $ds) {
  125. if ($ds === $source) {
  126. return $name;
  127. }
  128. }
  129. return null;
  130. }
  131. /**
  132. * Loads the DataSource class for the given connection name
  133. *
  134. * @param string|array $connName A string name of the connection, as defined in app/Config/database.php,
  135. * or an array containing the filename (without extension) and class name of the object,
  136. * to be found in app/Model/Datasource/ or lib/Cake/Model/Datasource/.
  137. * @return boolean True on success, null on failure or false if the class is already loaded
  138. * @throws MissingDatasourceException
  139. */
  140. public static function loadDataSource($connName) {
  141. if (empty(self::$_init)) {
  142. self::_init();
  143. }
  144. if (is_array($connName)) {
  145. $conn = $connName;
  146. } else {
  147. $conn = self::$_connectionsEnum[$connName];
  148. }
  149. if (class_exists($conn['classname'], false)) {
  150. return false;
  151. }
  152. $plugin = $package = null;
  153. if (!empty($conn['plugin'])) {
  154. $plugin = $conn['plugin'] . '.';
  155. }
  156. if (!empty($conn['package'])) {
  157. $package = '/' . $conn['package'];
  158. }
  159. App::uses($conn['classname'], $plugin . 'Model/Datasource' . $package);
  160. if (!class_exists($conn['classname'])) {
  161. throw new MissingDatasourceException(array(
  162. 'class' => $conn['classname'],
  163. 'plugin' => substr($plugin, 0, -1)
  164. ));
  165. }
  166. return true;
  167. }
  168. /**
  169. * Return a list of connections
  170. *
  171. * @return array An associative array of elements where the key is the connection name
  172. * (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
  173. */
  174. public static function enumConnectionObjects() {
  175. if (empty(self::$_init)) {
  176. self::_init();
  177. }
  178. return (array)self::$config;
  179. }
  180. /**
  181. * Dynamically creates a DataSource object at runtime, with the given name and settings
  182. *
  183. * @param string $name The DataSource name
  184. * @param array $config The DataSource configuration settings
  185. * @return DataSource A reference to the DataSource object, or null if creation failed
  186. */
  187. public static function create($name = '', $config = array()) {
  188. if (empty(self::$_init)) {
  189. self::_init();
  190. }
  191. if (empty($name) || empty($config) || array_key_exists($name, self::$_connectionsEnum)) {
  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. * Removes a connection configuration at runtime given its name
  201. *
  202. * @param string $name the connection name as it was created
  203. * @return boolean success if connection was removed, false if it does not exist
  204. */
  205. public static function drop($name) {
  206. if (empty(self::$_init)) {
  207. self::_init();
  208. }
  209. if (!isset(self::$config->{$name})) {
  210. return false;
  211. }
  212. unset(self::$_connectionsEnum[$name], self::$_dataSources[$name], self::$config->{$name});
  213. return true;
  214. }
  215. /**
  216. * Gets a list of class and file names associated with the user-defined DataSource connections
  217. *
  218. * @param string $name Connection name
  219. * @return void
  220. * @throws MissingDatasourceConfigException
  221. */
  222. protected static function _getConnectionObject($name) {
  223. if (!empty(self::$config->{$name})) {
  224. self::$_connectionsEnum[$name] = self::_connectionData(self::$config->{$name});
  225. } else {
  226. throw new MissingDatasourceConfigException(array('config' => $name));
  227. }
  228. }
  229. /**
  230. * Returns the file, class name, and parent for the given driver.
  231. *
  232. * @param array $config Array with connection configuration. Key 'datasource' is required
  233. * @return array An indexed array with: filename, classname, plugin and parent
  234. */
  235. protected static function _connectionData($config) {
  236. $package = $classname = $plugin = null;
  237. list($plugin, $classname) = pluginSplit($config['datasource']);
  238. if (strpos($classname, '/') !== false) {
  239. $package = dirname($classname);
  240. $classname = basename($classname);
  241. }
  242. return compact('package', 'classname', 'plugin');
  243. }
  244. }