DriverManager.php 5.47 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?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.org>.
 */

22
#namespace Doctrine\DBAL;
romanb's avatar
romanb committed
23 24

/**
25
 * Factory for creating Doctrine\DBAL\Connection instances.
romanb's avatar
romanb committed
26 27 28 29
 *
 * @author Roman Borschel <roman@code-factory.org>
 * @since 2.0
 */
30
final class Doctrine_DBAL_DriverManager
romanb's avatar
romanb committed
31 32 33 34 35 36
{
    /**
     * List of supported drivers and their mappings to the driver class.
     *
     * @var array
     */
37
     private static $_driverMap = array(
romanb's avatar
romanb committed
38 39 40 41 42 43 44 45
            'pdo_mysql'  => 'Doctrine_DBAL_Driver_PDOMySql_Driver',
            'pdo_sqlite' => 'Doctrine_DBAL_Driver_PDOSqlite_Driver',
            'pdo_pgsql'  => 'Doctrine_DBAL_Driver_PDOPgSql_Driver',
            'pdo_oracle' => 'Doctrine_DBAL_Driver_PDOOracle_Driver',
            'pdo_mssql'  => 'Doctrine_DBAL_Driver_PDOMsSql_Driver',
            'pdo_firebird' => 'Doctrine_DBAL_Driver_PDOFirebird_Driver',
            'pdo_informix' => 'Doctrine_DBAL_Driver_PDOInformix_Driver',
            );
46 47

    /** Private constructor. This class cannot be instantiated. */
48 49
    private function __construct() {}
            
romanb's avatar
romanb committed
50
    /**
51
     * Creates a connection object based on the specified parameters.
52
     * This method returns a Doctrine\DBAL\Connection which wraps the underlying
53
     * driver connection.
romanb's avatar
romanb committed
54
     *
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
     * $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
83
     * instance will be wrapped in a Doctrine\DBAL\Connection.
84 85 86
     * 
     * <b>wrapperClass</b>:
     * You may specify a custom wrapper class through the 'wrapperClass'
87
     * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
88 89
     * 
     * @param array $params The parameters.
90 91 92
     * @param Doctrine\DBAL\Configuration The configuration to use.
     * @param Doctrine\Common\EventManager The event manager to use.
     * @return Doctrine\DBAL\Connection
romanb's avatar
romanb committed
93
     */
94
    public static function getConnection(array $params,
95
            Doctrine_DBAL_Configuration $config = null,
romanb's avatar
romanb committed
96 97 98 99
            Doctrine_Common_EventManager $eventManager = null)
    {
        // create default config and event manager, if not set
        if ( ! $config) {
100
            $config = new Doctrine_DBAL_Configuration();
romanb's avatar
romanb committed
101 102 103 104 105 106 107 108 109 110 111
        }
        if ( ! $eventManager) {
            $eventManager = new Doctrine_Common_EventManager();
        }
        
        // check for existing pdo object
        if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) {
            throw Doctrine_DBAL_Exceptions_DBALException::invalidPDOInstance();
        } else if (isset($params['pdo'])) {
            $params['driver'] = $params['pdo']->getAttribute(PDO::ATTR_DRIVER_NAME);
        } else {
112
            self::_checkParams($params);
romanb's avatar
romanb committed
113 114 115 116
        }
        if (isset($params['driverClass'])) {
            $className = $params['driverClass'];
        } else {
117
            $className = self::$_driverMap[$params['driver']];
romanb's avatar
romanb committed
118 119 120 121 122
        }
        
        $driver = new $className();
        
        $wrapperClass = 'Doctrine_DBAL_Connection';
123
        if (isset($params['wrapperClass']) && is_subclass_of($params['wrapperClass'], $wrapperClass)) {
romanb's avatar
romanb committed
124 125 126 127 128 129 130 131 132 133 134
            $wrapperClass = $params['wrapperClass'];
        }
        
        return new $wrapperClass($params, $driver, $config, $eventManager);
    }
    
    /**
     * Checks the list of parameters.
     *
     * @param array $params
     */
135
    private static function _checkParams(array $params)
romanb's avatar
romanb committed
136 137 138 139 140 141 142 143 144 145 146
    {        
        // check existance of mandatory parameters
        
        // driver
        if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
            throw Doctrine_ConnectionFactory_Exception::driverRequired();
        }
        
        // check validity of parameters
        
        // driver
147
        if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
romanb's avatar
romanb committed
148 149 150 151 152 153
            throw Doctrine_DBAL_Exceptions_DBALException::unknownDriver($params['driver']);
        }
    }
}

?>