PageRenderTime 46ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/cake/libs/model/connection_manager.php

http://skygames.googlecode.com/
PHP | 261 lines | 124 code | 15 blank | 122 comment | 29 complexity | 63161e7d088d1d9b0960099ad3230ab0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0
  1. <?php
  2. /* SVN FILE: $Id: connection_manager.php 7847 2008-11-08 02:54:07Z renan.saddam $ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  19. * @package cake
  20. * @subpackage cake.cake.libs.model
  21. * @since CakePHP(tm) v 0.10.x.1402
  22. * @version $Revision: 7847 $
  23. * @modifiedby $LastChangedBy: renan.saddam $
  24. * @lastmodified $Date: 2008-11-07 20:54:07 -0600 (Fri, 07 Nov 2008) $
  25. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  26. */
  27. uses ('model' . DS . 'datasources' . DS . 'datasource');
  28. config('database');
  29. /**
  30. * Manages loaded instances of DataSource objects
  31. *
  32. * Long description for file
  33. *
  34. * @package cake
  35. * @subpackage cake.cake.libs.model
  36. */
  37. class ConnectionManager extends Object {
  38. /**
  39. * Holds a loaded instance of the Connections object
  40. *
  41. * @var object
  42. * @access public
  43. */
  44. var $config = null;
  45. /**
  46. * Holds instances DataSource objects
  47. *
  48. * @var array
  49. * @access protected
  50. */
  51. var $_dataSources = array();
  52. /**
  53. * Contains a list of all file and class names used in Connection settings
  54. *
  55. * @var array
  56. * @access protected
  57. */
  58. var $_connectionsEnum = array();
  59. /**
  60. * Constructor.
  61. *
  62. */
  63. function __construct() {
  64. if (class_exists('DATABASE_CONFIG')) {
  65. $this->config =& new DATABASE_CONFIG();
  66. }
  67. }
  68. /**
  69. * Gets a reference to the ConnectionManger object instance
  70. *
  71. * @return object Instance
  72. * @access public
  73. * @static
  74. */
  75. function &getInstance() {
  76. static $instance = array();
  77. if (!$instance) {
  78. $instance[0] =& new ConnectionManager();
  79. }
  80. return $instance[0];
  81. }
  82. /**
  83. * Gets a reference to a DataSource object
  84. *
  85. * @param string $name The name of the DataSource, as defined in app/config/connections
  86. * @return object Instance
  87. * @access public
  88. * @static
  89. */
  90. function &getDataSource($name) {
  91. $_this =& ConnectionManager::getInstance();
  92. if (!empty($_this->_dataSources[$name])) {
  93. $return =& $_this->_dataSources[$name];
  94. return $return;
  95. }
  96. $connections = $_this->enumConnectionObjects();
  97. if (!empty($connections[$name])) {
  98. $conn = $connections[$name];
  99. $class = $conn['classname'];
  100. $_this->loadDataSource($name);
  101. $_this->_dataSources[$name] =& new $class($_this->config->{$name});
  102. $_this->_dataSources[$name]->configKeyName = $name;
  103. } else {
  104. trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
  105. return null;
  106. }
  107. $return =& $_this->_dataSources[$name];
  108. return $return;
  109. }
  110. /**
  111. * Gets the list of available DataSource connections
  112. *
  113. * @return array List of available connections
  114. * @access public
  115. * @static
  116. */
  117. function sourceList() {
  118. $_this =& ConnectionManager::getInstance();
  119. return array_keys($_this->_dataSources);
  120. }
  121. /**
  122. * Gets a DataSource name from an object reference
  123. *
  124. * @param object $source DataSource object
  125. * @return string Datasource name
  126. * @access public
  127. * @static
  128. */
  129. function getSourceName(&$source) {
  130. $_this =& ConnectionManager::getInstance();
  131. $names = array_keys($_this->_dataSources);
  132. for ($i = 0; $i < count($names); $i++) {
  133. if ($_this->_dataSources[$names[$i]] === $source) {
  134. return $names[$i];
  135. }
  136. }
  137. return null;
  138. }
  139. /**
  140. * Loads the DataSource class for the given connection name
  141. *
  142. * @param mixed $connName A string name of the connection, as defined in Connections config,
  143. * or an array containing the file and class name of the object.
  144. * @return boolean True on success, null on failure or false if the class is already loaded
  145. * @access public
  146. * @static
  147. */
  148. function loadDataSource($connName) {
  149. $_this =& ConnectionManager::getInstance();
  150. if (is_array($connName)) {
  151. $conn = $connName;
  152. } else {
  153. $connections = $_this->enumConnectionObjects();
  154. $conn = $connections[$connName];
  155. }
  156. if (!empty($conn['parent'])) {
  157. $_this->loadDataSource($conn['parent']);
  158. }
  159. if (class_exists($conn['classname'])) {
  160. return false;
  161. }
  162. if (file_exists(MODELS . 'datasources' . DS . $conn['filename'] . '.php')) {
  163. require (MODELS . 'datasources' . DS . $conn['filename'] . '.php');
  164. } elseif (fileExistsInPath(LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php')) {
  165. require (LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php');
  166. } else {
  167. trigger_error(sprintf(__('Unable to load DataSource file %s.php', true), $conn['filename']), E_USER_ERROR);
  168. return null;
  169. }
  170. }
  171. /**
  172. * Gets a list of class and file names associated with the user-defined DataSource connections
  173. *
  174. * @return array An associative array of elements where the key is the connection name
  175. * (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
  176. * @access public
  177. * @static
  178. */
  179. function enumConnectionObjects() {
  180. $_this =& ConnectionManager::getInstance();
  181. if (!empty($_this->_connectionsEnum)) {
  182. return $_this->_connectionsEnum;
  183. }
  184. $connections = get_object_vars($_this->config);
  185. if ($connections != null) {
  186. foreach ($connections as $name => $config) {
  187. $_this->_connectionsEnum[$name] = $_this->__getDriver($config);
  188. }
  189. return $_this->_connectionsEnum;
  190. } else {
  191. $_this->cakeError('missingConnection', array(array('className' => 'ConnectionManager')));
  192. }
  193. }
  194. /**
  195. * Dynamically creates a DataSource object at runtime, with the given name and settings
  196. *
  197. * @param string $name The DataSource name
  198. * @param array $config The DataSource configuration settings
  199. * @return object A reference to the DataSource object, or null if creation failed
  200. * @access public
  201. * @static
  202. */
  203. function &create($name = '', $config = array()) {
  204. $_this =& ConnectionManager::getInstance();
  205. if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
  206. $null = null;
  207. return $null;
  208. }
  209. $_this->config->{$name} = $config;
  210. $_this->_connectionsEnum[$name] = $_this->__getDriver($config);
  211. $return =& $_this->getDataSource($name);
  212. return $return;
  213. }
  214. /**
  215. * Returns the file, class name, and parent for the given driver.
  216. *
  217. * @return array An indexed array with: filename, classname, and parent
  218. * @access private
  219. */
  220. function __getDriver($config) {
  221. if (!isset($config['datasource'])) {
  222. $config['datasource'] = 'dbo';
  223. }
  224. if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) {
  225. $filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver'];
  226. $classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver']));
  227. $parent = $this->__getDriver(array('datasource' => $config['datasource']));
  228. } else {
  229. $filename = $config['datasource'] . '_source';
  230. $classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
  231. $parent = null;
  232. }
  233. return array('filename' => $filename, 'classname' => $classname, 'parent' => $parent);
  234. }
  235. /**
  236. * Destructor.
  237. *
  238. * @access private
  239. */
  240. function __destruct() {
  241. if (Configure::read('Session.save') == 'database' && function_exists('session_write_close')) {
  242. session_write_close();
  243. }
  244. }
  245. }
  246. ?>