[ Index ]
 

Code source de CakePHP 1.1.13.4450

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/cake/libs/model/ -> connection_manager.php (source)

   1  <?php
   2  /* SVN FILE: $Id: connection_manager.php 4409 2007-02-02 13:20:59Z phpnut $ */
   3  /**
   4   * Short description for file.
   5   *
   6   * Long description for file
   7   *
   8   * PHP versions 4 and 5
   9   *
  10   * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
  11   * Copyright 2005-2007, Cake Software Foundation, Inc.
  12   *                                1785 E. Sahara Avenue, Suite 490-204
  13   *                                Las Vegas, Nevada 89104
  14   *
  15   * Licensed under The MIT License
  16   * Redistributions of files must retain the above copyright notice.
  17   *
  18   * @filesource
  19   * @copyright        Copyright 2005-2007, Cake Software Foundation, Inc.
  20   * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  21   * @package            cake
  22   * @subpackage        cake.cake.libs.model
  23   * @since            CakePHP(tm) v 0.10.x.1402
  24   * @version            $Revision: 4409 $
  25   * @modifiedby        $LastChangedBy: phpnut $
  26   * @lastmodified    $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
  27   * @license            http://www.opensource.org/licenses/mit-license.php The MIT License
  28   */
  29  /**
  30   * Load files needed by this class
  31   */
  32  uses ('model' . DS . 'datasources' . DS . 'datasource');
  33  config('database');
  34  /**
  35   * Manages loaded instances of DataSource objects
  36   *
  37   * Long description for file
  38   *
  39   * @package        cake
  40   * @subpackage    cake.cake.libs.model
  41   */
  42  class ConnectionManager extends Object{
  43  /**
  44   * Holds a loaded instance of the Connections object
  45   *
  46   * @var class:Connections
  47   * @access public
  48   */
  49      var $config = null;
  50  /**
  51   * Holds instances DataSource objects
  52   *
  53   * @var array
  54   * @access protected
  55   */
  56      var $_dataSources = array();
  57  /**
  58   * Contains a list of all file and class names used in Connection settings
  59   *
  60   * @var array
  61   * @access protected
  62   */
  63      var $_connectionsEnum = array();
  64  /**
  65   * Constructor.
  66   *
  67   */
  68  	function __construct() {
  69          if (class_exists('DATABASE_CONFIG')) {
  70              $this->config = new DATABASE_CONFIG();
  71          }
  72      }
  73  /**
  74   * Gets a reference to the ConnectionManger object instance
  75   *
  76   * @return object Instance of ConnectionManager
  77   * @access public
  78   */
  79      function &getInstance() {
  80          static $instance = array();
  81  
  82          if (!isset($instance[0]) || !$instance[0]) {
  83              $instance[0] = &new ConnectionManager();
  84          }
  85  
  86          return $instance[0];
  87      }
  88  /**
  89   * Gets a reference to a DataSource object
  90   *
  91   * @param string $name The name of the DataSource, as defined in app/config/database.php
  92   * @return object Instance of Dbo
  93   * @access public
  94   */
  95      function &getDataSource($name) {
  96          $_this =& ConnectionManager::getInstance();
  97  
  98          if (in_array($name, array_keys($_this->_dataSources))) {
  99              return $_this->_dataSources[$name];
 100          }
 101  
 102          $connections = $_this->enumConnectionObjects();
 103          if (in_array($name, array_keys($connections))) {
 104              $conn = $connections[$name];
 105              $class = $conn['classname'];
 106              $_this->loadDataSource($name);
 107              $_this->_dataSources[$name] =& new $class($_this->config->{$name});
 108              $_this->_dataSources[$name]->configKeyName = $name;
 109          } else {
 110              trigger_error("ConnectionManager::getDataSource - Non-existent data source {$name}", E_USER_ERROR);
 111              return null;
 112          }
 113  
 114          return $_this->_dataSources[$name];
 115      }
 116  /**
 117   * Gets a DataSource name from an object reference
 118   *
 119   * @param object $source
 120   * @return string
 121   * @access public
 122   */
 123  	function getSourceName(&$source) {
 124          $_this =& ConnectionManager::getInstance();
 125  
 126          $names = array_keys($_this->_dataSources);
 127          for ($i = 0; $i < count($names); $i++) {
 128              if ($_this->_dataSources[$names[$i]] === $source) {
 129                  return $names[$i];
 130              }
 131          }
 132          return null;
 133      }
 134  /**
 135   * Loads the DataSource class for the given connection name
 136   *
 137   * @param string $connName The name of the connection, as defined in Connections config
 138   * @return boolean True on success, false on failure or if the class is already loaded
 139   * @access public
 140   */
 141  	function loadDataSource($connName) {
 142  
 143          $_this =& ConnectionManager::getInstance();
 144          $connections = $_this->enumConnectionObjects();
 145          $conn = $connections[$connName];
 146  
 147          if (class_exists($conn['classname'])) {
 148              return false;
 149          }
 150  
 151          if(file_exists(MODELS . $conn['filename'] . '.php')) {
 152              require (MODELS . $conn['filename'] . '.php');
 153          } else if (fileExistsInPath(LIBS . 'model' . DS . $conn['filename'] . '.php')) {
 154              require (LIBS . 'model' . DS . $conn['filename'] . '.php');
 155          } else {
 156              trigger_error('Unable to load DataSource file ' . $conn['filename'] . '.php', E_USER_ERROR);
 157              return null;
 158          }
 159      }
 160  /**
 161   * Gets a list of class and file names associated with the user-defined DataSource connections
 162   *
 163   * @return array An associative array of elements where the key is the connection name
 164   *               (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
 165   * @access public
 166   */
 167  	function enumConnectionObjects() {
 168          $_this =& ConnectionManager::getInstance();
 169  
 170          if (!empty($_this->_connectionsEnum)) {
 171              return $_this->_connectionsEnum;
 172          }
 173          $connections = get_object_vars($_this->config);
 174  
 175          if ($connections != null) {
 176  
 177              foreach($connections as $name => $config) {
 178  
 179                  if (!isset($config['datasource'])) {
 180                      $config['datasource'] = 'dbo';
 181                  }
 182  
 183                  if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) {
 184                      $filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver'];
 185                      $classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver']));
 186                  } else {
 187                      $filename = $config['datasource'] . '_source';
 188                      $classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
 189                  }
 190                  $_this->_connectionsEnum[$name] = array('filename'  => $filename, 'classname' => $classname);
 191              }
 192              return $this->_connectionsEnum;
 193          } else {
 194              $this->cakeError('missingConnection', array(array('className' => 'ConnectionManager')));
 195          }
 196      }
 197  /**
 198   * Destructor.
 199   */
 200  	function __destruct() {
 201          if (CAKE_SESSION_SAVE == 'database' && function_exists('session_write_close')) {
 202              session_write_close();
 203          }
 204      }
 205  }
 206  ?>


Généré le : Sun Feb 25 19:27:47 2007 par Balluche grâce à PHPXref 0.7