Browse Source

Simplify usage of parseDsn

parseDsn can now be used mostly out of the box by all classes using the StaticConfigTrait - with the exception of ConnectionManager, as noted below. Unless they are otherwise specified, the `scheme` of the DSN is set as both the `className` and `driver`. As such, the preferred method for usage in your application would be:

    Fully\Namespaced\ClassName://username:password@host:port/path?query=string&args=here&driver=Some\Other\Class\Name

This commit drops support for `scheme+Namespaced\ClassName\As\Driver`, simplifying the underlying logic a bit.

For the ConnectionManager class, Drivers still need to transform the path into a database_name, and as such we cannot use the trait method as is.
Jose Diaz-Gonzalez 11 years ago
parent
commit
953b72c9c3

+ 1 - 34
src/Cache/Cache.php

@@ -65,9 +65,7 @@ use RuntimeException;
  */
 class Cache {
 
-	use StaticConfigTrait {
-		parseDsn as protected _parseDsn;
-	}
+	use StaticConfigTrait;
 
 /**
  * Flag for tracking whether or not caching is enabled.
@@ -480,35 +478,4 @@ class Cache {
 		return $results;
 	}
 
-/**
- * Parses a DSN into a valid connection configuration
- *
- * This method allows setting a DSN using PEAR::DB formatting, with added support for drivers
- * in the SQLAlchemy format. The following is an example of its usage:
- *
- * {{{
- * 	 $dsn = 'File:///';
- * 	 $config = Cache::parseDsn($dsn);
- *
- * 	 $dsn = 'File://?prefix=myapp_cake_core_&serialize=true&duration=+2 minutes&path=/tmp/persistent/';
- * 	 $config = Cache::parseDsn($dsn);
- * }}
- *
- * If an array is given, the parsed DSN will be merged into this array. Note that querystring
- * arguments are also parsed and set as values in the returned configuration.
- *
- * @param array $config An array with a `url` key mapping to a string DSN
- * @return mixed null when adding configuration and an array of configuration data when reading.
- */
-	public static function parseDsn($config = null) {
-		$config = static::_parseDsn($config);
-
-		if (isset($config['scheme'])) {
-			$config['className'] = $config['scheme'];
-		}
-
-		unset($config['scheme']);
-		return $config;
-	}
-
 }

+ 41 - 19
src/Core/StaticConfigTrait.php

