PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/model/connection_manager.php

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