Browse Source

Add DSN parsing support to the Log class

Jose Diaz-Gonzalez 11 years ago
parent
commit
53a0868ca4
1 changed files with 35 additions and 0 deletions
  1. 35 0
      src/Log/Log.php

+ 35 - 0
src/Log/Log.php

@@ -105,6 +105,7 @@ class Log {
 
 	use StaticConfigTrait {
 		config as protected _config;
+		parseDsn as protected _parseDsn;
 	}
 
 /**
@@ -259,6 +260,40 @@ 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 it's 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.
+ *
+ * There is a special replacement value for the string `LOGS`, which is replaced by the LOGS constant.
+ *
+ * @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) {
+		if (is_array($config) && isset($config['url'])) {
+			$config['url'] = str_replace('LOGS', LOGS, $config['url']);
+		}
+
+		$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.