-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for Heroku style REDIS_URL env variables
- Loading branch information
Klaus Großmann
committed
Jun 12, 2017
1 parent
abfb9da
commit 29290d9
Showing
10 changed files
with
483 additions
and
21 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 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 |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
/** | ||
* RedisDsn | ||
*/ | ||
class RedisDsn | ||
class RedisDsn implements RedisDsnInterface | ||
{ | ||
/** | ||
* @var string | ||
|
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,16 @@ | ||
<?php | ||
|
||
namespace Snc\RedisBundle\DependencyInjection\Configuration; | ||
|
||
interface RedisDsnInterface | ||
{ | ||
/** | ||
* @return bool | ||
*/ | ||
public function isValid(); | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getAlias(); | ||
} |
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,43 @@ | ||
<?php | ||
|
||
namespace Snc\RedisBundle\DependencyInjection\Configuration; | ||
|
||
class RedisEnvDsn implements RedisDsnInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $dsn; | ||
|
||
/** | ||
* @param string $dsn | ||
*/ | ||
public function __construct($dsn) | ||
{ | ||
$this->dsn = $dsn; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isValid() | ||
{ | ||
return (bool)preg_match('#^env_\w+_[0-9a-fA-F]{32}$#', $this->dsn); | ||
} | ||
|
||
/** | ||
* @return null | ||
*/ | ||
public function getAlias() | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getDsn() | ||
{ | ||
return $this->dsn; | ||
} | ||
} |
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 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,30 @@ | ||
<?php | ||
|
||
namespace Snc\RedisBundle\Factory; | ||
|
||
use Predis\Connection\ParametersInterface; | ||
|
||
class EnvParametersFactory | ||
{ | ||
/** | ||
* @param array $options | ||
* @param string $class | ||
* @param string $dsn | ||
* | ||
* @return ParametersInterface | ||
*/ | ||
public static function create($options, $class, $dsn) | ||
{ | ||
$callable = [$class, 'parse']; | ||
|
||
if(!is_callable($callable)) { | ||
$alias = isset($options['alias']) ? $options['alias'] : 'the client'; | ||
|
||
throw new \InvalidArgumentException(sprintf('The parameters class you defined for %s does not support parsing url like DSNs.', $alias)); | ||
} | ||
|
||
$dsnOptions = call_user_func($callable, $dsn); | ||
|
||
return new $class(array_merge($options, $dsnOptions)); | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
Tests/DependencyInjection/Configuration/RedisEnvDsnTest.php
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,78 @@ | ||
<?php | ||
|
||
namespace Snc\RedisBundle\Tests\DependencyInjection\Configuration; | ||
|
||
use Snc\RedisBundle\DependencyInjection\Configuration\RedisEnvDsn; | ||
|
||
class RedisEnvDsnTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @static | ||
* | ||
* @return array | ||
*/ | ||
public static function isValidValues() | ||
{ | ||
return array( | ||
array('redis://localhost', false), | ||
array('redis://localhost/1', false), | ||
array('redis://pw@localhost:63790/10', false), | ||
array('redis://127.0.0.1', false), | ||
array('redis://127.0.0.1/1', false), | ||
array('redis://pw@127.0.0.1:63790/10', false), | ||
array('redis:///redis.sock', false), | ||
array('redis:///redis.sock/1', false), | ||
array('redis://pw@/redis.sock/10', false), | ||
array('redis://pw@/redis.sock/10', false), | ||
array('redis://%redis_host%', false), | ||
array('redis://%redis_host%/%redis_db%', false), | ||
array('redis://%redis_host%:%redis_port%', false), | ||
array('redis://%redis_host%:%redis_port%/%redis_db%', false), | ||
array('redis://%redis_pass%@%redis_host%:%redis_port%/%redis_db%', false), | ||
array('redis://env_REDIS_HOST_1ef60d9ef7a55747f99d0a42206e58ed', false), | ||
array('redis://env_REDIS_HOST_1ef60d9ef7a55747f99d0a42206e58ed/env_REDIS_DB_0d1da5bfb707f91e21a1f78cd11fcd0a', false), | ||
array('redis://env_REDIS_HOST_1ef60d9ef7a55747f99d0a42206e58ed:env_REDIS_PORT_0458150d4bf631c8ac63b0fa4d257a21', false), | ||
array('redis://env_REDIS_HOST_1ef60d9ef7a55747f99d0a42206e58ed:env_REDIS_PORT_0458150d4bf631c8ac63b0fa4d257a21/env_REDIS_DB_0d1da5bfb707f91e21a1f78cd11fcd0a', false), | ||
array('redis://env_REDIS_PW_e7406513a853fd4692343d101baecb7c@env_REDIS_HOST_1ef60d9ef7a55747f99d0a42206e58ed:env_REDIS_PORT_0458150d4bf631c8ac63b0fa4d257a21/env_REDIS_DB_0d1da5bfb707f91e21a1f78cd11fcd0a', false), | ||
array('localhost', false), | ||
array('localhost/1', false), | ||
array('pw@localhost:63790/10', false), | ||
array('env_REDIS_URL_z07910a06a086c83ba41827aa00b26ed', false), | ||
array('env_REDIS_URL_e07910a06a086c83ba41827aa00b26ed', true), | ||
); | ||
} | ||
|
||
/** | ||
* @param string $dsn DSN | ||
* @param bool $valid Valid | ||
* | ||
* @dataProvider isValidValues | ||
*/ | ||
public function testIsValid($dsn, $valid) | ||
{ | ||
$dsn = new RedisEnvDsn($dsn); | ||
$this->assertSame($valid, $dsn->isValid()); | ||
} | ||
|
||
/** | ||
* @param string $dsn DSN | ||
* | ||
* @dataProvider isValidValues | ||
*/ | ||
public function testAliasIsNull($dsn) | ||
{ | ||
$dsn = new RedisEnvDsn($dsn); | ||
$this->assertNull($dsn->getAlias()); | ||
} | ||
|
||
/** | ||
* @param string $dsn DSN | ||
* | ||
* @dataProvider isValidValues | ||
*/ | ||
public function testDsnIsUnmodified($providedDsn) | ||
{ | ||
$dsn = new RedisEnvDsn($providedDsn); | ||
$this->assertEquals($providedDsn, $dsn->getDsn()); | ||
} | ||
} |
Oops, something went wrong.