ConnectionManager.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 0.10.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Datasource;
  16. use Cake\Core\StaticConfigTrait;
  17. use Cake\Datasource\Exception\MissingDatasourceConfigException;
  18. /**
  19. * Manages and loads instances of Connection
  20. *
  21. * Provides an interface to loading and creating connection objects. Acts as
  22. * a registry for the connections defined in an application.
  23. *
  24. * Provides an interface for loading and enumerating connections defined in
  25. * config/app.php
  26. */
  27. class ConnectionManager
  28. {
  29. use StaticConfigTrait {
  30. config as protected _config;
  31. parseDsn as protected _parseDsn;
  32. }
  33. /**
  34. * A map of connection aliases.
  35. *
  36. * @var array
  37. */
  38. protected static $_aliasMap = [];
  39. /**
  40. * An array mapping url schemes to fully qualified driver class names
  41. *
  42. * @return array
  43. */
  44. protected static $_dsnClassMap = [
  45. 'mysql' => 'Cake\Database\Driver\Mysql',
  46. 'postgres' => 'Cake\Database\Driver\Postgres',
  47. 'sqlite' => 'Cake\Database\Driver\Sqlite',
  48. 'sqlserver' => 'Cake\Database\Driver\Sqlserver',
  49. ];
  50. /**
  51. * The ConnectionRegistry used by the manager.
  52. *
  53. * @var \Cake\Datasource\ConnectionRegistry
  54. */
  55. protected static $_registry = null;
  56. /**
  57. * Configure a new connection object.
  58. *
  59. * The connection will not be constructed until it is first used.
  60. *
  61. * @param string|array $key The name of the connection config, or an array of multiple configs.
  62. * @param array $config An array of name => config data for adapter.
  63. * @return mixed null when adding configuration and an array of configuration data when reading.
  64. * @throws \Cake\Core\Exception\Exception When trying to modify an existing config.
  65. * @see \Cake\Core\StaticConfigTrait::config()
  66. */
  67. public static function config($key, $config = null)
  68. {
  69. if (is_array($config)) {
  70. $config['name'] = $key;
  71. }
  72. return static::_config($key, $config);
  73. }
  74. /**
  75. * Parses a DSN into a valid connection configuration
  76. *
  77. * This method allows setting a DSN using formatting similar to that used by PEAR::DB.
  78. * The following is an example of its usage:
  79. *
  80. * ```
  81. * $dsn = 'mysql://user:pass@localhost/database';
  82. * $config = ConnectionManager::parseDsn($dsn);
  83. *
  84. * $dsn = 'Cake\Database\Driver\Mysql://localhost:3306/database?className=Cake\Database\Connection';
  85. * $config = ConnectionManager::parseDsn($dsn);
  86. *
  87. * $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
  88. * $config = ConnectionManager::parseDsn($dsn);
  89. * ```
  90. *
  91. * For all classes, the value of `scheme` is set as the value of both the `className` and `driver`
  92. * unless they have been otherwise specified.
  93. *
  94. * Note that querystring arguments are also parsed and set as values in the returned configuration.
  95. *
  96. * @param string $config The DSN string to convert to a configuration array
  97. * @return array The configuration array to be stored after parsing the DSN
  98. */
  99. public static function parseDsn($config = null)
  100. {
  101. $config = static::_parseDsn($config);
  102. if (isset($config['path']) && empty($config['database'])) {
  103. $config['database'] = substr($config['path'], 1);
  104. }
  105. if (empty($config['driver'])) {
  106. $config['driver'] = $config['className'];
  107. $config['className'] = 'Cake\Database\Connection';
  108. }
  109. unset($config['path']);
  110. return $config;
  111. }
  112. /**
  113. * Set one or more connection aliases.
  114. *
  115. * Connection aliases allow you to rename active connections without overwriting
  116. * the aliased connection. This is most useful in the testsuite for replacing
  117. * connections with their test variant.
  118. *
  119. * Defined aliases will take precedence over normal connection names. For example,
  120. * if you alias 'default' to 'test', fetching 'default' will always return the 'test'
  121. * connection as long as the alias is defined.
  122. *
  123. * You can remove aliases with ConnectionManager::dropAlias().
  124. *
  125. * @param string $from The connection to add an alias to.
  126. * @param string $to The alias to create. $from should return when loaded with get().
  127. * @return void
  128. * @throws \Cake\Datasource\Exception\MissingDatasourceConfigException When aliasing a
  129. * connection that does not exist.
  130. */
  131. public static function alias($from, $to)
  132. {
  133. if (empty(static::$_config[$to]) && empty(static::$_config[$from])) {
  134. throw new MissingDatasourceConfigException(
  135. sprintf('Cannot create alias of "%s" as it does not exist.', $from)
  136. );
  137. }
  138. static::$_aliasMap[$to] = $from;
  139. }
  140. /**
  141. * Drop an alias.
  142. *
  143. * Removes an alias from ConnectionManager. Fetching the aliased
  144. * connection may fail if there is no other connection with that name.
  145. *
  146. * @param string $name The connection name to remove aliases for.
  147. * @return void
  148. */
  149. public static function dropAlias($name)
  150. {
  151. unset(static::$_aliasMap[$name]);
  152. }
  153. /**
  154. * Get a connection.
  155. *
  156. * If the connection has not been constructed an instance will be added
  157. * to the registry. This method will use any aliases that have been
  158. * defined. If you want the original unaliased connections pass `FALSE`
  159. * as second parameter.
  160. *
  161. * @param string $name The connection name.
  162. * @param bool $useAliases Set to false to not use aliased connections.
  163. * @return \Cake\Datasource\ConnectionInterface A connection object.
  164. * @throws \Cake\Datasource\Exception\MissingDatasourceConfigException When config
  165. * data is missing.
  166. */
  167. public static function get($name, $useAliases = true)
  168. {
  169. if ($useAliases && isset(static::$_aliasMap[$name])) {
  170. $name = static::$_aliasMap[$name];
  171. }
  172. if (empty(static::$_config[$name])) {
  173. throw new MissingDatasourceConfigException(['name' => $name]);
  174. }
  175. if (empty(static::$_registry)) {
  176. static::$_registry = new ConnectionRegistry();
  177. }
  178. if (isset(static::$_registry->{$name})) {
  179. return static::$_registry->{$name};
  180. }
  181. return static::$_registry->load($name, static::$_config[$name]);
  182. }
  183. }