ConnectionManager.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 0.10.x.1402
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Database;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\StaticConfigTrait;
  21. use Cake\Database\Connection;
  22. use Cake\Database\ConnectionRegistry;
  23. use Cake\Error;
  24. /**
  25. * Manages and loads instances of Connection
  26. *
  27. * Provides an interface to loading and creating connection objects. Acts as
  28. * a registry for the connections defined in an application.
  29. *
  30. * Provides an interface for loading and enumerating connections defined in
  31. * app/Config/datasources.php
  32. */
  33. class ConnectionManager {
  34. use StaticConfigTrait {
  35. config as protected _config;
  36. }
  37. /**
  38. * Holds a list of connection configurations
  39. *
  40. * @var array
  41. */
  42. protected static $_config = [];
  43. /**
  44. * A map of connection aliases.
  45. *
  46. * @var array
  47. */
  48. protected static $_aliasMap = [];
  49. /**
  50. * The ConnectionRegistry used by the manager.
  51. *
  52. * @var Cake\Database\ConnectionRegistry
  53. */
  54. protected static $_registry = null;
  55. /**
  56. * Configure a new connection object.
  57. *
  58. * The connection will not be constructed until it is first used.
  59. *
  60. * @see Cake\Core\StaticConfigTrait::config()
  61. *
  62. * @param string|array $key The name of the connection config, or an array of multiple configs.
  63. * @param array $config An array of name => config data for adapter.
  64. * @return mixed null when adding configuration and an array of configuration data when reading.
  65. * @throws Cake\Error\Exception When trying to modify an existing config.
  66. */
  67. public static function config($key, $config = null) {
  68. if (is_array($config)) {
  69. $config['name'] = $key;
  70. }
  71. return static::_config($key, $config);
  72. }
  73. /**
  74. * Set one or more connection aliases.
  75. *
  76. * Connection aliases allow you to rename active connections without overwriting
  77. * the aliased connection. This is most useful in the testsuite for replacing
  78. * connections with their test variant.
  79. *
  80. * Defined aliases will take precedence over normal connection names. For example,
  81. * if you alias 'default' to 'test', fetching 'default' will always return the 'test'
  82. * connection as long as the alias is defined.
  83. *
  84. * You can remove aliases with ConnectionManager::dropAlias().
  85. *
  86. * @param string $from The connection to add an alias to.
  87. * @param string $to The alias to create. $from should return when loaded with get().
  88. * @return void
  89. * @throws Cake\Error\MissingDataSourceConfigException When aliasing a connection that does not exist.
  90. */
  91. public static function alias($from, $to) {
  92. if (empty(static::$_config[$to]) && empty(static::$_config[$from])) {
  93. throw new Error\MissingDatasourceConfigException(
  94. __d('cake_dev', 'Cannot create alias of "%s" as it does not exist.', $from)
  95. );
  96. }
  97. static::$_aliasMap[$to] = $from;
  98. }
  99. /**
  100. * Drop an alias.
  101. *
  102. * Removes an alias from ConnectionManager. Fetching the aliased
  103. * connection may fail if there is no other connection with that name.
  104. *
  105. * @param string $name The connection name to remove aliases for.
  106. * @return void
  107. */
  108. public static function dropAlias($name) {
  109. unset(static::$_aliasMap[$name]);
  110. }
  111. /**
  112. * Get a connection.
  113. *
  114. * If the connection has not been constructed an instance will be added
  115. * to the registry. This method will use any aliases that have been
  116. * defined. If you want the original unaliased connections use getOriginal()
  117. *
  118. * @param string $name The connection name.
  119. * @param boolean $useAliases Set to false to not use aliased connections.
  120. * @return Connection A connection object.
  121. * @throws Cake\Error\MissingDatasourceConfigException When config data is missing.
  122. */
  123. public static function get($name, $useAliases = true) {
  124. if ($useAliases && isset(static::$_aliasMap[$name])) {
  125. $name = static::$_aliasMap[$name];
  126. }
  127. if (empty(static::$_config[$name])) {
  128. throw new Error\MissingDatasourceConfigException(['name' => $name]);
  129. }
  130. if (empty(static::$_registry)) {
  131. static::$_registry = new ConnectionRegistry();
  132. }
  133. if (isset(static::$_registry->{$name})) {
  134. return static::$_registry->{$name};
  135. }
  136. return static::$_registry->load($name, static::$_config[$name]);
  137. }
  138. /**
  139. * Gets a reference to a DataSource object
  140. *
  141. * @param string $name The name of the DataSource, as defined in app/Config/datasources.php
  142. * @return DataSource Instance
  143. * @throws Cake\Error\MissingDatasourceException
  144. * @deprecated Will be removed in 3.0 stable.
  145. */
  146. public static function getDataSource($name) {
  147. return static::get($name);
  148. }
  149. /**
  150. * Dynamically creates a DataSource object at runtime, with the given name and settings
  151. *
  152. * @param string $name The DataSource name
  153. * @param array $config The DataSource configuration settings
  154. * @return DataSource A reference to the DataSource object, or null if creation failed
  155. * @deprecated Will be removed in 3.0 stable
  156. */
  157. public static function create($name = '', $config = array()) {
  158. static::config($name, $config);
  159. return static::get($name);
  160. }
  161. }