PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/cake/libs/model/connection_manager.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 295 lines | 138 code | 31 blank | 126 comment | 30 complexity | 2ea02ad085950a28868577b9486ef815 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  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 versions 4 and 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
  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. var $config = null;
  38. /**
  39. * Holds instances DataSource objects
  40. *
  41. * @var array
  42. * @access protected
  43. */
  44. var $_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. var $_connectionsEnum = array();
  52. /**
  53. * Constructor.
  54. *
  55. */
  56. function __construct() {
  57. if (class_exists('DATABASE_CONFIG')) {
  58. $this->config =& new DATABASE_CONFIG();
  59. $this->_getConnectionObjects();
  60. }
  61. }
  62. /**
  63. * Gets a reference to the ConnectionManger object instance
  64. *
  65. * @return object Instance
  66. * @access public
  67. * @static
  68. */
  69. function &getInstance() {
  70. static $instance = array();
  71. if (!$instance) {
  72. $instance[0] =& new ConnectionManager();
  73. }
  74. return $instance[0];
  75. }
  76. /**
  77. * Gets a reference to a DataSource object
  78. *
  79. * @param string $name The name of the DataSource, as defined in app/config/database.php
  80. * @return object Instance
  81. * @access public
  82. * @static
  83. */
  84. function &getDataSource($name) {
  85. $_this =& ConnectionManager::getInstance();
  86. if (!empty($_this->_dataSources[$name])) {
  87. $return =& $_this->_dataSources[$name];
  88. return $return;
  89. }
  90. if (empty($_this->_connectionsEnum[$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 = $_this->_connectionsEnum[$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. 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. * **Warning** this method may cause fatal errors in PHP4.
  122. *
  123. * @param object $source DataSource object
  124. * @return string Datasource name, or null if source is not present
  125. * in the ConnectionManager.
  126. * @access public
  127. * @static
  128. */
  129. function getSourceName(&$source) {
  130. $_this =& ConnectionManager::getInstance();
  131. foreach ($_this->_dataSources as $name => $ds) {
  132. if ($ds == $source) {
  133. return $name;
  134. }
  135. }
  136. return '';
  137. }
  138. /**
  139. * Loads the DataSource class for the given connection name
  140. *
  141. * @param mixed $connName A string name of the connection, as defined in app/config/database.php,
  142. * or an array containing the filename (without extension) and class name of the object,
  143. * to be found in app/models/datasources/ or cake/libs/model/datasources/.
  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. $conn = $_this->_connectionsEnum[$connName];
  154. }
  155. if (class_exists($conn['classname'])) {
  156. return false;
  157. }
  158. if (!empty($conn['parent'])) {
  159. $_this->loadDataSource($conn['parent']);
  160. }
  161. $conn = array_merge(array('plugin' => null, 'classname' => null, 'parent' => null), $conn);
  162. $class = "{$conn['plugin']}.{$conn['classname']}";
  163. if (!App::import('Datasource', $class, !is_null($conn['plugin']))) {
  164. trigger_error(sprintf(__('ConnectionManager::loadDataSource - Unable to import DataSource class %s', true), $class), E_USER_ERROR);
  165. return null;
  166. }
  167. return true;
  168. }
  169. /**
  170. * Return a list of connections
  171. *
  172. * @return array An associative array of elements where the key is the connection name
  173. * (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
  174. * @access public
  175. * @static
  176. */
  177. function enumConnectionObjects() {
  178. $_this =& ConnectionManager::getInstance();
  179. return $_this->_connectionsEnum;
  180. }
  181. /**
  182. * Dynamically creates a DataSource object at runtime, with the given name and settings
  183. *
  184. * @param string $name The DataSource name
  185. * @param array $config The DataSource configuration settings
  186. * @return object A reference to the DataSource object, or null if creation failed
  187. * @access public
  188. * @static
  189. */
  190. function &create($name = '', $config = array()) {
  191. $_this =& ConnectionManager::getInstance();
  192. if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
  193. $null = null;
  194. return $null;
  195. }
  196. $_this->config->{$name} = $config;
  197. $_this->_connectionsEnum[$name] = $_this->__connectionData($config);
  198. $return =& $_this->getDataSource($name);
  199. return $return;
  200. }
  201. /**
  202. * Gets a list of class and file names associated with the user-defined DataSource connections
  203. *
  204. * @return void
  205. * @access protected
  206. * @static
  207. */
  208. function _getConnectionObjects() {
  209. $connections = get_object_vars($this->config);
  210. if ($connections != null) {
  211. foreach ($connections as $name => $config) {
  212. $this->_connectionsEnum[$name] = $this->__connectionData($config);
  213. }
  214. } else {
  215. $this->cakeError('missingConnection', array(array('code' => 500, 'className' => 'ConnectionManager')));
  216. }
  217. }
  218. /**
  219. * Returns the file, class name, and parent for the given driver.
  220. *
  221. * @return array An indexed array with: filename, classname, plugin and parent
  222. * @access private
  223. */
  224. function __connectionData($config) {
  225. if (!isset($config['datasource'])) {
  226. $config['datasource'] = 'dbo';
  227. }
  228. $filename = $classname = $parent = $plugin = null;
  229. if (!empty($config['driver'])) {
  230. $parent = $this->__connectionData(array('datasource' => $config['datasource']));
  231. $parentSource = preg_replace('/_source$/', '', $parent['filename']);
  232. list($plugin, $classname) = pluginSplit($config['driver']);
  233. if ($plugin) {
  234. $source = Inflector::underscore($classname);
  235. } else {
  236. $source = $parentSource . '_' . $config['driver'];
  237. $classname = Inflector::camelize(strtolower($source));
  238. }
  239. $filename = $parentSource . DS . $source;
  240. } else {
  241. list($plugin, $classname) = pluginSplit($config['datasource']);
  242. if ($plugin) {
  243. $filename = Inflector::underscore($classname);
  244. } else {
  245. $filename = Inflector::underscore($config['datasource']);
  246. }
  247. if (substr($filename, -7) != '_source') {
  248. $filename .= '_source';
  249. }
  250. $classname = Inflector::camelize(strtolower($filename));
  251. }
  252. return compact('filename', 'classname', 'parent', 'plugin');
  253. }
  254. /**
  255. * Destructor.
  256. *
  257. * @access private
  258. */
  259. function __destruct() {
  260. if (Configure::read('Session.save') == 'database' && function_exists('session_write_close')) {
  261. session_write_close();
  262. }
  263. }
  264. }