Skip to content

Commit

Permalink
Merge pull request #147 from snc/issue-147
Browse files Browse the repository at this point in the history
IPv6 support
  • Loading branch information
Seldaek authored Apr 6, 2018
2 parents 3b11bdc + 112afe3 commit a2c17e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
9 changes: 8 additions & 1 deletion DependencyInjection/Configuration/RedisDsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected function parseDsn($dsn)
$this->database = (int) $matches[2];
$dsn = $matches[1];
}
if (preg_match('#^([^:]+)(:(\d+))?$#', $dsn, $matches)) {
if (preg_match('#^([^:]+)(:(\d+))?$#', $dsn, $matches)) { // parse socket path or IPv4 address with optional port
if (!empty($matches[1])) {
// parse host/ip or socket
if ('/' === $matches[1]{0}) {
Expand All @@ -177,6 +177,13 @@ protected function parseDsn($dsn)
// parse port
$this->port = (int) $matches[3];
}
} elseif (preg_match('#^\[([^\]]+)](:(\d+))?$#', $dsn, $matches)) { // parse enclosed IPv6 address and optional port
if (!empty($matches[1])) {
$this->host = $matches[1];
}
if (!empty($matches[3])) {
$this->port = (int) $matches[3];
}
}
}

Expand Down
32 changes: 21 additions & 11 deletions Tests/DependencyInjection/Configuration/RedisDsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,28 @@ class RedisDsnTest extends \PHPUnit_Framework_TestCase
*/
public static function hostValues()
{
return array(
array('redis://localhost', 'localhost'),
array('redis://localhost/1', 'localhost'),
array('redis://localhost:63790', 'localhost'),
array('redis://localhost:63790/10', 'localhost'),
array('redis://pw@localhost:63790/10', 'localhost'),
array('redis://127.0.0.1', '127.0.0.1'),
array('redis://127.0.0.1/1', '127.0.0.1'),
array('redis://127.0.0.1:63790', '127.0.0.1'),
array('redis://127.0.0.1:63790/10', '127.0.0.1'),
array('redis://pw@127.0.0.1:63790/10', '127.0.0.1'),
$result = array();

$hosts = array(
'localhost',
'127.0.0.1',
'::1',
'1050:0000:0000:0000:0005:0600:300c:326b',
'1050:0:0:0:5:600:300c:326b',
'ff06:0:0:0:0:0:0:c3',
'ff06::c3'
);

foreach ($hosts as $host) {
$h = false !== strpos($host, ':') ? '['.$host.']' : $host;
$result[] = array(sprintf('redis://%s', $h), $host);
$result[] = array(sprintf('redis://%s/1', $h), $host);
$result[] = array(sprintf('redis://%s:6379', $h), $host);
$result[] = array(sprintf('redis://%s:6379/10', $h), $host);
$result[] = array(sprintf('redis://pw@%s:6379/10', $h), $host);
}

return $result;
}

/**
Expand Down

0 comments on commit a2c17e3

Please sign in to comment.