DriverManager.php 5.9 KB

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