From 53a0868ca462c6a323f1ce790ea55dfbd8eaef49 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 12 Oct 2014 14:06:08 -0400 Subject: [PATCH] Add DSN parsing support to the Log class --- src/Log/Log.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Log/Log.php b/src/Log/Log.php index 9f53424888b..5f233b58182 100644 --- a/src/Log/Log.php +++ b/src/Log/Log.php @@ -105,6 +105,7 @@ class Log { use StaticConfigTrait { config as protected _config; + parseDsn as protected _parseDsn; } /** @@ -258,6 +259,40 @@ public static function config($key, $config = null) { static::$_dirtyConfig = true; } +/** + * 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. *