ConnectionManager.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\Error\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. }
  32. /**
  33. * A map of connection aliases.
  34. *
  35. * @var array
  36. */
  37. protected static $_aliasMap = [];
  38. /**
  39. * The ConnectionRegistry used by the manager.
  40. *
  41. * @var \Cake\Datasource\ConnectionRegistry
  42. */
  43. protected static $_registry = null;
  44. /**
  45. * Configure a new connection object.
  46. *
  47. * The connection will not be constructed until it is first used.
  48. *
  49. * @param string|array $key The name of the connection config, or an array of multiple configs.
  50. * @param array $config An array of name => config data for adapter.
  51. * @return mixed null when adding configuration and an array of configuration data when reading.
  52. * @throws \Cake\Error\Exception When trying to modify an existing config.
  53. * @see \Cake\Core\StaticConfigTrait::config()
  54. */
  55. public static function config($key, $config = null) {
  56. if (is_array($config)) {
  57. $config['name'] = $key;
  58. }
  59. return static::_config($key, $config);
  60. }
  61. /**
  62. * Set one or more connection aliases.
  63. *
  64. * Connection aliases allow you to rename active connections without overwriting
  65. * the aliased connection. This is most useful in the testsuite for replacing
  66. * connections with their test variant.
  67. *
  68. * Defined aliases will take precedence over normal connection names. For example,
  69. * if you alias 'default' to 'test', fetching 'default' will always return the 'test'
  70. * connection as long as the alias is defined.
  71. *
  72. * You can remove aliases with ConnectionManager::dropAlias().
  73. *
  74. * @param string $from The connection to add an alias to.
  75. * @param string $to The alias to create. $from should return when loaded with get().
  76. * @return void
  77. * @throws \Cake\Datasource\Error\MissingDatasourceConfigException When aliasing a
  78. * connection that does not exist.
  79. */
  80. public static function alias($from, $to) {
  81. if (empty(static::$_config[$to]) && empty(static::$_config[$from])) {
  82. throw new MissingDatasourceConfigException(
  83. sprintf('Cannot create alias of "%s" as it does not exist.', $from)
  84. );
  85. }
  86. static::$_aliasMap[$to] = $from;
  87. }
  88. /**
  89. * Drop an alias.
  90. *
  91. * Removes an alias from ConnectionManager. Fetching the aliased
  92. * connection may fail if there is no other connection with that name.
  93. *
  94. * @param string $name The connection name to remove aliases for.
  95. * @return void
  96. */
  97. public static function dropAlias($name) {
  98. unset(static::$_aliasMap[$name]);
  99. }
  100. /**
  101. * Get a connection.
  102. *
  103. * If the connection has not been constructed an instance will be added
  104. * to the registry. This method will use any aliases that have been
  105. * defined. If you want the original unaliased connections use getOriginal()
  106. *
  107. * @param string $name The connection name.
  108. * @param bool $useAliases Set to false to not use aliased connections.
  109. * @return \Cake\Database\Connection A connection object.
  110. * @throws \Cake\Datasource\Error\MissingDatasourceConfigException When config
  111. * data is missing.
  112. */
  113. public static function get($name, $useAliases = true) {
  114. if ($useAliases && isset(static::$_aliasMap[$name])) {
  115. $name = static::$_aliasMap[$name];
  116. }
  117. if (empty(static::$_config[$name])) {
  118. throw new Error\MissingDatasourceConfigException(['name' => $name]);
  119. }
  120. if (empty(static::$_registry)) {
  121. static::$_registry = new ConnectionRegistry();
  122. }
  123. if (isset(static::$_registry->{$name})) {
  124. return static::$_registry->{$name};
  125. }
  126. return static::$_registry->load($name, static::$_config[$name]);
  127. }
  128. }