2008-09-12 21:25:38 +04: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
|
2009-02-17 15:25:03 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\DBAL;
|
|
|
|
|
2009-05-07 17:54:01 +04:00
|
|
|
use Doctrine\Common\DoctrineException;
|
2009-01-22 22:38:10 +03:00
|
|
|
use Doctrine\Common\EventManager;
|
2008-09-12 21:25:38 +04:00
|
|
|
|
|
|
|
/**
|
2009-01-03 22:50:13 +03:00
|
|
|
* Factory for creating Doctrine\DBAL\Connection instances.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
final class DriverManager
|
2008-09-12 21:25:38 +04:00
|
|
|
{
|
|
|
|
/**
|
2009-05-03 15:49:48 +04:00
|
|
|
* List of supported drivers and their mappings to the driver classes.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2008-09-13 14:28:29 +04:00
|
|
|
private static $_driverMap = array(
|
2009-01-22 22:38:10 +03:00
|
|
|
'pdo_mysql' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
|
|
|
|
'pdo_sqlite' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
|
|
|
|
'pdo_pgsql' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
|
2009-05-07 17:54:01 +04:00
|
|
|
'pdo_oci' => 'Doctrine\DBAL\Driver\PDOOracle\Driver',
|
2009-01-22 22:38:10 +03:00
|
|
|
'pdo_mssql' => 'Doctrine\DBAL\Driver\PDOMsSql\Driver',
|
|
|
|
'pdo_firebird' => 'Doctrine\DBAL\Driver\PDOFirebird\Driver',
|
|
|
|
'pdo_informix' => 'Doctrine\DBAL\Driver\PDOInformix\Driver',
|
2008-09-12 21:25:38 +04:00
|
|
|
);
|
2009-01-03 22:50:13 +03:00
|
|
|
|
|
|
|
/** Private constructor. This class cannot be instantiated. */
|
2008-09-13 14:28:29 +04:00
|
|
|
private function __construct() {}
|
2009-02-20 08:46:20 +03:00
|
|
|
|
2008-09-12 21:25:38 +04:00
|
|
|
/**
|
2008-09-13 14:28:29 +04:00
|
|
|
* Creates a connection object based on the specified parameters.
|
2009-01-03 22:50:13 +03:00
|
|
|
* This method returns a Doctrine\DBAL\Connection which wraps the underlying
|
2008-09-13 14:28:29 +04:00
|
|
|
* driver connection.
|
2008-09-12 21:25:38 +04:00
|
|
|
*
|
2008-09-13 14:28:29 +04:00
|
|
|
* $params must contain at least one of the following.
|
|
|
|
*
|
|
|
|
* Either 'driver' with one of the following values:
|
|
|
|
* pdo_mysql
|
|
|
|
* pdo_sqlite
|
|
|
|
* pdo_pgsql
|
|
|
|
* pdo_oracle
|
|
|
|
* pdo_mssql
|
|
|
|
* pdo_firebird
|
|
|
|
* pdo_informix
|
|
|
|
*
|
|
|
|
* OR 'driverClass' that contains the full class name (with namespace) of the
|
|
|
|
* driver class to instantiate.
|
|
|
|
*
|
|
|
|
* Other (optional) parameters:
|
|
|
|
*
|
|
|
|
* <b>user (string)</b>:
|
|
|
|
* The username to use when connecting.
|
|
|
|
*
|
|
|
|
* <b>password (string)</b>:
|
|
|
|
* The password to use when connecting.
|
|
|
|
*
|
|
|
|
* <b>driverOptions (array)</b>:
|
|
|
|
* Any additional driver-specific options for the driver. These are just passed
|
|
|
|
* through to the driver.
|
|
|
|
*
|
|
|
|
* <b>pdo</b>:
|
|
|
|
* You can pass an existing PDO instance through this parameter. The PDO
|
2009-01-03 22:50:13 +03:00
|
|
|
* instance will be wrapped in a Doctrine\DBAL\Connection.
|
2008-09-13 14:28:29 +04:00
|
|
|
*
|
|
|
|
* <b>wrapperClass</b>:
|
|
|
|
* You may specify a custom wrapper class through the 'wrapperClass'
|
2009-01-03 22:50:13 +03:00
|
|
|
* parameter but this class MUST inherit from Doctrine\DBAL\Connection.
|
2008-09-13 14:28:29 +04:00
|
|
|
*
|
|
|
|
* @param array $params The parameters.
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param Doctrine\DBAL\Configuration The configuration to use.
|
|
|
|
* @param Doctrine\Common\EventManager The event manager to use.
|
|
|
|
* @return Doctrine\DBAL\Connection
|
2008-09-12 21:25:38 +04:00
|
|
|
*/
|
2008-12-18 17:08:11 +03:00
|
|
|
public static function getConnection(array $params,
|
2009-01-22 22:38:10 +03:00
|
|
|
Configuration $config = null,
|
|
|
|
EventManager $eventManager = null)
|
2008-09-12 21:25:38 +04:00
|
|
|
{
|
|
|
|
// create default config and event manager, if not set
|
|
|
|
if ( ! $config) {
|
2009-01-22 22:38:10 +03:00
|
|
|
$config = new Configuration();
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
if ( ! $eventManager) {
|
2009-01-22 22:38:10 +03:00
|
|
|
$eventManager = new EventManager();
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for existing pdo object
|
|
|
|
if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) {
|
2009-05-07 17:54:01 +04:00
|
|
|
throw DoctrineException::invalidPDOInstance();
|
2008-09-12 21:25:38 +04:00
|
|
|
} else if (isset($params['pdo'])) {
|
|
|
|
$params['driver'] = $params['pdo']->getAttribute(PDO::ATTR_DRIVER_NAME);
|
|
|
|
} else {
|
2008-09-13 14:28:29 +04:00
|
|
|
self::_checkParams($params);
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
if (isset($params['driverClass'])) {
|
|
|
|
$className = $params['driverClass'];
|
|
|
|
} else {
|
2008-09-13 14:28:29 +04:00
|
|
|
$className = self::$_driverMap[$params['driver']];
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$driver = new $className();
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
$wrapperClass = 'Doctrine\DBAL\Connection';
|
2008-09-13 14:28:29 +04:00
|
|
|
if (isset($params['wrapperClass']) && is_subclass_of($params['wrapperClass'], $wrapperClass)) {
|
2008-09-12 21:25:38 +04:00
|
|
|
$wrapperClass = $params['wrapperClass'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $wrapperClass($params, $driver, $config, $eventManager);
|
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
|
2008-09-12 21:25:38 +04:00
|
|
|
/**
|
|
|
|
* Checks the list of parameters.
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
2008-09-13 14:28:29 +04:00
|
|
|
private static function _checkParams(array $params)
|
2008-09-12 21:25:38 +04:00
|
|
|
{
|
|
|
|
// check existance of mandatory parameters
|
|
|
|
|
|
|
|
// driver
|
|
|
|
if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
|
2009-05-07 17:54:01 +04:00
|
|
|
throw DoctrineException::driverRequired();
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// check validity of parameters
|
|
|
|
|
|
|
|
// driver
|
2008-09-13 14:28:29 +04:00
|
|
|
if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
|
2009-05-07 17:54:01 +04:00
|
|
|
throw DoctrineException::unknownDriver($params['driver']);
|
2008-09-12 21:25:38 +04:00
|
|
|
}
|
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|