PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/model/connection_manager.php

https://github.com/v2ninad/nm_cake
PHP | 274 lines | 138 code | 29 blank | 107 comment | 30 complexity | fdaff760a3cff7f270685edf438c6a65 MD5 | raw file
Possible License(s): GPL-2.0
  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.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. require LIBS . 'model' . DS . 'datasources' . DS . 'datasource.php';
  22. include_once CONFIGS . 'database.php';
  23. /**
  24. * Manages loaded instances of DataSource objects
  25. *
  26. * @package cake.libs.model
  27. */
  28. class ConnectionManager {
  29. /**
  30. * Holds a loaded instance of the Connections object
  31. *
  32. * @var DATABASE_CONFIG
  33. * @access public
  34. */
  35. public $config = null;
  36. /**
  37. * Holds instances DataSource objects
  38. *
  39. * @var array
  40. * @access protected
  41. */
  42. protected $_dataSources = array();
  43. /**
  44. * Contains a list of all file and class names used in Connection settings
  45. *
  46. * @var array
  47. * @access protected
  48. */
  49. protected $_connectionsEnum = array();
  50. /**
  51. * Constructor.
  52. *
  53. */
  54. function __construct() {
  55. if (class_exists('DATABASE_CONFIG')) {
  56. $this->config = new DATABASE_CONFIG();
  57. $this->_getConnectionObjects();
  58. }
  59. }
  60. /**
  61. * Gets a reference to the ConnectionManger object instance
  62. *
  63. * @return object Instance
  64. */
  65. public static function &getInstance() {
  66. static $instance = array();
  67. if (!$instance) {
  68. $instance[0] = new ConnectionManager();
  69. }
  70. return $instance[0];
  71. }
  72. /**
  73. * Gets a reference to a DataSource object
  74. *
  75. * @param string $name The name of the DataSource, as defined in app/config/database.php
  76. * @return object Instance
  77. */
  78. public static function &getDataSource($name) {
  79. //echo "NAME >>".$name;
  80. $_this = ConnectionManager::getInstance();
  81. if (!empty($_this->_dataSources[$name])) {
  82. $return = $_this->_dataSources[$name];
  83. return $return;
  84. }
  85. if (empty($_this->_connectionsEnum[$name])) {
  86. trigger_error(__("ConnectionManager::getDataSource - Non-existent data source %s", $name), E_USER_ERROR);
  87. $null = null;
  88. return $null;
  89. }
  90. $conn = $_this->_connectionsEnum[$name];
  91. $class = $conn['classname'];
  92. if ($_this->loadDataSource($name) === null) {
  93. trigger_error(__("ConnectionManager::getDataSource - Could not load class %s", $class), E_USER_ERROR);
  94. $null = null;
  95. return $null;
  96. }
  97. $_this->_dataSources[$name] = new $class($_this->config->{$name});
  98. $_this->_dataSources[$name]->configKeyName = $name;
  99. $return = $_this->_dataSources[$name];
  100. return $return;
  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. $_this = ConnectionManager::getInstance();
  109. return array_keys($_this->_dataSources);
  110. }
  111. /**
  112. * Gets a DataSource name from an object reference.
  113. *
  114. * **Warning** this method may cause fatal errors in PHP4.
  115. *
  116. * @param object $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. $_this = ConnectionManager::getInstance();
  122. foreach ($_this->_dataSources as $name => $ds) {
  123. if ($ds == $source) {
  124. return $name;
  125. }
  126. }
  127. return '';
  128. }
  129. /**
  130. * Loads the DataSource class for the given connection name
  131. *
  132. * @param mixed $connName A string name of the connection, as defined in app/config/database.php,
  133. * or an array containing the filename (without extension) and class name of the object,
  134. * to be found in app/models/datasources/ or cake/libs/model/datasources/.
  135. * @return boolean True on success, null on failure or false if the class is already loaded
  136. */
  137. public static function loadDataSource($connName) {
  138. $_this = ConnectionManager::getInstance();
  139. if (is_array($connName)) {
  140. $conn = $connName;
  141. } else {
  142. $conn = $_this->_connectionsEnum[$connName];
  143. }
  144. if (class_exists($conn['classname'])) {
  145. return false;
  146. }
  147. if (!empty($conn['parent'])) {
  148. $_this->loadDataSource($conn['parent']);
  149. }
  150. $conn = array_merge(array('plugin' => null, 'classname' => null, 'parent' => null), $conn);
  151. $class = "{$conn['plugin']}.{$conn['classname']}";
  152. if (!App::import('Datasource', $class, !is_null($conn['plugin']))) {
  153. trigger_error(__('ConnectionManager::loadDataSource - Unable to import DataSource class %s', $class), E_USER_ERROR);
  154. return null;
  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. $_this = ConnectionManager::getInstance();
  166. return $_this->_connectionsEnum;
  167. }
  168. /**
  169. * Dynamically creates a DataSource object at runtime, with the given name and settings
  170. *
  171. * @param string $name The DataSource name
  172. * @param array $config The DataSource configuration settings
  173. * @return object A reference to the DataSource object, or null if creation failed
  174. */
  175. public static function &create($name = '', $config = array()) {
  176. $_this = ConnectionManager::getInstance();
  177. if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
  178. $null = null;
  179. return $null;
  180. }
  181. $_this->config->{$name} = $config;
  182. $_this->_connectionsEnum[$name] = $_this->__connectionData($config);
  183. $return = $_this->getDataSource($name);
  184. return $return;
  185. }
  186. /**
  187. * Gets a list of class and file names associated with the user-defined DataSource connections
  188. *
  189. * @return void
  190. */
  191. protected function _getConnectionObjects() {
  192. $connections = get_object_vars($this->config);
  193. if ($connections != null) {
  194. foreach ($connections as $name => $config) {
  195. $this->_connectionsEnum[$name] = $this->__connectionData($config);
  196. }
  197. } else {
  198. throw new MissingConnectionException(array('class' => 'ConnectionManager'));
  199. }
  200. }
  201. /**
  202. * Returns the file, class name, and parent for the given driver.
  203. *
  204. * @return array An indexed array with: filename, classname, plugin and parent
  205. */
  206. private function __connectionData($config) {
  207. if (!isset($config['datasource'])) {
  208. $config['datasource'] = 'dbo';
  209. }
  210. $filename = $classname = $parent = $plugin = null;
  211. if (!empty($config['driver'])) {
  212. $parent = $this->__connectionData(array('datasource' => $config['datasource']));
  213. $parentSource = preg_replace('/_source$/', '', $parent['filename']);
  214. list($plugin, $classname) = pluginSplit($config['driver']);
  215. if ($plugin) {
  216. $source = Inflector::underscore($classname);
  217. } else {
  218. $source = $parentSource . '_' . $config['driver'];
  219. $classname = Inflector::camelize(strtolower($source));
  220. }
  221. $filename = $parentSource . DS . $source;
  222. } else {
  223. list($plugin, $classname) = pluginSplit($config['datasource']);
  224. if ($plugin) {
  225. $filename = Inflector::underscore($classname);
  226. } else {
  227. $filename = Inflector::underscore($config['datasource']);
  228. }
  229. if (substr($filename, -7) != '_source') {
  230. $filename .= '_source';
  231. }
  232. $classname = Inflector::camelize(strtolower($filename));
  233. }
  234. return compact('filename', 'classname', 'parent', 'plugin');
  235. }
  236. /**
  237. * Destructor.
  238. *
  239. */
  240. function __destruct() {
  241. if (Configure::read('Session.defaults') == 'database' && function_exists('session_write_close')) {
  242. session_write_close();
  243. }
  244. }
  245. }