PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Model/ConnectionManager.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 265 lines | 122 code | 28 blank | 115 comment | 24 complexity | a77aebfad18abf4375d0c1c6b1d9dcb9 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 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. * Provides an interface for loading and enumerating connections defined in
  26. * app/Config/database.php
  27. *
  28. * @package Cake.Model
  29. */
  30. class ConnectionManager {
  31. /**
  32. * Holds a loaded instance of the Connections object
  33. *
  34. * @var DATABASE_CONFIG
  35. */
  36. public static $config = null;
  37. /**
  38. * Holds instances DataSource objects
  39. *
  40. * @var array
  41. */
  42. protected static $_dataSources = array();
  43. /**
  44. * Contains a list of all file and class names used in Connection settings
  45. *
  46. * @var array
  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. protected static $_init = false;
  55. /**
  56. * Loads connections configuration.
  57. *
  58. * @return void
  59. */
  60. protected static function _init() {
  61. include_once APP . 'Config' . DS . 'database.php';
  62. if (class_exists('DATABASE_CONFIG')) {
  63. self::$config = new DATABASE_CONFIG();
  64. }
  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 DataSource Instance
  72. * @throws MissingDatasourceConfigException
  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. 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 DataSource $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 MissingDatasourceException
  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 MissingDatasourceException(array(
  155. 'class' => $conn['classname'],
  156. 'plugin' => substr($plugin, 0, -1)
  157. ));
  158. }
  159. return true;
  160. }
  161. /**
  162. * Return a list of connections
  163. *
  164. * @return array An associative array of elements where the key is the connection name
  165. * (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
  166. */
  167. public static function enumConnectionObjects() {
  168. if (empty(self::$_init)) {
  169. self::_init();
  170. }
  171. return (array) self::$config;
  172. }
  173. /**
  174. * Dynamically creates a DataSource object at runtime, with the given name and settings
  175. *
  176. * @param string $name The DataSource name
  177. * @param array $config The DataSource configuration settings
  178. * @return DataSource A reference to the DataSource object, or null if creation failed
  179. */
  180. public static function create($name = '', $config = array()) {
  181. if (empty(self::$_init)) {
  182. self::_init();
  183. }
  184. if (empty($name) || empty($config) || array_key_exists($name, self::$_connectionsEnum)) {
  185. return null;
  186. }
  187. self::$config->{$name} = $config;
  188. self::$_connectionsEnum[$name] = self::_connectionData($config);
  189. $return = self::getDataSource($name);
  190. return $return;
  191. }
  192. /**
  193. * Removes a connection configuration at runtime given its name
  194. *
  195. * @param string $name the connection name as it was created
  196. * @return boolean success if connection was removed, false if it does not exist
  197. */
  198. public static function drop($name) {
  199. if (empty(self::$_init)) {
  200. self::_init();
  201. }
  202. if (!isset(self::$config->{$name})) {
  203. return false;
  204. }
  205. unset(self::$_connectionsEnum[$name], self::$_dataSources[$name], self::$config->{$name});
  206. return true;
  207. }
  208. /**
  209. * Gets a list of class and file names associated with the user-defined DataSource connections
  210. *
  211. * @param string $name Connection name
  212. * @return void
  213. * @throws MissingDatasourceConfigException
  214. */
  215. protected static function _getConnectionObject($name) {
  216. if (!empty(self::$config->{$name})) {
  217. self::$_connectionsEnum[$name] = self::_connectionData(self::$config->{$name});
  218. } else {
  219. throw new MissingDatasourceConfigException(array('config' => $name));
  220. }
  221. }
  222. /**
  223. * Returns the file, class name, and parent for the given driver.
  224. *
  225. * @param array $config Array with connection configuration. Key 'datasource' is required
  226. * @return array An indexed array with: filename, classname, plugin and parent
  227. */
  228. protected static function _connectionData($config) {
  229. $package = $classname = $plugin = null;
  230. list($plugin, $classname) = pluginSplit($config['datasource']);
  231. if (strpos($classname, '/') !== false) {
  232. $package = dirname($classname);
  233. $classname = basename($classname);
  234. }
  235. return compact('package', 'classname', 'plugin');
  236. }
  237. }