connection_manager.php

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

Generated on Sun Nov 22 00:30:53 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7