Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / DriverManager.php
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 MIT license. 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      * To add your own driver use the 'driverClass' parameter to
36      * {@link DriverManager::getConnection()}.
37      *
38      * @var array
39      */
40      private static $_driverMap = array(
41             'pdo_mysql'  => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
42             'pdo_sqlite' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
43             'pdo_pgsql'  => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
44             'pdo_oci' => 'Doctrine\DBAL\Driver\PDOOracle\Driver',
45             'oci8' => 'Doctrine\DBAL\Driver\OCI8\Driver',
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',
52             );
53
54     /** Private constructor. This class cannot be instantiated. */
55     private function __construct() { }
56
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.
61      *
62      * $params must contain at least one of the following.
63      *
64      * Either 'driver' with one of the following values:
65      *
66      *     pdo_mysql
67      *     pdo_sqlite
68      *     pdo_pgsql
69      *     pdo_oci (unstable)
70      *     pdo_sqlsrv
71      *     pdo_ibm (unstable)
72      *     pdo_sqlsrv
73      *     mysqli
74      *     sqlsrv
75      *     ibm_db2 (unstable)
76      *     drizzle_pdo_mysql
77      *
78      * OR 'driverClass' that contains the full class name (with namespace) of the
79      * driver class to instantiate.
80      *
81      * Other (optional) parameters:
82      *
83      * <b>user (string)</b>:
84      * The username to use when connecting.
85      *
86      * <b>password (string)</b>:
87      * The password to use when connecting.
88      *
89      * <b>driverOptions (array)</b>:
90      * Any additional driver-specific options for the driver. These are just passed
91      * through to the driver.
92      *
93      * <b>pdo</b>:
94      * You can pass an existing PDO instance through this parameter. The PDO
95      * instance will be wrapped in a Doctrine\DBAL\Connection.
96      *
97      * <b>wrapperClass</b>:
98      * You may specify a custom wrapper class through the 'wrapperClass'
99      * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
100      *
101      * <b>driverClass</b>:
102      * The driver class to use.
103      *
104      * @param array $params The parameters.
105      * @param \Doctrine\DBAL\Configuration The configuration to use.
106      * @param \Doctrine\Common\EventManager The event manager to use.
107      * @return \Doctrine\DBAL\Connection
108      */
109     public static function getConnection(
110             array $params,
111             Configuration $config = null,
112             EventManager $eventManager = null)
113     {
114         // create default config and event manager, if not set
115         if ( ! $config) {
116             $config = new Configuration();
117         }
118         if ( ! $eventManager) {
119             $eventManager = new EventManager();
120         }
121
122         // check for existing pdo object
123         if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
124             throw DBALException::invalidPdoInstance();
125         } else if (isset($params['pdo'])) {
126             $params['pdo']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
127             $params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME);
128         } else {
129             self::_checkParams($params);
130         }
131         if (isset($params['driverClass'])) {
132             $className = $params['driverClass'];
133         } else {
134             $className = self::$_driverMap[$params['driver']];
135         }
136
137         $driver = new $className();
138
139         $wrapperClass = 'Doctrine\DBAL\Connection';
140         if (isset($params['wrapperClass'])) {
141             if (is_subclass_of($params['wrapperClass'], $wrapperClass)) {
142                $wrapperClass = $params['wrapperClass'];
143             } else {
144                 throw DBALException::invalidWrapperClass($params['wrapperClass']);
145             }
146         }
147
148         return new $wrapperClass($params, $driver, $config, $eventManager);
149     }
150
151     /**
152      * Checks the list of parameters.
153      *
154      * @param array $params
155      */
156     private static function _checkParams(array $params)
157     {
158         // check existance of mandatory parameters
159
160         // driver
161         if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
162             throw DBALException::driverRequired();
163         }
164
165         // check validity of parameters
166
167         // driver
168         if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
169             throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap));
170         }
171
172         if (isset($params['driverClass']) && ! in_array('Doctrine\DBAL\Driver', class_implements($params['driverClass'], true))) {
173             throw DBALException::invalidDriverClass($params['driverClass']);
174         }
175     }
176 }