DriverManager.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. 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. * @var array
  33. * @todo REMOVE. Users should directly supply class names instead.
  34. */
  35. private static $_driverMap = array(
  36. 'pdo_mysql' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
  37. 'pdo_sqlite' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
  38. 'pdo_pgsql' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
  39. 'pdo_oci' => 'Doctrine\DBAL\Driver\PDOOracle\Driver',
  40. 'oci8' => 'Doctrine\DBAL\Driver\OCI8\Driver',
  41. 'ibm_db2' => 'Doctrine\DBAL\Driver\IBMDB2\DB2Driver',
  42. 'pdo_ibm' => 'Doctrine\DBAL\Driver\PDOIbm\Driver',
  43. 'pdo_sqlsrv' => 'Doctrine\DBAL\Driver\PDOSqlsrv\Driver',
  44. );
  45. /** Private constructor. This class cannot be instantiated. */
  46. private function __construct() { }
  47. /**
  48. * Creates a connection object based on the specified parameters.
  49. * This method returns a Doctrine\DBAL\Connection which wraps the underlying
  50. * driver connection.
  51. *
  52. * $params must contain at least one of the following.
  53. *
  54. * Either 'driver' with one of the following values:
  55. * pdo_mysql
  56. * pdo_sqlite
  57. * pdo_pgsql
  58. * pdo_oracle
  59. * pdo_sqlsrv
  60. *
  61. * OR 'driverClass' that contains the full class name (with namespace) of the
  62. * driver class to instantiate.
  63. *
  64. * Other (optional) parameters:
  65. *
  66. * <b>user (string)</b>:
  67. * The username to use when connecting.
  68. *
  69. * <b>password (string)</b>:
  70. * The password to use when connecting.
  71. *
  72. * <b>driverOptions (array)</b>:
  73. * Any additional driver-specific options for the driver. These are just passed
  74. * through to the driver.
  75. *
  76. * <b>pdo</b>:
  77. * You can pass an existing PDO instance through this parameter. The PDO
  78. * instance will be wrapped in a Doctrine\DBAL\Connection.
  79. *
  80. * <b>wrapperClass</b>:
  81. * You may specify a custom wrapper class through the 'wrapperClass'
  82. * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
  83. *
  84. * @param array $params The parameters.
  85. * @param Doctrine\DBAL\Configuration The configuration to use.
  86. * @param Doctrine\Common\EventManager The event manager to use.
  87. * @return Doctrine\DBAL\Connection
  88. */
  89. public static function getConnection(
  90. array $params,
  91. Configuration $config = null,
  92. EventManager $eventManager = null)
  93. {
  94. // create default config and event manager, if not set
  95. if ( ! $config) {
  96. $config = new Configuration();
  97. }
  98. if ( ! $eventManager) {
  99. $eventManager = new EventManager();
  100. }
  101. // check for existing pdo object
  102. if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
  103. throw DBALException::invalidPdoInstance();
  104. } else if (isset($params['pdo'])) {
  105. $params['pdo']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  106. $params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME);
  107. } else {
  108. self::_checkParams($params);
  109. }
  110. if (isset($params['driverClass'])) {
  111. $className = $params['driverClass'];
  112. } else {
  113. $className = self::$_driverMap[$params['driver']];
  114. }
  115. $driver = new $className();
  116. $wrapperClass = 'Doctrine\DBAL\Connection';
  117. if (isset($params['wrapperClass'])) {
  118. if (is_subclass_of($params['wrapperClass'], $wrapperClass)) {
  119. $wrapperClass = $params['wrapperClass'];
  120. } else {
  121. throw DBALException::invalidWrapperClass($params['wrapperClass']);
  122. }
  123. }
  124. return new $wrapperClass($params, $driver, $config, $eventManager);
  125. }
  126. /**
  127. * Checks the list of parameters.
  128. *
  129. * @param array $params
  130. */
  131. private static function _checkParams(array $params)
  132. {
  133. // check existance of mandatory parameters
  134. // driver
  135. if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
  136. throw DBALException::driverRequired();
  137. }
  138. // check validity of parameters
  139. // driver
  140. if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
  141. throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap));
  142. }
  143. if (isset($params['driverClass']) && ! in_array('Doctrine\DBAL\Driver', class_implements($params['driverClass'], true))) {
  144. throw DBALException::invalidDriverClass($params['driverClass']);
  145. }
  146. }
  147. }