|
|
@@ -46,6 +46,13 @@ class ConnectionManager {
|
|
|
protected static $_config = [];
|
|
|
|
|
|
/**
|
|
|
+ * A map of connection aliases.
|
|
|
+ *
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ protected static $_aliasMap = [];
|
|
|
+
|
|
|
+/**
|
|
|
* The ConnectionRegistry used by the manager.
|
|
|
*
|
|
|
* @var Cake\Database\ConnectionRegistry
|
|
|
@@ -72,22 +79,71 @@ class ConnectionManager {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Set one or more connection aliases.
|
|
|
+ *
|
|
|
+ * Connection aliases allow you to rename active connections without overwriting
|
|
|
+ * the aliased connection. This is most useful in the testsuite for replacing
|
|
|
+ * connections with their test variant.
|
|
|
+ *
|
|
|
+ * Defined aliases will take precedence over normal connection names. For example,
|
|
|
+ * if you alias 'default' to 'test', fetching 'default' will always return the 'test'
|
|
|
+ * connection as long as the alias is defined.
|
|
|
+ *
|
|
|
+ * You can remove aliases with ConnectionManager::dropAlias().
|
|
|
+ *
|
|
|
+ * @param string|array $from The connection to rename. Can also be a map of multiple
|
|
|
+ * aliases to set.
|
|
|
+ * @param string $to The connection $from should return when loaded with get().
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public static function alias($from, $to = null) {
|
|
|
+ if (is_array($from)) {
|
|
|
+ foreach ($from as $source => $dest) {
|
|
|
+ static::alias($source, $dest);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (empty(static::$_config[$from])) {
|
|
|
+ throw new Error\MissingDatasourceConfigException(
|
|
|
+ __d('cake_dev', 'Cannot alias connection "%s" as it does not exist.', $from)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ static::$_aliasMap[$to] = $from;
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
+ * Drop an alias.
|
|
|
+ *
|
|
|
+ * Removes an alias from ConnectionManager. Fetching the aliased
|
|
|
+ * connection may fail if there is no other connection with that name.
|
|
|
+ *
|
|
|
+ * @param string $name The connection name to remove aliases for.
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public static function dropAlias($name) {
|
|
|
+ unset(static::$_aliasMap[$name]);
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
* Get a connection.
|
|
|
*
|
|
|
* If the connection has not been constructed an instance will be added
|
|
|
- * to the registry.
|
|
|
+ * to the registry. This method will use any aliases that have been
|
|
|
+ * defined. If you want the original unaliased connections use getOriginal()
|
|
|
*
|
|
|
* @param string $name The connection name.
|
|
|
* @return Connection A connection object.
|
|
|
* @throws Cake\Error\MissingDatasourceConfigException When config data is missing.
|
|
|
*/
|
|
|
public static function get($name) {
|
|
|
- if (empty(static::$_config[$name])) {
|
|
|
+ if (empty(static::$_config[$name]) && empty(static::$_aliasMap[$name])) {
|
|
|
throw new Error\MissingDatasourceConfigException(['name' => $name]);
|
|
|
}
|
|
|
if (empty(static::$_registry)) {
|
|
|
static::$_registry = new ConnectionRegistry();
|
|
|
}
|
|
|
+ if (isset(static::$_aliasMap[$name])) {
|
|
|
+ $name = static::$_aliasMap[$name];
|
|
|
+ }
|
|
|
if (isset(static::$_registry->{$name})) {
|
|
|
return static::$_registry->{$name};
|
|
|
}
|