connection_manager.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: connection__manager_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 
00004 /**
00005  * Short description for file.
00006  *
00007  * Long description for file
00008  *
00009  * PHP versions 4 and 5
00010  *
00011  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00012  * Copyright 2005-2008, Cake Software Foundation, Inc.
00013  *                              1785 E. Sahara Avenue, Suite 490-204
00014  *                              Las Vegas, Nevada 89104
00015  *
00016  * Licensed under The MIT License
00017  * Redistributions of files must retain the above copyright notice.
00018  *
00019  * @filesource
00020  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00021  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00022  * @package         cake
00023  * @subpackage      cake.cake.libs.model
00024  * @since           CakePHP(tm) v 0.10.x.1402
00025  * @version         $Revision: 580 $
00026  * @modifiedby      $LastChangedBy: gwoo $
00027  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00028  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00029  */
00030 
00031 /**
00032  * Manages loaded instances of DataSource objects
00033  *
00034  * Long description for file
00035  *
00036  * @package     cake
00037  * @subpackage  cake.cake.libs.model
00038  */
00039 
00040 uses ('model' . DS . 'datasources' . DS . 'datasource');
00041 config('database');
00042 
00043 class ConnectionManager extends Object {
00044 /**
00045  * Holds a loaded instance of the Connections object
00046  *
00047  * @var object
00048  * @access public
00049  */
00050     var $config = null;
00051 /**
00052  * Holds instances DataSource objects
00053  *
00054  * @var array
00055  * @access protected
00056  */
00057     var $_dataSources = array();
00058 /**
00059  * Contains a list of all file and class names used in Connection settings
00060  *
00061  * @var array
00062  * @access protected
00063  */
00064     var $_connectionsEnum = array();
00065 /**
00066  * Constructor.
00067  *
00068  */
00069     function __construct() {
00070         if (class_exists('DATABASE_CONFIG')) {
00071             $this->config =& new DATABASE_CONFIG();
00072         }
00073     }
00074 /**
00075  * Gets a reference to the ConnectionManger object instance
00076  *
00077  * @return object Instance
00078  * @access public
00079  * @static
00080  */
00081     function &getInstance() {
00082         static $instance = array();
00083 
00084         if (!isset($instance[0]) || !$instance[0]) {
00085             $instance[0] =& new ConnectionManager();
00086         }
00087 
00088         return $instance[0];
00089     }
00090 /**
00091  * Gets a reference to a DataSource object
00092  *
00093  * @param string $name The name of the DataSource, as defined in app/config/connections
00094  * @return object Instance
00095  * @access public
00096  * @static
00097  */
00098     function &getDataSource($name) {
00099         $_this =& ConnectionManager::getInstance();
00100 
00101         if (in_array($name, array_keys($_this->_dataSources))) {
00102             return $_this->_dataSources[$name];
00103         }
00104 
00105         $connections = $_this->enumConnectionObjects();
00106         if (in_array($name, array_keys($connections))) {
00107             $conn = $connections[$name];
00108             $class = $conn['classname'];
00109             $_this->loadDataSource($name);
00110             $_this->_dataSources[$name] =& new $class($_this->config->{$name});
00111             $_this->_dataSources[$name]->configKeyName = $name;
00112         } else {
00113             trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
00114             return null;
00115         }
00116 
00117         return $_this->_dataSources[$name];
00118     }
00119 /**
00120  * Gets the list of available DataSource connections
00121  *
00122  * @return array List of available connections
00123  * @access public
00124  * @static
00125  */
00126     function sourceList() {
00127         $_this =& ConnectionManager::getInstance();
00128         return array_keys($_this->_dataSources);
00129     }
00130 /**
00131  * Gets a DataSource name from an object reference
00132  *
00133  * @param object $source DataSource object
00134  * @return string Datasource name
00135  * @access public
00136  * @static
00137  */
00138     function getSourceName(&$source) {
00139         $_this =& ConnectionManager::getInstance();
00140         $names = array_keys($_this->_dataSources);
00141         for ($i = 0; $i < count($names); $i++) {
00142             if ($_this->_dataSources[$names[$i]] === $source) {
00143                 return $names[$i];
00144             }
00145         }
00146         return null;
00147     }
00148 /**
00149  * Loads the DataSource class for the given connection name
00150  *
00151  * @param mixed $connName A string name of the connection, as defined in Connections config,
00152  *                        or an array containing the file and class name of the object.
00153  * @return boolean True on success, null on failure or false if the class is already loaded
00154  * @access public
00155  * @static
00156  */
00157     function loadDataSource($connName) {
00158         $_this =& ConnectionManager::getInstance();
00159 
00160         if (is_array($connName)) {
00161             $conn = $connName;
00162         } else {
00163             $connections = $_this->enumConnectionObjects();
00164             $conn = $connections[$connName];
00165         }
00166 
00167         if (isset($conn['parent']) && !empty($conn['parent'])) {
00168             $_this->loadDataSource($conn['parent']);
00169         }
00170 
00171         if (class_exists($conn['classname'])) {
00172             return false;
00173         }
00174 
00175         if (file_exists(MODELS . 'datasources' . DS . $conn['filename'] . '.php')) {
00176             require (MODELS . 'datasources' . DS . $conn['filename'] . '.php');
00177         } elseif (fileExistsInPath(LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php')) {
00178             require (LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php');
00179         } else {
00180             trigger_error(sprintf(__('Unable to load DataSource file %s.php', true), $conn['filename']), E_USER_ERROR);
00181             return null;
00182         }
00183     }
00184 /**
00185  * Gets a list of class and file names associated with the user-defined DataSource connections
00186  *
00187  * @return array An associative array of elements where the key is the connection name
00188  *               (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
00189  * @access public
00190  * @static
00191  */
00192     function enumConnectionObjects() {
00193         $_this =& ConnectionManager::getInstance();
00194 
00195         if (!empty($_this->_connectionsEnum)) {
00196             return $_this->_connectionsEnum;
00197         }
00198         $connections = get_object_vars($_this->config);
00199 
00200         if ($connections != null) {
00201             foreach ($connections as $name => $config) {
00202                 $_this->_connectionsEnum[$name] = $_this->__getDriver($config);
00203             }
00204             return $_this->_connectionsEnum;
00205         } else {
00206             $_this->cakeError('missingConnection', array(array('className' => 'ConnectionManager')));
00207         }
00208     }
00209 /**
00210  * Dynamically creates a DataSource object at runtime, with the given name and settings
00211  *
00212  * @param string $name The DataSource name
00213  * @param array $config The DataSource configuration settings
00214  * @return object A reference to the DataSource object, or null if creation failed
00215  * @access public
00216  * @static
00217  */
00218     function &create($name = '', $config = array()) {
00219         $_this =& ConnectionManager::getInstance();
00220 
00221         if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
00222             $null = null;
00223             return $null;
00224         }
00225 
00226         $_this->config->{$name} = $config;
00227         $_this->_connectionsEnum[$name] = $_this->__getDriver($config);
00228         return $_this->getDataSource($name);
00229     }
00230 /**
00231  * Returns the file, class name, and parent for the given driver.
00232  *
00233  * @return array An indexed array with: filename, classname, and parent
00234  * @access private
00235  */
00236     function __getDriver($config) {
00237         $_this =& ConnectionManager::getInstance();
00238 
00239         if (!isset($config['datasource'])) {
00240             $config['datasource'] = 'dbo';
00241         }
00242 
00243         if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) {
00244             $filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver'];
00245             $classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver']));
00246             $parent = $_this->__getDriver(array('datasource' => $config['datasource']));
00247         } else {
00248             $filename = $config['datasource'] . '_source';
00249             $classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
00250             $parent = null;
00251         }
00252         return array('filename'  => $filename, 'classname' => $classname, 'parent' => $parent);
00253     }
00254 /**
00255  * Destructor.
00256  *
00257  * @access private
00258  */
00259     function __destruct() {
00260         if (Configure::read('Session.save') == 'database' && function_exists('session_write_close')) {
00261             session_write_close();
00262         }
00263     }
00264 }
00265 
00266 ?>