8000 bug #20891 Add support for REDIS_URL environment variables. (robinvdv… · skalpa/symfony@2422f7d · GitHub
[go: up one dir, main page]

Skip to content

Commit 2422f7d

Browse files
committed
bug symfony#20891 Add support for REDIS_URL environment variables. (robinvdvleuten)
This PR was submitted for the master branch but it was merged into the 3.2 branch instead (closes symfony#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 | symfony#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.
2 parents 1e1a018 + 4e6086f commit 2422f7d

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ private function getNamespace($seed, $id)
9696
*/
9797
public static function getServiceProvider(ContainerBuilder $container, $name)
9898
{
99-
if (0 === strpos($name, 'redis://')) {
99+
$container->resolveEnvPlaceholders($name, null, $usedEnvs);
100+
101+
if (0 === strpos($name, 'redis://') || $usedEnvs) {
100102
$dsn = $name;
101103

102104
if (!$container->hasDefinition($name = md5($dsn))) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
$container->setParameter('env(REDIS_URL)', 'redis://paas.com');
4+
5+
$container->loadFromExtension('framework', array(
6+
'cache' => array(
7+
'default_redis_provider' => '%env(REDIS_URL)%',
8+
),
9+
));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:framework="http://symfony.com/schema/dic/symfony"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7+
8+
<parameters>
9+
<parameter key="env(REDIS_URL)">redis://paas.com</parameter>
10+
</parameters>
11+
12+
<framework:config>
13+
<framework:cache>
14+
<framework:default-redis-provider>%env(REDIS_URL)%</framework:default-redis-provider>
15+
</framework:cache>
16+
</framework:config>
17+
</container>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
env(REDIS_URL): redis://paas.com
3+
4+
framework:
5+
cache:
6+
default_redis_provider: "%env(REDIS_URL)%"

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,34 @@ public function testPropertyInfoEnabled()
697697
$this->assertTrue($container->has('property_info'));
698698
}
699699

700+
public function testCacheDefaultRedisProvider()
701+
{
702+
$container = $this->createContainerFromFile('cache');
703+
704+
$redisUrl = 'redis://localhost';
705+
$providerId = md5($redisUrl);
706+
707+
$this->assertTrue($container->hasDefinition($providerId));
708+
709+
$url = $container->getDefinition($providerId)->getArgument(0);
710+
711+
$this->assertSame($redisUrl, $url);
712+
}
713+
714+
public function testCacheDefaultRedisProviderWithEnvVar()
715+
{
716+
$container = $this->createContainerFromFile('cache_env_var');
717+
718+
$redisUrl = 'redis://paas.com';
719+
$providerId = md5($redisUrl);
720+
721+
$this->assertTrue($container->hasDefinition($providerId));
722+
723+
$url = $container->getDefinition($providerId)->getArgument(0);
724+
725+
$this->assertSame($redisUrl, $url);
726+
}
727+
700728
public function testCachePoolServices()
701729
{
702730
$container = $this->createContainerFromFile('cache');

0 commit comments

Comments
 (0)
0