DriverManager.php 5.51 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. */
jwage's avatar
jwage committed
51 52 53 54
    public function __construct()
    {
        throw \Doctrine\Common\DoctrineException::driverManagerCannotBeInstantiated();
    }
55

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

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