Skip to content

Commit

Permalink
bug #20891 Add support for REDIS_URL environment variables. (robinvdv…
Browse files Browse the repository at this point in the history
…leuten)

This PR was submitted for the master branch but it was merged into the 3.2 branch instead (closes #20891).

Discussion
----------

Add support for REDIS_URL environment variables.

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20850
| License       | MIT
| Doc PR        | -

In most PAAS solutions - Heroku for example - the Redis connection string is exposed as an environment variable like `REDIS_URL`. This PR adds support for those by allowing the following config;

```yml
framework:
    cache:
        default_redis_provider: "%env(REDIS_URL)%"
```

In the referenced issue there was some discussion about maybe a new config options like `default_redis_url`, but this makes it hard to check in the extension for the case that an user configured a custom _default_redis_provider_ and also has configured the default url.

Commits
-------

4e6086f Add support for REDIS_URL environment variables.
  • Loading branch information
fabpot committed Dec 13, 2016
2 parents 1e1a018 + 4e6086f commit 2422f7d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
Expand Up @@ -96,7 +96,9 @@ private function getNamespace($seed, $id)
*/
public static function getServiceProvider(ContainerBuilder $container, $name)
{
if (0 === strpos($name, 'redis://')) {
$container->resolveEnvPlaceholders($name, null, $usedEnvs);

if (0 === strpos($name, 'redis://') || $usedEnvs) {
$dsn = $name;

if (!$container->hasDefinition($name = md5($dsn))) {
Expand Down
@@ -0,0 +1,9 @@
<?php

$container->setParameter('env(REDIS_URL)', 'redis://paas.com');

$container->loadFromExtension('framework', array(
'cache' => array(
'default_redis_provider' => '%env(REDIS_URL)%',
),
));
@@ -0,0 +1,17 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<parameters>
<parameter key="env(REDIS_URL)">redis://paas.com</parameter>
</parameters>

<framework:config>
<framework:cache>
<framework:default-redis-provider>%env(REDIS_URL)%</framework:default-redis-provider>
</framework:cache>
</framework:config>
</container>
@@ -0,0 +1,6 @@
parameters:
env(REDIS_URL): redis://paas.com

framework:
cache:
default_redis_provider: "%env(REDIS_URL)%"
Expand Up @@ -697,6 +697,34 @@ public function testPropertyInfoEnabled()
$this->assertTrue($container->has('property_info'));
}

public function testCacheDefaultRedisProvider()
{
$container = $this->createContainerFromFile('cache');

$redisUrl = 'redis://localhost';
$providerId = md5($redisUrl);

$this->assertTrue($container->hasDefinition($providerId));

$url = $container->getDefinition($providerId)->getArgument(0);

$this->assertSame($redisUrl, $url);
}

public function testCacheDefaultRedisProviderWithEnvVar()
{
$container = $this->createContainerFromFile('cache_env_var');

$redisUrl = 'redis://paas.com';
$providerId = md5($redisUrl);

$this->assertTrue($container->hasDefinition($providerId));

$url = $container->getDefinition($providerId)->getArgument(0);

$this->assertSame($redisUrl, $url);
}

public function testCachePoolServices()
{
$container = $this->createContainerFromFile('cache');
Expand Down

0 comments on commit 2422f7d

Please sign in to comment.