PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/model/connection_manager.php

https://github.com/Forbin/cakephp2x
PHP | 276 lines | 128 code | 30 blank | 118 comment | 25 complexity | 13dac80ffef42df22c6d54eab29056b3 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 Version 5.x
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2009, 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-2009, 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. require LIBS . 'model' . DS . 'datasources' . DS . 'datasource.php';
  23. include_once CONFIGS . 'database.php';
  24. /**
  25. * Manages loaded instances of DataSource objects
  26. *
  27. * @package cake
  28. * @subpackage cake.cake.libs.model
  29. */
  30. class ConnectionManager extends Object {
  31. /**
  32. * Holds a loaded instance of the Connections object
  33. *
  34. * @var DATABASE_CONFIG
  35. * @access public
  36. */
  37. public $config = null;
  38. /**
  39. * Holds instances DataSource objects
  40. *
  41. * @var array
  42. * @access protected
  43. */
  44. protected $_dataSources = array();
  45. /**
  46. * Contains a list of all file and class names used in Connection settings
  47. *
  48. * @var array
  49. * @access protected
  50. */
  51. protected $_connectionsEnum = array();
  52. /**
  53. * Constructor.
  54. *
  55. */
  56. public function __construct() {
  57. if (class_exists('DATABASE_CONFIG')) {
  58. $this->config = new DATABASE_CONFIG();
  59. }
  60. }
  61. /**
  62. * Gets a reference to the ConnectionManger object instance
  63. *
  64. * @return object Instance
  65. * @access public
  66. * @static
  67. */
  68. public function &getInstance() {
  69. static $instance = array();
  70. if (!$instance) {
  71. $instance[0] = new ConnectionManager();
  72. }
  73. return $instance[0];
  74. }
  75. /**
  76. * Gets a reference to a DataSource object
  77. *
  78. * @param string $name The name of the DataSource, as defined in app/config/database.php
  79. * @return object Instance
  80. * @access public
  81. * @static
  82. */
  83. public function &getDataSource($name) {
  84. $_this = ConnectionManager::getInstance();
  85. if (!empty($_this->_dataSources[$name])) {
  86. $return = $_this->_dataSources[$name];
  87. return $return;
  88. }
  89. $connections = $_this->enumConnectionObjects();
  90. if (empty($connections[$name])) {
  91. trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
  92. $null = null;
  93. return $null;
  94. }
  95. $conn = $connections[$name];
  96. $class = $conn['classname'];
  97. if ($_this->loadDataSource($name) === null) {
  98. trigger_error(sprintf(__("ConnectionManager::getDataSource - Could not load class %s", true), $class), E_USER_ERROR);
  99. $null = null;
  100. return $null;
  101. }
  102. $_this->_dataSources[$name] = new $class($_this->config->{$name});
  103. $_this->_dataSources[$name]->configKeyName = $name;
  104. $return =& $_this->_dataSources[$name];
  105. return $return;
  106. }
  107. /**
  108. * Gets the list of available DataSource connections
  109. *
  110. * @return array List of available connections
  111. * @access public
  112. * @static
  113. */
  114. public function sourceList() {
  115. $_this = ConnectionManager::getInstance();
  116. return array_keys($_this->_dataSources);
  117. }
  118. /**
  119. * Gets a DataSource name from an object reference
  120. *
  121. * @param object $source DataSource object
  122. * @return string Datasource name, or null if source is not present
  123. * in the ConnectionManager.
  124. * @access public
  125. * @static
  126. */
  127. public function getSourceName(&$source) {
  128. $_this = ConnectionManager::getInstance();
  129. return array_search($source, $_this->_dataSources);
  130. }
  131. /**
  132. * Loads the DataSource class for the given connection name
  133. *
  134. * @param mixed $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/models/datasources/ or cake/libs/model/datasources/.
  137. * @return boolean True on success, null on failure or false if the class is already loaded
  138. * @access public
  139. * @static
  140. */
  141. public function loadDataSource($connName) {
  142. $_this = ConnectionManager::getInstance();
  143. if (is_array($connName)) {
  144. $conn = $connName;
  145. } else {
  146. $connections = $_this->enumConnectionObjects();
  147. $conn = $connections[$connName];
  148. }
  149. if (class_exists($conn['classname'])) {
  150. return false;
  151. }
  152. if (!empty($conn['parent'])) {
  153. $_this->loadDataSource($conn['parent']);
  154. }
  155. $conn = array_merge(array('plugin' => null, 'classname' => null, 'parent' => null), $conn);
  156. $class = "{$conn['plugin']}.{$conn['classname']}";
  157. if (!App::import('Datasource', $class, false)) {
  158. $error = __('ConnectionManager::loadDataSource - Unable to import DataSource class %s', true);
  159. trigger_error(sprintf($error, $class), E_USER_ERROR);
  160. return null;
  161. }
  162. return true;
  163. }
  164. /**
  165. * Gets a list of class and file names associated with the user-defined DataSource connections
  166. *
  167. * @return array An associative array of elements where the key is the connection name
  168. * (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
  169. * @access public
  170. * @static
  171. */
  172. public function enumConnectionObjects() {
  173. $_this = ConnectionManager::getInstance();
  174. if (!empty($_this->_connectionsEnum)) {
  175. return $_this->_connectionsEnum;
  176. }
  177. $connections = get_object_vars($_this->config);
  178. if ($connections != null) {
  179. foreach ($connections as $name => $config) {
  180. $_this->_connectionsEnum[$name] = $_this->__connectionData($config);
  181. }
  182. return $_this->_connectionsEnum;
  183. } else {
  184. $_this->cakeError('missingConnection', array(array('className' => 'ConnectionManager')));
  185. }
  186. }
  187. /**
  188. * Dynamically creates a DataSource object at runtime, with the given name and settings
  189. *
  190. * @param string $name The DataSource name
  191. * @param array $config The DataSource configuration settings
  192. * @return object A reference to the DataSource object, or null if creation failed
  193. * @access public
  194. * @static
  195. */
  196. public function &create($name = '', $config = array()) {
  197. $_this = ConnectionManager::getInstance();
  198. if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
  199. $null = null;
  200. return $null;
  201. }
  202. $_this->config->{$name} = $config;
  203. $_this->_connectionsEnum[$name] = $_this->__connectionData($config);
  204. $return = $_this->getDataSource($name);
  205. return $return;
  206. }
  207. /**
  208. * Returns the file, class name, and parent for the given driver.
  209. *
  210. * @return array An indexed array with: filename, classname, plugin and parent
  211. * @access private
  212. */
  213. private function __connectionData($config) {
  214. if (!isset($config['datasource'])) {
  215. $config['datasource'] = 'dbo';
  216. }
  217. $filename = $classname = $parent = $plugin = null;
  218. if (!empty($config['driver'])) {
  219. $source = $config['datasource'] . '_' . $config['driver'];
  220. $filename = $config['datasource'] . DS . $source;
  221. $classname = Inflector::camelize(strtolower($source));
  222. $parent = $this->__connectionData(array('datasource' => $config['datasource']));
  223. } else {
  224. if (strpos($config['datasource'], '.') !== false) {
  225. list($plugin, $classname) = explode('.', $config['datasource']);
  226. $filename = Inflector::underscore($classname);
  227. } else {
  228. $filename = $config['datasource'] . '_source';
  229. $classname = Inflector::camelize(strtolower($filename));
  230. }
  231. }
  232. return compact('filename', 'classname', 'parent', 'plugin');
  233. }
  234. /**
  235. * Destructor.
  236. *
  237. * @return void
  238. * @access public
  239. */
  240. public function __destruct() {
  241. if (Configure::read('Session.save') == 'database' && function_exists('session_write_close')) {
  242. session_write_close();
  243. }
  244. }
  245. }
  246. ?>