vendor/doctrine-dbal/lib/Doctrine/DBAL/DriverManager.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 /*
       
     3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
     4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
     5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
     6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
     7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
     8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
     9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    14  *
       
    15  * This software consists of voluntary contributions made by many individuals
       
    16  * and is licensed under the LGPL. For more information, see
       
    17  * <http://www.doctrine-project.org>.
       
    18  */
       
    19 
       
    20 namespace Doctrine\DBAL;
       
    21 
       
    22 use Doctrine\Common\EventManager;
       
    23 
       
    24 /**
       
    25  * Factory for creating Doctrine\DBAL\Connection instances.
       
    26  *
       
    27  * @author Roman Borschel <roman@code-factory.org>
       
    28  * @since 2.0
       
    29  */
       
    30 final class DriverManager
       
    31 {
       
    32     /**
       
    33      * List of supported drivers and their mappings to the driver classes.
       
    34      *
       
    35      * @var array
       
    36      * @todo REMOVE. Users should directly supply class names instead.
       
    37      */
       
    38      private static $_driverMap = array(
       
    39             'pdo_mysql'  => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
       
    40             'pdo_sqlite' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
       
    41             'pdo_pgsql'  => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
       
    42             'pdo_oci' => 'Doctrine\DBAL\Driver\PDOOracle\Driver',
       
    43             'oci8' => 'Doctrine\DBAL\Driver\OCI8\Driver',
       
    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             );
       
    48 
       
    49     /** Private constructor. This class cannot be instantiated. */
       
    50     private function __construct() { }
       
    51 
       
    52     /**
       
    53      * Creates a connection object based on the specified parameters.
       
    54      * This method returns a Doctrine\DBAL\Connection which wraps the underlying
       
    55      * driver connection.
       
    56      *
       
    57      * $params must contain at least one of the following.
       
    58      * 
       
    59      * Either 'driver' with one of the following values:
       
    60      *     pdo_mysql
       
    61      *     pdo_sqlite
       
    62      *     pdo_pgsql
       
    63      *     pdo_oracle
       
    64      *     pdo_sqlsrv
       
    65      * 
       
    66      * OR 'driverClass' that contains the full class name (with namespace) of the
       
    67      * driver class to instantiate.
       
    68      * 
       
    69      * Other (optional) parameters:
       
    70      * 
       
    71      * <b>user (string)</b>:
       
    72      * The username to use when connecting. 
       
    73      * 
       
    74      * <b>password (string)</b>:
       
    75      * The password to use when connecting.
       
    76      * 
       
    77      * <b>driverOptions (array)</b>:
       
    78      * Any additional driver-specific options for the driver. These are just passed
       
    79      * through to the driver.
       
    80      * 
       
    81      * <b>pdo</b>:
       
    82      * You can pass an existing PDO instance through this parameter. The PDO
       
    83      * instance will be wrapped in a Doctrine\DBAL\Connection.
       
    84      * 
       
    85      * <b>wrapperClass</b>:
       
    86      * 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      * @param Doctrine\DBAL\Configuration The configuration to use.
       
    91      * @param Doctrine\Common\EventManager The event manager to use.
       
    92      * @return Doctrine\DBAL\Connection
       
    93      */
       
    94     public static function getConnection(
       
    95             array $params,
       
    96             Configuration $config = null,
       
    97             EventManager $eventManager = null)
       
    98     {
       
    99         // create default config and event manager, if not set
       
   100         if ( ! $config) {
       
   101             $config = new Configuration();
       
   102         }
       
   103         if ( ! $eventManager) {
       
   104             $eventManager = new EventManager();
       
   105         }
       
   106         
       
   107         // check for existing pdo object
       
   108         if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
       
   109             throw DBALException::invalidPdoInstance();
       
   110         } else if (isset($params['pdo'])) {
       
   111             $params['pdo']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
       
   112             $params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME);
       
   113         } else {
       
   114             self::_checkParams($params);
       
   115         }
       
   116         if (isset($params['driverClass'])) {
       
   117             $className = $params['driverClass'];
       
   118         } else {
       
   119             $className = self::$_driverMap[$params['driver']];
       
   120         }
       
   121         
       
   122         $driver = new $className();
       
   123         
       
   124         $wrapperClass = 'Doctrine\DBAL\Connection';
       
   125         if (isset($params['wrapperClass'])) {
       
   126             if (is_subclass_of($params['wrapperClass'], $wrapperClass)) {
       
   127                $wrapperClass = $params['wrapperClass'];
       
   128             } else {
       
   129                 throw DBALException::invalidWrapperClass($params['wrapperClass']);
       
   130             }
       
   131         }
       
   132         
       
   133         return new $wrapperClass($params, $driver, $config, $eventManager);
       
   134     }
       
   135 
       
   136     /**
       
   137      * Checks the list of parameters.
       
   138      *
       
   139      * @param array $params
       
   140      */
       
   141     private static function _checkParams(array $params)
       
   142     {        
       
   143         // check existance of mandatory parameters
       
   144         
       
   145         // driver
       
   146         if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
       
   147             throw DBALException::driverRequired();
       
   148         }
       
   149         
       
   150         // check validity of parameters
       
   151         
       
   152         // driver
       
   153         if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
       
   154             throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap));
       
   155         }
       
   156 
       
   157         if (isset($params['driverClass']) && ! in_array('Doctrine\DBAL\Driver', class_implements($params['driverClass'], true))) {
       
   158             throw DBALException::invalidDriverClass($params['driverClass']);
       
   159         }
       
   160     }
       
   161 }