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

20 21 22
namespace Doctrine\DBAL;

use Doctrine\Common\EventManager;
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 DriverManager
romanb's avatar
romanb committed
31 32
{
    /**
33
     * List of supported drivers and their mappings to the driver classes.
romanb's avatar
romanb committed
34 35
     *
     * @var array
romanb's avatar
romanb committed
36
     * @todo REMOVE. Users should directly supply class names instead.
romanb's avatar
romanb committed
37
     */
38
     private static $_driverMap = array(
39 40 41
            'pdo_mysql'  => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
            'pdo_sqlite' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
            'pdo_pgsql'  => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
42
            'pdo_oci' => 'Doctrine\DBAL\Driver\PDOOracle\Driver',
43
            'oci8' => 'Doctrine\DBAL\Driver\OCI8\Driver',
Benjamin Eberlei's avatar
Benjamin Eberlei committed
44
            'ibm_db2' => 'Doctrine\DBAL\Driver\IBMDB2\DB2Driver',
45
            'pdo_ibm' => 'Doctrine\DBAL\Driver\PDOIbm\Driver',
46
            'pdo_sqlsrv' => 'Doctrine\DBAL\Driver\PDOSqlsrv\Driver',
47
            'mysqli' => 'Doctrine\DBAL\Driver\Mysqli\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
     * $params must contain at least one of the following.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
59
     *
60 61 62 63 64
     * Either 'driver' with one of the following values:
     *     pdo_mysql
     *     pdo_sqlite
     *     pdo_pgsql
     *     pdo_oracle
65
     *     pdo_sqlsrv
Benjamin Eberlei's avatar
Benjamin Eberlei committed
66
     *
67 68
     * OR 'driverClass' that contains the full class name (with namespace) of the
     * driver class to instantiate.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
69
     *
70
     * Other (optional) parameters:
Benjamin Eberlei's avatar
Benjamin Eberlei committed
71
     *
72
     * <b>user (string)</b>:
Benjamin Eberlei's avatar
Benjamin Eberlei committed
73 74
     * The username to use when connecting.
     *
75 76
     * <b>password (string)</b>:
     * The password to use when connecting.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
77
     *
78 79 80
     * <b>driverOptions (array)</b>:
     * Any additional driver-specific options for the driver. These are just passed
     * through to the driver.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
81
     *
82 83
     * <b>pdo</b>:
     * You can pass an existing PDO instance through this parameter. The PDO
84
     * instance will be wrapped in a Doctrine\DBAL\Connection.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
85
     *
86 87
     * <b>wrapperClass</b>:
     * You may specify a custom wrapper class through the 'wrapperClass'
88
     * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
89
     *
90 91 92
     * <b>driverClass</b>:
     * The driver class to use.
     *
93
     * @param array $params The parameters.
94 95 96
     * @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
97
     */
98 99
    public static function getConnection(
            array $params,
100 101
            Configuration $config = null,
            EventManager $eventManager = null)
romanb's avatar
romanb committed
102 103 104
    {
        // create default config and event manager, if not set
        if ( ! $config) {
105
            $config = new Configuration();
romanb's avatar
romanb committed
106 107
        }
        if ( ! $eventManager) {
108
            $eventManager = new EventManager();
romanb's avatar
romanb committed
109
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
110

romanb's avatar
romanb committed
111
        // check for existing pdo object
112
        if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
113
            throw DBALException::invalidPdoInstance();
romanb's avatar
romanb committed
114
        } else if (isset($params['pdo'])) {
115
            $params['pdo']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
jwage's avatar
jwage committed
116
            $params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME);
romanb's avatar
romanb committed
117
        } else {
118
            self::_checkParams($params);
romanb's avatar
romanb committed
119 120 121 122
        }
        if (isset($params['driverClass'])) {
            $className = $params['driverClass'];
        } else {
123
            $className = self::$_driverMap[$params['driver']];
romanb's avatar
romanb committed
124
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
125

romanb's avatar
romanb committed
126
        $driver = new $className();
Benjamin Eberlei's avatar
Benjamin Eberlei committed
127

128
        $wrapperClass = 'Doctrine\DBAL\Connection';
129 130 131 132 133 134
        if (isset($params['wrapperClass'])) {
            if (is_subclass_of($params['wrapperClass'], $wrapperClass)) {
               $wrapperClass = $params['wrapperClass'];
            } else {
                throw DBALException::invalidWrapperClass($params['wrapperClass']);
            }
romanb's avatar
romanb committed
135
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
136

romanb's avatar
romanb committed
137 138
        return new $wrapperClass($params, $driver, $config, $eventManager);
    }
139

romanb's avatar
romanb committed
140 141 142 143 144
    /**
     * Checks the list of parameters.
     *
     * @param array $params
     */
145
    private static function _checkParams(array $params)
Benjamin Eberlei's avatar
Benjamin Eberlei committed
146
    {
romanb's avatar
romanb committed
147
        // check existance of mandatory parameters
Benjamin Eberlei's avatar
Benjamin Eberlei committed
148

romanb's avatar
romanb committed
149 150
        // driver
        if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
151
            throw DBALException::driverRequired();
romanb's avatar
romanb committed
152
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
153

romanb's avatar
romanb committed
154
        // check validity of parameters
Benjamin Eberlei's avatar
Benjamin Eberlei committed
155

romanb's avatar
romanb committed
156
        // driver
157
        if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
158
            throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap));
romanb's avatar
romanb committed
159
        }
160 161 162 163

        if (isset($params['driverClass']) && ! in_array('Doctrine\DBAL\Driver', class_implements($params['driverClass'], true))) {
            throw DBALException::invalidDriverClass($params['driverClass']);
        }
romanb's avatar
romanb committed
164
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
165
}