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