@@ -132,16 +132,37 @@ trait StaticConfigTrait {
 /**
  * Parses a DSN into a valid connection configuration
  *
- * This method allows setting a DSN using PEAR::DB formatting, with added support for drivers
- * in the SQLAlchemy format. The following is an example of its usage:
+ * This method allows setting a DSN using formatting similar to that used by PEAR::DB.
+ * The following is an example of its usage:
  *
  * {{{
- * 	 $dsn = 'mysql+Cake\Database\Driver\Mysql://user:password@localhost:3306/database_name';
- * 	 $config = ConnectionManager::parseDsn($dsn);
+ *   $dsn = 'Cake\Database\Driver\Mysql://localhost/database?className=Cake\Database\Connection';
+ *   $config = ConnectionManager::parseDsn($dsn);
+ *
+ *   $dsn = 'Cake\Database\Driver\Mysql://localhost:3306/database?className=Cake\Database\Connection';
+ *   $config = ConnectionManager::parseDsn($dsn);
+ *
+ *   $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
+ *   $config = ConnectionManager::parseDsn($dsn);
+ *
+ *   $dsn = 'Cake\Log\Engine\FileLog://?types=notice,info,debug&file=debug&path=LOGS';
+ * 	 $config = Log::parseDsn($dsn);
+ *
+ *   $dsn = 'Mail://user:secret@localhost:25?timeout=30&client=null&tls=null';
+ * 	 $config = Email::parseDsn($dsn);
+ *
+ *   $dsn = 'File:///';
+ * 	 $config = Cache::parseDsn($dsn);
+ *
+ *   $dsn = 'File://?prefix=myapp_cake_core_&serialize=true&duration=+2 minutes&path=/tmp/persistent/';
+ * 	 $config = Cache::parseDsn($dsn);
+ *
  * }}
  *
- * If an array is given, the parsed DSN will be merged into this array. Note that querystring
- * arguments are also parsed and set as values in the returned configuration.
+ * For all classes, the value of `scheme` is set as the value of both the `className` and `driver`
+ * unless they have been otherwise specified.
+ *
+ * Note that querystring arguments are also parsed and set as values in the returned configuration.
  *
  * @param array $config An array with a `url` key mapping to a string DSN
  * @return mixed null when adding configuration and an array of configuration data when reading.
@@ -155,15 +176,11 @@ trait StaticConfigTrait {
 		$dsn = $config['url'];
 		unset($config['url']);
 
-		if (preg_match("/^([\w]+)\+([\w\\\]+)/", $dsn, $matches)) {
-			$scheme = $matches[1];
-			$driver = $matches[2];
-			$dsn = preg_replace("/^([\w]+)\+([\w\\\]+)/", $scheme, $dsn);
-		} elseif (preg_match("/^([\w\\\]+)/", $dsn, $matches)) {
-			$scheme = explode('\\', $matches[1]);
+		if (preg_match('/^([\w\\]+)/', $dsn, $matches)) {
+			$scheme = explode('\\', $matches[2]);
 			$scheme = array_pop($scheme);
-			$driver = $matches[1];
-			$dsn = preg_replace("/^([\w\\\]+)/", $scheme, $dsn);
+			$driver = $matches[2];
+			$dsn = preg_replace('/^([\w\\]+)/', $scheme, $dsn);
 		}
 
 		$parsed = parse_url($dsn);
@@ -176,14 +193,9 @@ trait StaticConfigTrait {
 
 		parse_str($query, $queryArgs);
 
-		if ($driver !== null) {
-			$queryArgs['driver'] = $driver;
-		}
-
 		if (isset($parsed['user'])) {
 			$parsed['username'] = $parsed['user'];
 		}
-
 		if (isset($parsed['pass'])) {
 			$parsed['password'] = $parsed['pass'];
 		}
@@ -191,6 +203,16 @@ trait StaticConfigTrait {
 		unset($config['user'], $config['pass']);
 		$config = array_merge($queryArgs, $parsed, $config);
 
+		if ($driver !== null) {
+			if (!isset($config['driver'])) {
+				$config['driver'] = $driver;
+			}
+
+			if (!isset($config['className'])) {
+				$config['className'] = $driver;
+			}
+		}
+
 		foreach ($config as $key => $value) {
 			if ($value === 'true') {
 				$config[$key] = true;

+ 14 - 6
src/Datasource/ConnectionManager.php

@@ -69,16 +69,24 @@ class ConnectionManager {
 /**
  * Parses a DSN into a valid connection configuration
  *
- * This method allows setting a DSN using PEAR::DB formatting, with added support for drivers
- * in the SQLAlchemy format. The following is an example of its usage:
+ * This method allows setting a DSN using formatting similar to that used by PEAR::DB.
+ * The following is an example of its usage:
  *
  * {{{
- * 	 $dsn = 'mysql+Cake\Database\Driver\Mysql://user:password@localhost:3306/database_name';
- * 	 $config = ConnectionManager::parseDsn($dsn);
+ *   $dsn = 'Cake\Database\Driver\Mysql://localhost/database?className=Cake\Database\Connection';
+ *   $config = ConnectionManager::parseDsn($dsn);
+ *
+ *   $dsn = 'Cake\Database\Driver\Mysql://localhost:3306/database?className=Cake\Database\Connection';
+ *   $config = ConnectionManager::parseDsn($dsn);
+ *
+ *   $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
+ *   $config = ConnectionManager::parseDsn($dsn);
  * }}
  *
- * If an array is given, the parsed DSN will be merged into this array. Note that querystring
- * arguments are also parsed and set as values in the returned configuration.
+ * For all classes, the value of `scheme` is set as the value of both the `className` and `driver`
+ * unless they have been otherwise specified.
+ *
+ * Note that querystring arguments are also parsed and set as values in the returned configuration.
  *
  * @param array $config An array with a `url` key mapping to a string DSN
  * @return mixed null when adding configuration and an array of configuration data when reading.

+ 0 - 29
src/Log/Log.php

@@ -105,7 +105,6 @@ class Log {
 
 	use StaticConfigTrait {
 		config as protected _config;
-		parseDsn as protected _parseDsn;
 	}
 
 /**
@@ -260,34 +259,6 @@ class Log {
 	}
 
 /**
- * Parses a DSN into a valid connection configuration
- *
- * This method allows setting a DSN using PEAR::DB formatting, with added support for drivers
- * in the SQLAlchemy format. The following is an example of its usage:
- *
- * {{{
- * 	 $dsn = 'Cake\Log\Engine\FileLog://?types=notice,info,debug&file=debug&path=LOGS';
- * 	 $config = Log::parseDsn($dsn);
- * }}
- *
- * If an array is given, the parsed DSN will be merged into this array. Note that querystring
- * arguments are also parsed and set as values in the returned configuration.
- *
- * @param array $config An array with a `url` key mapping to a string DSN
- * @return mixed null when adding configuration and an array of configuration data when reading.
- */
-	public static function parseDsn($config = null) {
-		$config = static::_parseDsn($config);
-
-		if (isset($config['driver'])) {
-			$config['className'] = $config['driver'];
-		}
-
-		unset($config['driver']);
-		return $config;
-	}
-
-/**
  * Get a logging engine.
  *
  * @param string $name Key name of a configured adapter to get.

+ 1 - 31
src/Network/Email/Email.php

@@ -42,9 +42,7 @@ use LogicException;
  */
 class Email {
 
-	use StaticConfigTrait {
-		parseDsn as protected _parseDsn;
-	}
+	use StaticConfigTrait;
 
 /**
  * Default X-Mailer
@@ -1194,34 +1192,6 @@ class Email {
 	}
 
 /**
- * Parses a DSN into a valid connection configuration
- *
- * This method allows setting a DSN using PEAR::DB formatting, with added support for drivers
- * in the SQLAlchemy format. The following is an example of its usage:
- *
- * {{{
- * 	 $dsn = 'mail://user:secret@localhost:25?timeout=30&client=null&tls=null';
- * 	 $config = Email::parseDsn($dsn);
- * }}
- *
- * If an array is given, the parsed DSN will be merged into this array. Note that querystring
- * arguments are also parsed and set as values in the returned configuration.
- *
- * @param array $config An array with a `url` key mapping to a string DSN
- * @return mixed null when adding configuration and an array of configuration data when reading.
- */
-	public static function parseDsn($config = null) {
-		$config = static::_parseDsn($config);
-
-		if (isset($config['scheme'])) {
-			$config['className'] = $config['scheme'];
-		}
-
-		unset($config['scheme']);
-		return $config;
-	}
-
-/**
  * Get/Set the configuration profile to use for this instance.
  *
  * @param null|string|array $config String with configuration name, or