DriverManager.php 5.39 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
<?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
19
 * <http://www.doctrine-project.org>.
romanb's avatar
romanb committed
20 21
 */

22 23
namespace Doctrine\DBAL;

24
use Doctrine\Common\DoctrineException;
25
use Doctrine\Common\EventManager;
romanb's avatar
romanb committed
26 27

/**
28
 * Factory for creating Doctrine\DBAL\Connection instances.
romanb's avatar
romanb committed
29 30 31 32
 *
 * @author Roman Borschel <roman@code-factory.org>
 * @since 2.0
 */
33
final class DriverManager
romanb's avatar
romanb committed
34 35
{
    /**
36
     * List of supported drivers and their mappings to the driver classes.
romanb's avatar
romanb committed
37 38 39
     *
     * @var array
     */
40
     private static $_driverMap = array(
41 42 43
            'pdo_mysql'  => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
            'pdo_sqlite' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
            'pdo_pgsql'  => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
44
            'pdo_oci' => 'Doctrine\DBAL\Driver\PDOOracle\Driver',
45 46 47
            'pdo_mssql'  => 'Doctrine\DBAL\Driver\PDOMsSql\Driver',
            'pdo_firebird' => 'Doctrine\DBAL\Driver\PDOFirebird\Driver',
            'pdo_informix' => 'Doctrine\DBAL\Driver\PDOInformix\Driver',
romanb's avatar
romanb committed
48
            );
49 50

    /** Private constructor. This class cannot be instantiated. */
51
    private function __construct() {}
52

romanb's avatar
romanb committed
53
    /**
54
     * Creates a connection object based on the specified parameters.
55
     * This method returns a Doctrine\DBAL\Connection which wraps the underlying
56
     * driver connection.
romanb's avatar
romanb committed
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 83 84 85
     * $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
86
     * instance will be wrapped in a Doctrine\DBAL\Connection.
87 88 89
     * 
     * <b>wrapperClass</b>:
     * You may specify a custom wrapper class through the 'wrapperClass'
90
     * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
91 92
     * 
     * @param array $params The parameters.
93 94 95
     * @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
96
     */
97
    public static function getConnection(array $params,
98 99
            Configuration $config = null,
            EventManager $eventManager = null)
romanb's avatar
romanb committed
100 101 102
    {
        // create default config and event manager, if not set
        if ( ! $config) {
103
            $config = new Configuration();
romanb's avatar
romanb committed
104 105
        }
        if ( ! $eventManager) {
106
            $eventManager = new EventManager();
romanb's avatar
romanb committed
107 108 109 110
        }
        
        // check for existing pdo object
        if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) {
111
            throw DoctrineException::invalidPDOInstance();
romanb's avatar
romanb committed
112 113 114
        } else if (isset($params['pdo'])) {
            $params['driver'] = $params['pdo']->getAttribute(PDO::ATTR_DRIVER_NAME);
        } else {
115
            self::_checkParams($params);
romanb's avatar
romanb committed
116 117 118 119
        }
        if (isset($params['driverClass'])) {
            $className = $params['driverClass'];
        } else {
120
            $className = self::$_driverMap[$params['driver']];
romanb's avatar
romanb committed
121 122 123 124
        }
        
        $driver = new $className();
        
125
        $wrapperClass = 'Doctrine\DBAL\Connection';
126
        if (isset($params['wrapperClass']) && is_subclass_of($params['wrapperClass'], $wrapperClass)) {
romanb's avatar
romanb committed
127 128 129 130 131
            $wrapperClass = $params['wrapperClass'];
        }
        
        return new $wrapperClass($params, $driver, $config, $eventManager);
    }
132

romanb's avatar
romanb committed
133 134 135 136 137
    /**
     * Checks the list of parameters.
     *
     * @param array $params
     */
138
    private static function _checkParams(array $params)
romanb's avatar
romanb committed
139 140 141 142 143
    {        
        // check existance of mandatory parameters
        
        // driver
        if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
144
            throw DoctrineException::driverRequired();
romanb's avatar
romanb committed
145 146 147 148 149
        }
        
        // check validity of parameters
        
        // driver
150
        if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
151
            throw DoctrineException::unknownDriver($params['driver']);
romanb's avatar
romanb committed
152 153
        }
    }
154
}