DriverManager.php 6.29 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?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
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. 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 36 37
     * To add your own driver use the 'driverClass' parameter to
     * {@link DriverManager::getConnection()}.
     *
romanb's avatar
romanb committed
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
            'oci8' => 'Doctrine\DBAL\Driver\OCI8\Driver',
Benjamin Eberlei's avatar
Benjamin Eberlei committed
46
            'ibm_db2' => 'Doctrine\DBAL\Driver\IBMDB2\DB2Driver',
47
            'pdo_ibm' => 'Doctrine\DBAL\Driver\PDOIbm\Driver',
48
            'pdo_sqlsrv' => 'Doctrine\DBAL\Driver\PDOSqlsrv\Driver',
49
            'mysqli' => 'Doctrine\DBAL\Driver\Mysqli\Driver',
50
            'drizzle_pdo_mysql'  => 'Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver',
51
            'sqlsrv' => 'Doctrine\DBAL\Driver\SQLSrv\Driver',
romanb's avatar
romanb committed
52
            );
53 54

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

romanb's avatar
romanb committed
57
    /**
58
     * Creates a connection object based on the specified parameters.
59
     * This method returns a Doctrine\DBAL\Connection which wraps the underlying
60
     * driver connection.
romanb's avatar
romanb committed
61
     *
62
     * $params must contain at least one of the following.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
63
     *
64
     * Either 'driver' with one of the following values:
65
     *
66 67 68
     *     pdo_mysql
     *     pdo_sqlite
     *     pdo_pgsql
69 70 71
     *     pdo_oci (unstable)
     *     pdo_sqlsrv
     *     pdo_ibm (unstable)
72
     *     pdo_sqlsrv
73 74 75 76
     *     mysqli
     *     sqlsrv
     *     ibm_db2 (unstable)
     *     drizzle_pdo_mysql
Benjamin Eberlei's avatar
Benjamin Eberlei committed
77
     *
78 79
     * OR 'driverClass' that contains the full class name (with namespace) of the
     * driver class to instantiate.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
80
     *
81
     * Other (optional) parameters:
Benjamin Eberlei's avatar
Benjamin Eberlei committed
82
     *
83
     * <b>user (string)</b>:
Benjamin Eberlei's avatar
Benjamin Eberlei committed
84 85
     * The username to use when connecting.
     *
86 87
     * <b>password (string)</b>:
     * The password to use when connecting.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
88
     *
89 90 91
     * <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
92
     *
93 94
     * <b>pdo</b>:
     * You can pass an existing PDO instance through this parameter. The PDO
95
     * instance will be wrapped in a Doctrine\DBAL\Connection.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
96
     *
97 98
     * <b>wrapperClass</b>:
     * You may specify a custom wrapper class through the 'wrapperClass'
99
     * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
Benjamin Eberlei's avatar
Benjamin Eberlei committed
100
     *
101 102 103
     * <b>driverClass</b>:
     * The driver class to use.
     *
104
     * @param array $params The parameters.
105 106 107
     * @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
108
     */
109 110
    public static function getConnection(
            array $params,
111 112
            Configuration $config = null,
            EventManager $eventManager = null)
romanb's avatar
romanb committed
113 114 115
    {
        // create default config and event manager, if not set
        if ( ! $config) {
116
            $config = new Configuration();
romanb's avatar
romanb committed
117 118
        }
        if ( ! $eventManager) {
119
            $eventManager = new EventManager();
romanb's avatar
romanb committed
120
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
121

romanb's avatar
romanb committed
122
        // check for existing pdo object
123
        if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
124
            throw DBALException::invalidPdoInstance();
romanb's avatar
romanb committed
125
        } else if (isset($params['pdo'])) {
126
            $params['pdo']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
jwage's avatar
jwage committed
127
            $params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME);
romanb's avatar
romanb committed
128
        } else {
129
            self::_checkParams($params);
romanb's avatar
romanb committed
130 131 132 133
        }
        if (isset($params['driverClass'])) {
            $className = $params['driverClass'];
        } else {
134
            $className = self::$_driverMap[$params['driver']];
romanb's avatar
romanb committed
135
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
136

romanb's avatar
romanb committed
137
        $driver = new $className();
Benjamin Eberlei's avatar
Benjamin Eberlei committed
138

139
        $wrapperClass = 'Doctrine\DBAL\Connection';
140 141 142 143 144 145
        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
146
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
147

romanb's avatar
romanb committed
148 149
        return new $wrapperClass($params, $driver, $config, $eventManager);
    }
150

romanb's avatar
romanb committed
151 152 153 154 155
    /**
     * Checks the list of parameters.
     *
     * @param array $params
     */
156
    private static function _checkParams(array $params)
Benjamin Eberlei's avatar
Benjamin Eberlei committed
157
    {
Pascal Borreli's avatar
Pascal Borreli committed
158
        // check existence of mandatory parameters
Benjamin Eberlei's avatar
Benjamin Eberlei committed
159

romanb's avatar
romanb committed
160 161
        // driver
        if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
162
            throw DBALException::driverRequired();
romanb's avatar
romanb committed
163
        }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
164

romanb's avatar
romanb committed
165
        // check validity of parameters
Benjamin Eberlei's avatar
Benjamin Eberlei committed
166

romanb's avatar
romanb committed
167
        // driver
168
        if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
169
            throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap));
romanb's avatar
romanb committed
170
        }
171 172 173 174

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