PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/baser/connection_manager.php

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