Skip to content

Commit

Permalink
Add dsn parsing support to the Email class
Browse files Browse the repository at this point in the history
This pr adds DSN parsing support to both `Email::config()` and `Email::configTransport()`, though it is more useful in the `Email::configTransport()` case.
  • Loading branch information
josegonzalez committed Oct 12, 2014
1 parent bf98b50 commit 50f48ee
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/Network/Email/Email.php
Expand Up @@ -42,7 +42,9 @@
*/
class Email {

use StaticConfigTrait;
use StaticConfigTrait {
parseDsn as protected _parseDsn;
}

/**
* Default X-Mailer
Expand Down Expand Up @@ -1169,7 +1171,13 @@ public static function configTransport($key, $config = null) {
if (isset(static::$_transportConfig[$key])) {
throw new BadMethodCallException(sprintf('Cannot modify an existing config "%s"', $key));
}
if (is_object($config)) {
if (is_array($config)) {
$config = static::parseDsn($config);
} elseif ($config === null && is_array($key)) {
foreach ($key as $name => $settings) {
$key[$name] = static::parseDsn($settings);
}
} elseif (is_object($config)) {
$config = ['className' => $config];
}
static::$_transportConfig[$key] = $config;
Expand All @@ -1185,6 +1193,42 @@ public static function dropTransport($key) {
unset(static::$_transportConfig[$key]);
}

/**
* 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 = '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) {
$config = static::_parseDsn($config);

if (isset($config['scheme'])) {
$config['className'] = $config['scheme'];
}

if (isset($config['user'])) {
$config['username'] = $config['user'];
}

if (isset($config['pass'])) {
$config['password'] = $config['pass'];
}

unset($config['scheme'], $config['user'], $config['pass']);
return $config;
}

/**
* Get/Set the configuration profile to use for this instance.
*
Expand Down

0 comments on commit 50f48ee

Please sign in to comment.