ConnectionManager.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\ConnectionRegistry;
  18. use Cake\Datasource\Exception\MissingDatasourceConfigException;
  19. /**
  20. * Manages and loads instances of Connection
  21. *
  22. * Provides an interface to loading and creating connection objects. Acts as
  23. * a registry for the connections defined in an application.
  24. *
  25. * Provides an interface for loading and enumerating connections defined in
  26. * config/app.php
  27. */
  28. class ConnectionManager {
  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. * The ConnectionRegistry used by the manager.
  41. *
  42. * @var \Cake\Datasource\ConnectionRegistry
  43. */
  44. protected static $_registry = null;
  45. /**
  46. * Configure a new connection object.
  47. *
  48. * The connection will not be constructed until it is first used.
  49. *
  50. * @param string|array $key The name of the connection config, or an array of multiple configs.
  51. * @param array $config An array of name => config data for adapter.
  52. * @return mixed null when adding configuration and an array of configuration data when reading.
  53. * @throws \Cake\Core\Exception\Exception When trying to modify an existing config.
  54. * @see \Cake\Core\StaticConfigTrait::config()
  55. */
  56. public static function config($key, $config = null) {
  57. if (is_array($config)) {
  58. $config['name'] = $key;
  59. }
  60. return static::_config($key, $config);
  61. }
  62. /**
  63. * Parses a DSN into a valid connection configuration
  64. *
  65. * This method allows setting a DSN using formatting similar to that used by PEAR::DB.
  66. * The following is an example of its usage:
  67. *
  68. * {{{
  69. * $dsn = 'mysql://user:pass@localhost/database';
  70. * $config = ConnectionManager::parseDsn($dsn);
  71. *
  72. * $dsn = 'Cake\Database\Driver\Mysql://localhost:3306/database?className=Cake\Database\Connection';
  73. * $config = ConnectionManager::parseDsn($dsn);
  74. *
  75. * $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
  76. * $config = ConnectionManager::parseDsn($dsn);
  77. * }}}
  78. *
  79. * For all classes, the value of `scheme` is set as the value of both the `className` and `driver`
  80. * unless they have been otherwise specified.
  81. *
  82. * Note that querystring arguments are also parsed and set as values in the returned configuration.
  83. *
  84. * @param array $config An array with a `url` key mapping to a string DSN
  85. * @return array The configuration array to be stored after parsing the DSN
  86. */
  87. public static function parseDsn($config = null) {
  88. $config = static::_parseDsn($config);
  89. if (isset($config['path']) && empty($config['database'])) {
  90. $config['database'] = substr($config['path'], 1);
  91. }
  92. if (empty($config['driver'])) {
  93. $config['driver'] = $config['className'];
  94. $config['className'] = 'Cake\Database\Connection';
  95. }
  96. unset($config['path']);
  97. return $config;
  98. }
  99. /**
  100. * Returns an array mapping url schemes to fully qualified driver class names
  101. *
  102. * @return array
  103. */
  104. public static function getClassMap() {
  105. return [
  106. 'mysql' => 'Cake\Database\Driver\Mysql',
  107. 'postgres' => 'Cake\Database\Driver\Postgres',
  108. 'sqlite' => 'Cake\Database\Driver\Sqlite',
  109. 'sqlserver' => 'Cake\Database\Driver\Sqlserver',
  110. ];
  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. if (empty(static::$_config[$to]) && empty(static::$_config[$from])) {
  133. throw new MissingDatasourceConfigException(
  134. sprintf('Cannot create alias of "%s" as it does not exist.', $from)
  135. );
  136. }
  137. static::$_aliasMap[$to] = $from;
  138. }
  139. /**
  140. * Drop an alias.
  141. *
  142. * Removes an alias from ConnectionManager. Fetching the aliased
  143. * connection may fail if there is no other connection with that name.
  144. *
  145. * @param string $name The connection name to remove aliases for.
  146. * @return void
  147. */
  148. public static function dropAlias($name) {
  149. unset(static::$_aliasMap[$name]);
  150. }
  151. /**
  152. * Get a connection.
  153. *
  154. * If the connection has not been constructed an instance will be added
  155. * to the registry. This method will use any aliases that have been
  156. * defined. If you want the original unaliased connections use getOriginal()
  157. *
  158. * @param string $name The connection name.
  159. * @param bool $useAliases Set to false to not use aliased connections.
  160. * @return \Cake\Database\Connection A connection object.
  161. * @throws \Cake\Datasource\Exception\MissingDatasourceConfigException When config
  162. * data is missing.
  163. */
  164. public static function get($name, $useAliases = true) {
  165. if ($useAliases && isset(static::$_aliasMap[$name])) {
  166. $name = static::$_aliasMap[$name];
  167. }
  168. if (empty(static::$_config[$name])) {
  169. throw new MissingDatasourceConfigException(['name' => $name]);
  170. }
  171. if (empty(static::$_registry)) {
  172. static::$_registry = new ConnectionRegistry();
  173. }
  174. if (isset(static::$_registry->{$name})) {
  175. return static::$_registry->{$name};
  176. }
  177. return static::$_registry->load($name, static::$_config[$name]);
  178. }
  179. }