2006-12-29 17:01:31 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
*
|
2006-12-29 17:40:47 +03:00
|
|
|
* Doctrine_Manager is the base component of all doctrine based projects.
|
2006-12-29 17:01:31 +03:00
|
|
|
* It opens and keeps track of all connections (database connections).
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Manager
|
2006-12-29 17:01:31 +03:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
class Doctrine_Manager extends Doctrine_Configurable implements Countable, IteratorAggregate
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
2007-09-18 01:51:42 +04:00
|
|
|
* @var array $connections an array containing all the opened connections
|
2006-12-29 17:01:31 +03:00
|
|
|
*/
|
2007-05-17 02:45:13 +04:00
|
|
|
protected $_connections = array();
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
2007-09-18 01:51:42 +04:00
|
|
|
* @var array $bound an array containing all components that have a bound connection
|
2006-12-29 17:01:31 +03:00
|
|
|
*/
|
2007-06-20 03:33:04 +04:00
|
|
|
protected $_bound = array();
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
2007-09-18 01:51:42 +04:00
|
|
|
* @var integer $index the incremented index
|
2006-12-29 17:01:31 +03:00
|
|
|
*/
|
2007-06-20 03:33:04 +04:00
|
|
|
protected $_index = 0;
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
2007-09-18 01:51:42 +04:00
|
|
|
* @var integer $currIndex the current connection index
|
2006-12-29 17:01:31 +03:00
|
|
|
*/
|
2007-06-20 03:33:04 +04:00
|
|
|
protected $_currIndex = 0;
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
2007-09-18 01:51:42 +04:00
|
|
|
* @var string $root root directory
|
2006-12-29 17:01:31 +03:00
|
|
|
*/
|
2007-05-17 02:45:13 +04:00
|
|
|
protected $_root;
|
2007-01-26 00:40:40 +03:00
|
|
|
/**
|
2007-09-18 01:51:42 +04:00
|
|
|
* @var Doctrine_Query_Registry the query registry
|
2007-01-26 00:40:40 +03:00
|
|
|
*/
|
2007-09-18 01:51:42 +04:00
|
|
|
protected $_queryRegistry;
|
2007-06-25 14:16:54 +04:00
|
|
|
|
2007-06-25 14:21:57 +04:00
|
|
|
protected static $driverMap = array('oci' => 'oracle');
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*
|
|
|
|
* this is private constructor (use getInstance to get an instance of this class)
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
private function __construct()
|
|
|
|
{
|
2007-05-17 02:45:13 +04:00
|
|
|
$this->_root = dirname(__FILE__);
|
2006-12-29 17:01:31 +03:00
|
|
|
|
2007-10-08 00:58:08 +04:00
|
|
|
Doctrine_Locator_Injectable::initNullObject(new Doctrine_Null);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* setDefaultAttributes
|
|
|
|
* sets default attributes
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-05-17 02:45:13 +04:00
|
|
|
public function setDefaultAttributes()
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
static $init = false;
|
|
|
|
if ( ! $init) {
|
|
|
|
$init = true;
|
|
|
|
$attributes = array(
|
2007-06-29 14:18:05 +04:00
|
|
|
Doctrine::ATTR_CACHE => null,
|
2007-05-30 14:20:21 +04:00
|
|
|
Doctrine::ATTR_LOAD_REFERENCES => true,
|
2006-12-29 17:01:31 +03:00
|
|
|
Doctrine::ATTR_LISTENER => new Doctrine_EventListener(),
|
2007-07-11 18:39:15 +04:00
|
|
|
Doctrine::ATTR_RECORD_LISTENER => new Doctrine_Record_Listener(),
|
2007-07-12 02:03:47 +04:00
|
|
|
Doctrine::ATTR_THROW_EXCEPTIONS => true,
|
2007-09-03 00:36:58 +04:00
|
|
|
Doctrine::ATTR_VALIDATE => Doctrine::VALIDATE_NONE,
|
2006-12-29 17:01:31 +03:00
|
|
|
Doctrine::ATTR_QUERY_LIMIT => Doctrine::LIMIT_RECORDS,
|
|
|
|
Doctrine::ATTR_IDXNAME_FORMAT => "%s_idx",
|
|
|
|
Doctrine::ATTR_SEQNAME_FORMAT => "%s_seq",
|
|
|
|
Doctrine::ATTR_QUOTE_IDENTIFIER => false,
|
|
|
|
Doctrine::ATTR_SEQCOL_NAME => 'id',
|
|
|
|
Doctrine::ATTR_PORTABILITY => Doctrine::PORTABILITY_ALL,
|
2007-07-08 23:33:57 +04:00
|
|
|
Doctrine::ATTR_EXPORT => Doctrine::EXPORT_ALL,
|
2007-05-11 23:13:20 +04:00
|
|
|
Doctrine::ATTR_DECIMAL_PLACES => 2,
|
2006-12-29 17:01:31 +03:00
|
|
|
);
|
|
|
|
foreach ($attributes as $attribute => $value) {
|
|
|
|
$old = $this->getAttribute($attribute);
|
|
|
|
if ($old === null) {
|
|
|
|
$this->setAttribute($attribute,$value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns the root directory of Doctrine
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function getRoot()
|
|
|
|
{
|
2007-05-17 02:45:13 +04:00
|
|
|
return $this->_root;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getInstance
|
|
|
|
* returns an instance of this class
|
|
|
|
* (this class uses the singleton pattern)
|
|
|
|
*
|
|
|
|
* @return Doctrine_Manager
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public static function getInstance()
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
static $instance;
|
|
|
|
if ( ! isset($instance)) {
|
|
|
|
$instance = new self();
|
|
|
|
}
|
|
|
|
return $instance;
|
|
|
|
}
|
2007-09-18 01:51:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getQueryRegistry
|
|
|
|
* lazy-initializes the query registry object and returns it
|
|
|
|
*
|
|
|
|
* @return Doctrine_Query_Registry
|
|
|
|
*/
|
|
|
|
public function getQueryRegistry()
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_queryRegistry)) {
|
2007-09-18 02:26:25 +04:00
|
|
|
$this->_queryRegistry = new Doctrine_Query_Registry;
|
2007-09-18 01:51:42 +04:00
|
|
|
}
|
|
|
|
return $this->_queryRegistry;
|
|
|
|
}
|
2007-09-18 02:26:25 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* setQueryRegistry
|
|
|
|
* sets the query registry
|
|
|
|
*
|
|
|
|
* @return Doctrine_Manager this object
|
|
|
|
*/
|
2007-09-18 02:30:45 +04:00
|
|
|
public function setQueryRegistry(Doctrine_Query_Registry $registry)
|
2007-09-18 02:26:25 +04:00
|
|
|
{
|
|
|
|
$this->_queryRegistry = $registry;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2007-09-18 21:26:17 +04:00
|
|
|
/**
|
|
|
|
* fetch
|
|
|
|
* fetches data using the provided queryKey and
|
|
|
|
* the associated query in the query registry
|
|
|
|
*
|
|
|
|
* if no query for given queryKey is being found a
|
|
|
|
* Doctrine_Query_Registry exception is being thrown
|
|
|
|
*
|
|
|
|
* @param string $queryKey the query key
|
|
|
|
* @param array $params prepared statement params (if any)
|
|
|
|
* @return mixed the fetched data
|
|
|
|
*/
|
|
|
|
public function find($queryKey, $params = array(), $hydrationMode = Doctrine::HYDRATE_RECORD)
|
|
|
|
{
|
|
|
|
return Doctrine_Manager::getInstance()
|
|
|
|
->getQueryRegistry()
|
|
|
|
->get($queryKey)
|
|
|
|
->execute($params, $hydrationMode);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* fetchOne
|
|
|
|
* fetches data using the provided queryKey and
|
|
|
|
* the associated query in the query registry
|
|
|
|
*
|
|
|
|
* if no query for given queryKey is being found a
|
|
|
|
* Doctrine_Query_Registry exception is being thrown
|
|
|
|
*
|
|
|
|
* @param string $queryKey the query key
|
|
|
|
* @param array $params prepared statement params (if any)
|
|
|
|
* @return mixed the fetched data
|
|
|
|
*/
|
|
|
|
public function findOne($queryKey, $params = array(), $hydrationMode = Doctrine::HYDRATE_RECORD)
|
|
|
|
{
|
|
|
|
return Doctrine_Manager::getInstance()
|
|
|
|
->getQueryRegistry()
|
|
|
|
->get($queryKey)
|
|
|
|
->fetchOne($params, $hydrationMode);
|
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
|
|
|
* connection
|
2006-12-29 17:40:47 +03:00
|
|
|
*
|
2006-12-29 17:01:31 +03:00
|
|
|
* if the adapter parameter is set this method acts as
|
|
|
|
* a short cut for Doctrine_Manager::getInstance()->openConnection($adapter, $name);
|
|
|
|
*
|
2006-12-29 17:40:47 +03:00
|
|
|
* if the adapter paramater is not set this method acts as
|
2006-12-29 17:01:31 +03:00
|
|
|
* a short cut for Doctrine_Manager::getInstance()->getCurrentConnection()
|
|
|
|
*
|
|
|
|
* @param PDO|Doctrine_Adapter_Interface $adapter database driver
|
|
|
|
* @param string $name name of the connection, if empty numeric key is used
|
|
|
|
* @throws Doctrine_Manager_Exception if trying to bind a connection with an existing name
|
|
|
|
* @return Doctrine_Connection
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public static function connection($adapter = null, $name = null)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($adapter == null) {
|
|
|
|
return Doctrine_Manager::getInstance()->getCurrentConnection();
|
|
|
|
} else {
|
|
|
|
return Doctrine_Manager::getInstance()->openConnection($adapter, $name);
|
|
|
|
}
|
2006-12-29 17:40:47 +03:00
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
|
|
|
* openConnection
|
|
|
|
* opens a new connection and saves it to Doctrine_Manager->connections
|
|
|
|
*
|
|
|
|
* @param PDO|Doctrine_Adapter_Interface $adapter database driver
|
|
|
|
* @param string $name name of the connection, if empty numeric key is used
|
|
|
|
* @throws Doctrine_Manager_Exception if trying to bind a connection with an existing name
|
2007-01-26 00:40:40 +03:00
|
|
|
* @throws Doctrine_Manager_Exception if trying to open connection for unknown driver
|
2006-12-29 17:01:31 +03:00
|
|
|
* @return Doctrine_Connection
|
|
|
|
*/
|
2007-01-19 20:00:25 +03:00
|
|
|
public function openConnection($adapter, $name = null, $setCurrent = true)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if (is_object($adapter)) {
|
2007-06-20 03:33:04 +04:00
|
|
|
if ( ! ($adapter instanceof PDO) && ! in_array('Doctrine_Adapter_Interface', class_implements($adapter))) {
|
|
|
|
throw new Doctrine_Manager_Exception("First argument should be an instance of PDO or implement Doctrine_Adapter_Interface");
|
|
|
|
}
|
|
|
|
|
|
|
|
$driverName = $adapter->getAttribute(Doctrine::ATTR_DRIVER_NAME);
|
|
|
|
} elseif (is_array($adapter)) {
|
|
|
|
if ( ! isset($adapter[0])) {
|
|
|
|
throw new Doctrine_Manager_Exception('Empty data source name given.');
|
|
|
|
}
|
|
|
|
$e = explode(':', $adapter[0]);
|
|
|
|
|
2007-09-03 18:57:18 +04:00
|
|
|
if ($e[0] == 'uri') {
|
2007-06-20 03:33:04 +04:00
|
|
|
$e[0] = 'odbc';
|
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
|
2007-06-20 03:33:04 +04:00
|
|
|
$parts['dsn'] = $adapter[0];
|
|
|
|
$parts['scheme'] = $e[0];
|
|
|
|
$parts['user'] = (isset($adapter[1])) ? $adapter[1] : null;
|
|
|
|
$parts['pass'] = (isset($adapter[2])) ? $adapter[2] : null;
|
|
|
|
|
|
|
|
$driverName = $e[0];
|
|
|
|
$adapter = $parts;
|
|
|
|
} else {
|
|
|
|
$parts = $this->parseDsn($adapter);
|
|
|
|
|
|
|
|
$driverName = $parts['scheme'];
|
|
|
|
|
|
|
|
$adapter = $parts;
|
2007-02-02 01:46:59 +03:00
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
// initialize the default attributes
|
|
|
|
$this->setDefaultAttributes();
|
|
|
|
|
|
|
|
if ($name !== null) {
|
|
|
|
$name = (string) $name;
|
2007-05-17 02:45:13 +04:00
|
|
|
if (isset($this->_connections[$name])) {
|
|
|
|
return $this->_connections[$name];
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
} else {
|
2007-05-17 02:45:13 +04:00
|
|
|
$name = $this->_index;
|
|
|
|
$this->_index++;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-01-26 00:40:40 +03:00
|
|
|
|
2007-06-20 03:33:04 +04:00
|
|
|
$drivers = array('mysql' => 'Doctrine_Connection_Mysql',
|
|
|
|
'sqlite' => 'Doctrine_Connection_Sqlite',
|
|
|
|
'pgsql' => 'Doctrine_Connection_Pgsql',
|
|
|
|
'oci' => 'Doctrine_Connection_Oracle',
|
|
|
|
'oci8' => 'Doctrine_Connection_Oracle',
|
|
|
|
'oracle' => 'Doctrine_Connection_Oracle',
|
|
|
|
'mssql' => 'Doctrine_Connection_Mssql',
|
|
|
|
'dblib' => 'Doctrine_Connection_Mssql',
|
|
|
|
'firebird' => 'Doctrine_Connection_Firebird',
|
|
|
|
'informix' => 'Doctrine_Connection_Informix',
|
2007-10-08 19:58:23 +04:00
|
|
|
'mock' => 'Doctrine_Connection_Mock',
|
|
|
|
'resource' => 'Doctrine_Connection_Resource');
|
2007-06-20 03:33:04 +04:00
|
|
|
if ( ! isset($drivers[$driverName])) {
|
|
|
|
throw new Doctrine_Manager_Exception('Unknown driver ' . $driverName);
|
|
|
|
}
|
|
|
|
$className = $drivers[$driverName];
|
|
|
|
$conn = new $className($this, $adapter);
|
|
|
|
|
|
|
|
$this->_connections[$name] = $conn;
|
|
|
|
|
|
|
|
if ($setCurrent) {
|
|
|
|
$this->_currIndex = $name;
|
|
|
|
}
|
|
|
|
return $this->_connections[$name];
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* parseDsn
|
|
|
|
*
|
|
|
|
* @param string $dsn
|
|
|
|
* @return array Parsed contents of DSN
|
|
|
|
*/
|
|
|
|
public function parseDsn($dsn)
|
|
|
|
{
|
|
|
|
// silence any warnings
|
|
|
|
$parts = @parse_url($dsn);
|
2007-10-08 19:58:23 +04:00
|
|
|
|
2007-06-20 03:33:04 +04:00
|
|
|
$names = array('dsn', 'scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment');
|
|
|
|
|
|
|
|
foreach ($names as $name) {
|
|
|
|
if ( ! isset($parts[$name])) {
|
|
|
|
$parts[$name] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($parts) == 0 || ! isset($parts['scheme'])) {
|
|
|
|
throw new Doctrine_Manager_Exception('Empty data source name');
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($parts['scheme']) {
|
2007-10-08 19:58:23 +04:00
|
|
|
case 'resource':
|
2006-12-30 00:30:37 +03:00
|
|
|
case 'sqlite':
|
2007-06-20 03:33:04 +04:00
|
|
|
case 'sqlite2':
|
|
|
|
case 'sqlite3':
|
|
|
|
if (isset($parts['host']) && $parts['host'] == ':memory') {
|
|
|
|
$parts['database'] = ':memory:';
|
|
|
|
$parts['dsn'] = 'sqlite::memory:';
|
2007-08-30 01:57:46 +04:00
|
|
|
} else {
|
|
|
|
$parts['database'] = $parts['path'];
|
2007-09-03 00:36:58 +04:00
|
|
|
$parts['dsn'] = $parts['scheme'] . ':' . $parts['path'];
|
2007-06-20 03:33:04 +04:00
|
|
|
}
|
|
|
|
|
2006-12-30 00:30:37 +03:00
|
|
|
break;
|
2007-06-20 03:33:04 +04:00
|
|
|
case 'mysql':
|
|
|
|
case 'informix':
|
2007-06-05 21:56:04 +04:00
|
|
|
case 'oci8':
|
2007-06-20 03:33:04 +04:00
|
|
|
case 'oci':
|
2006-12-30 00:30:37 +03:00
|
|
|
case 'mssql':
|
|
|
|
case 'firebird':
|
2007-06-20 03:33:04 +04:00
|
|
|
case 'dblib':
|
|
|
|
case 'pgsql':
|
|
|
|
case 'odbc':
|
2006-12-30 00:30:37 +03:00
|
|
|
case 'mock':
|
2007-06-20 03:33:04 +04:00
|
|
|
case 'oracle':
|
|
|
|
if ( ! isset($parts['path']) || $parts['path'] == '/') {
|
2007-06-26 15:35:58 +04:00
|
|
|
throw new Doctrine_Manager_Exception('No database available in data source name');
|
2007-06-20 03:33:04 +04:00
|
|
|
}
|
|
|
|
if (isset($parts['path'])) {
|
|
|
|
$parts['database'] = substr($parts['path'], 1);
|
|
|
|
}
|
|
|
|
if ( ! isset($parts['host'])) {
|
|
|
|
throw new Doctrine_Manager_Exception('No hostname set in data source name');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset(self::$driverMap[$parts['scheme']])) {
|
|
|
|
$parts['scheme'] = self::$driverMap[$parts['scheme']];
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts['dsn'] = $parts['scheme'] . ':host='
|
|
|
|
. $parts['host'] . ';dbname='
|
|
|
|
. $parts['database'];
|
|
|
|
|
|
|
|
if (isset($parts['port'])) {
|
|
|
|
// append port to dsn if supplied
|
|
|
|
$parts['dsn'] .= ';port=' . $parts['port'];
|
|
|
|
}
|
2006-12-30 00:30:37 +03:00
|
|
|
break;
|
|
|
|
default:
|
2007-06-25 14:08:03 +04:00
|
|
|
throw new Doctrine_Manager_Exception('Unknown driver '.$parts['scheme']);
|
2007-01-19 20:00:25 +03:00
|
|
|
}
|
2007-06-20 03:33:04 +04:00
|
|
|
|
|
|
|
|
|
|
|
return $parts;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getConnection
|
|
|
|
* @param integer $index
|
|
|
|
* @return object Doctrine_Connection
|
|
|
|
* @throws Doctrine_Manager_Exception if trying to get a non-existent connection
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getConnection($name)
|
|
|
|
{
|
2007-05-17 02:45:13 +04:00
|
|
|
if ( ! isset($this->_connections[$name])) {
|
2006-12-29 17:01:31 +03:00
|
|
|
throw new Doctrine_Manager_Exception('Unknown connection: ' . $name);
|
|
|
|
}
|
2007-02-15 16:30:05 +03:00
|
|
|
|
2007-05-17 02:45:13 +04:00
|
|
|
return $this->_connections[$name];
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-05-11 23:13:20 +04:00
|
|
|
/**
|
2007-03-09 16:58:22 +03:00
|
|
|
* getComponentAlias
|
2007-05-11 23:13:20 +04:00
|
|
|
* retrieves the alias for given component name
|
2007-03-09 16:58:22 +03:00
|
|
|
* if the alias couldn't be found, this method returns the given
|
|
|
|
* component name
|
|
|
|
*
|
|
|
|
* @param string $componentName
|
|
|
|
* @return string the component alias
|
|
|
|
*/
|
|
|
|
public function getComponentAlias($componentName)
|
|
|
|
{
|
|
|
|
if (isset($this->componentAliases[$componentName])) {
|
|
|
|
return $this->componentAliases[$componentName];
|
|
|
|
}
|
2007-05-11 23:13:20 +04:00
|
|
|
|
2007-03-09 16:58:22 +03:00
|
|
|
return $componentName;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* sets an alias for given component name
|
|
|
|
* very useful when building a large framework with a possibility
|
|
|
|
* to override any given class
|
|
|
|
*
|
|
|
|
* @param string $componentName the name of the component
|
|
|
|
* @param string $alias
|
|
|
|
* @return Doctrine_Manager
|
|
|
|
*/
|
2007-05-11 23:13:20 +04:00
|
|
|
public function setComponentAlias($componentName, $alias)
|
2007-03-09 16:58:22 +03:00
|
|
|
{
|
|
|
|
$this->componentAliases[$componentName] = $alias;
|
2007-05-11 23:13:20 +04:00
|
|
|
|
2007-03-09 16:58:22 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2007-04-02 20:50:35 +04:00
|
|
|
/**
|
|
|
|
* getConnectionName
|
|
|
|
*
|
2007-04-02 20:54:46 +04:00
|
|
|
* @param Doctrine_Connection $conn connection object to be searched for
|
|
|
|
* @return string the name of the connection
|
2007-04-02 20:50:35 +04:00
|
|
|
*/
|
2007-05-11 23:13:20 +04:00
|
|
|
public function getConnectionName(Doctrine_Connection $conn)
|
2007-04-02 20:50:35 +04:00
|
|
|
{
|
2007-05-17 02:45:13 +04:00
|
|
|
return array_search($conn, $this->_connections, true);
|
2007-04-02 20:50:35 +04:00
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
|
|
|
* bindComponent
|
|
|
|
* binds given component to given connection
|
|
|
|
* this means that when ever the given component uses a connection
|
|
|
|
* it will be using the bound connection instead of the current connection
|
|
|
|
*
|
|
|
|
* @param string $componentName
|
|
|
|
* @param string $connectionName
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function bindComponent($componentName, $connectionName)
|
|
|
|
{
|
2007-05-17 02:45:13 +04:00
|
|
|
$this->_bound[$componentName] = $connectionName;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getConnectionForComponent
|
|
|
|
*
|
|
|
|
* @param string $componentName
|
|
|
|
* @return Doctrine_Connection
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getConnectionForComponent($componentName = null)
|
|
|
|
{
|
2007-05-17 02:45:13 +04:00
|
|
|
if (isset($this->_bound[$componentName])) {
|
|
|
|
return $this->getConnection($this->_bound[$componentName]);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
return $this->getCurrentConnection();
|
|
|
|
}
|
2006-12-29 17:40:47 +03:00
|
|
|
/**
|
2006-12-29 17:01:31 +03:00
|
|
|
* getTable
|
|
|
|
* this is the same as Doctrine_Connection::getTable() except
|
|
|
|
* that it works seamlessly in multi-server/connection environment
|
|
|
|
*
|
|
|
|
* @see Doctrine_Connection::getTable()
|
|
|
|
* @param string $componentName
|
|
|
|
* @return Doctrine_Table
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getTable($componentName)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
return $this->getConnectionForComponent($componentName)->getTable($componentName);
|
|
|
|
}
|
2007-07-23 22:32:29 +04:00
|
|
|
/**
|
|
|
|
* table
|
|
|
|
* this is the same as Doctrine_Connection::getTable() except
|
|
|
|
* that it works seamlessly in multi-server/connection environment
|
|
|
|
*
|
|
|
|
* @see Doctrine_Connection::getTable()
|
|
|
|
* @param string $componentName
|
|
|
|
* @return Doctrine_Table
|
|
|
|
*/
|
|
|
|
public static function table($componentName)
|
|
|
|
{
|
|
|
|
return Doctrine_Manager::getInstance()
|
|
|
|
->getConnectionForComponent($componentName)
|
|
|
|
->getTable($componentName);
|
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
|
|
|
* closes the connection
|
|
|
|
*
|
|
|
|
* @param Doctrine_Connection $connection
|
|
|
|
* @return void
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function closeConnection(Doctrine_Connection $connection)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
$connection->close();
|
2007-05-11 23:13:20 +04:00
|
|
|
|
2007-05-17 02:45:13 +04:00
|
|
|
$key = array_search($connection, $this->_connections, true);
|
2007-04-24 20:59:07 +04:00
|
|
|
|
|
|
|
if ($key !== false) {
|
2007-05-17 02:45:13 +04:00
|
|
|
unset($this->_connections[$key]);
|
2007-04-24 20:59:07 +04:00
|
|
|
}
|
2007-05-17 02:45:13 +04:00
|
|
|
$this->_currIndex = key($this->_connections);
|
2007-04-24 20:59:07 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
unset($connection);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getConnections
|
|
|
|
* returns all opened connections
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getConnections()
|
|
|
|
{
|
2007-05-17 02:45:13 +04:00
|
|
|
return $this->_connections;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* setCurrentConnection
|
|
|
|
* sets the current connection to $key
|
|
|
|
*
|
|
|
|
* @param mixed $key the connection key
|
|
|
|
* @throws InvalidKeyException
|
|
|
|
* @return void
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function setCurrentConnection($key)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
$key = (string) $key;
|
2007-05-17 02:45:13 +04:00
|
|
|
if ( ! isset($this->_connections[$key])) {
|
2006-12-29 17:01:31 +03:00
|
|
|
throw new InvalidKeyException();
|
|
|
|
}
|
2007-05-17 02:45:13 +04:00
|
|
|
$this->_currIndex = $key;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-01-26 00:40:40 +03:00
|
|
|
/**
|
|
|
|
* contains
|
|
|
|
* whether or not the manager contains specified connection
|
|
|
|
*
|
|
|
|
* @param mixed $key the connection key
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-05-11 23:13:20 +04:00
|
|
|
public function contains($key)
|
2007-01-26 00:40:40 +03:00
|
|
|
{
|
2007-05-17 02:45:13 +04:00
|
|
|
return isset($this->_connections[$key]);
|
2007-01-26 00:40:40 +03:00
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
/**
|
|
|
|
* count
|
|
|
|
* returns the number of opened connections
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function count()
|
|
|
|
{
|
2007-05-17 02:45:13 +04:00
|
|
|
return count($this->_connections);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getIterator
|
|
|
|
* returns an ArrayIterator that iterates through all connections
|
|
|
|
*
|
|
|
|
* @return ArrayIterator
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getIterator()
|
|
|
|
{
|
2007-05-17 02:45:13 +04:00
|
|
|
return new ArrayIterator($this->_connections);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getCurrentConnection
|
|
|
|
* returns the current connection
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if there are no open connections
|
|
|
|
* @return Doctrine_Connection
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getCurrentConnection()
|
|
|
|
{
|
2007-05-17 02:45:13 +04:00
|
|
|
$i = $this->_currIndex;
|
|
|
|
if ( ! isset($this->_connections[$i])) {
|
2006-12-29 17:01:31 +03:00
|
|
|
throw new Doctrine_Connection_Exception();
|
|
|
|
}
|
2007-05-17 02:45:13 +04:00
|
|
|
return $this->_connections[$i];
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* __toString
|
|
|
|
* returns a string representation of this object
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function __toString()
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
$r[] = "<pre>";
|
|
|
|
$r[] = "Doctrine_Manager";
|
2007-05-17 02:45:13 +04:00
|
|
|
$r[] = "Connections : ".count($this->_connections);
|
2006-12-29 17:01:31 +03:00
|
|
|
$r[] = "</pre>";
|
|
|
|
return implode("\n",$r);
|
|
|
|
}
|
2007-10-08 00:58:08 +04:00
|
|
|
}
|