DriverManager.php 6.7 KB

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