forked from snc/SncRedisBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use RedisDsn to build connection options for env based config
fix snc#425 - Always use PredisParametersFactory to build connection options so that it creates consistency between env/non-env based config - Use RedisDsn to parse redis DSN instead of \Predis\Connection\Parameters::parse(). It creates consistence between phpredis and predis behavior.
- Loading branch information
Showing
6 changed files
with
158 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Snc\RedisBundle\Factory; | ||
|
||
use Predis\Connection\ParametersInterface; | ||
use Snc\RedisBundle\DependencyInjection\Configuration\RedisDsn; | ||
|
||
class PredisParametersFactory | ||
{ | ||
/** | ||
* @param array $options | ||
* @param string $class | ||
* @param string $dsn | ||
* | ||
* @return ParametersInterface | ||
*/ | ||
public static function create($options, $class, $dsn) | ||
{ | ||
if (!is_a($class, '\Predis\Connection\ParametersInterface', true)) { | ||
throw new \InvalidArgumentException(sprintf('%s::%s requires $class argument to implement %s', __CLASS__, __METHOD__, '\Predis\Connection\ParametersInterface')); | ||
} | ||
|
||
$dsnOptions = static::parseDsn(new RedisDsn($dsn)); | ||
$dsnOptions = array_merge($options, $dsnOptions); | ||
|
||
return new $class($dsnOptions); | ||
} | ||
|
||
/** | ||
* @param RedisDsn $dsn | ||
* | ||
* @return array | ||
*/ | ||
private static function parseDsn(RedisDsn $dsn) | ||
{ | ||
if (null !== $dsn->getSocket()) { | ||
$options['scheme'] = 'unix'; | ||
$options['path'] = $dsn->getSocket(); | ||
} else { | ||
$options['scheme'] = 'tcp'; | ||
$options['host'] = $dsn->getHost(); | ||
$options['port'] = $dsn->getPort(); | ||
if (null !== $dsn->getDatabase()) { | ||
$options['path'] = $dsn->getDatabase(); | ||
} | ||
} | ||
if (null !== $dsn->getDatabase()) { | ||
$options['database'] = $dsn->getDatabase(); | ||
} | ||
$options['password'] = $dsn->getPassword(); | ||
$options['weight'] = $dsn->getWeight(); | ||
|
||
return $options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Snc\RedisBundle\Tests\Factory; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Snc\RedisBundle\Factory\PredisParametersFactory; | ||
|
||
class PredisParametersFactoryTest extends TestCase | ||
{ | ||
public function createDp() | ||
{ | ||
return array( | ||
array( | ||
'redis://z:df577d779b4f724c8c29b5eff5bcc534b732722b9df308a661f1b79014175063d5@ec2-34-321-123-45.us-east-1.compute.amazonaws.com:3210', | ||
'Predis\Connection\Parameters', | ||
array( | ||
'test' => 123, | ||
'some' => 'string', | ||
'arbitrary' => true, | ||
'values' => array(1, 2, 3) | ||
), | ||
array( | ||
'test' => 123, | ||
'some' => 'string', | ||
'arbitrary' => true, | ||
'values' => array(1, 2, 3), | ||
'scheme' => 'tcp', | ||
'host' => 'ec2-34-321-123-45.us-east-1.compute.amazonaws.com', | ||
'port' => 3210, | ||
'path' => null, | ||
'alias' => null, | ||
'timeout' => null, | ||
'read_write_timeout' => null, | ||
'async_connect' => null, | ||
'tcp_nodelay' => null, | ||
'persistent' => null, | ||
'password' => 'df577d779b4f724c8c29b5eff5bcc534b732722b9df308a661f1b79014175063d5', | ||
'database' => null, | ||
), | ||
), | ||
array( | ||
'redis://pw@/var/run/redis/redis-1.sock/10', | ||
'Predis\Connection\Parameters', | ||
array( | ||
'test' => 124, | ||
'password' => 'toto', | ||
'alias' => 'one_alias', | ||
), | ||
array( | ||
'test' => 124, | ||
'scheme' => 'unix', | ||
'host' => '127.0.0.1', | ||
'port' => 6379, | ||
'path' => '/var/run/redis/redis-1.sock', | ||
'alias' => 'one_alias', | ||
'timeout' => null, | ||
'read_write_timeout' => null, | ||
'async_connect' => null, | ||
'tcp_nodelay' => null, | ||
'persistent' => null, | ||
'password' => 'pw', | ||
'database' => 10, | ||
), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @param string $dsn | ||
* @param string $class | ||
* @param array $options | ||
* @param array $expectedParameters | ||
* | ||
* @dataProvider createDp | ||
*/ | ||
public function testCreate($dsn, $class, $options, $expectedParameters) | ||
{ | ||
$parameters = PredisParametersFactory::create($options, $class, $dsn); | ||
|
||
$this->assertInstanceOf($class, $parameters); | ||
|
||
foreach ($expectedParameters as $name => $value) { | ||
$this->assertSame($value, $parameters->{$name}, "Wrong '$name' value"); | ||
} | ||
|
||
// No user can exist within a redis connection. | ||
$this->assertObjectNotHasAttribute('user', $parameters); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
public function testCreateException() | ||
{ | ||
PredisParametersFactory::create(array(), '\stdClass', 'redis://localhost'); | ||
} | ||
} |