Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Parse Url in Redis configuration #28612

Merged
merged 5 commits into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Illuminate/Database/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Support\ConfigurationUrlParser;
use Illuminate\Database\Connectors\ConnectionFactory;

/**
Expand Down
26 changes: 22 additions & 4 deletions src/Illuminate/Redis/RedisManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use InvalidArgumentException;
use Illuminate\Contracts\Redis\Factory;
use Illuminate\Redis\Connections\Connection;
use Illuminate\Support\ConfigurationUrlParser;

/**
* @mixin \Illuminate\Redis\Connections\Connection
Expand Down Expand Up @@ -95,7 +96,9 @@ public function resolve($name = null)
$options = $this->config['options'] ?? [];

if (isset($this->config[$name])) {
return $this->connector()->connect($this->config[$name], $options);
return $this->connector()->connect(
$this->parseConnectionConfigWithUrl($this->config[$name]), $options
);
}

if (isset($this->config['clusters'][$name])) {
Expand All @@ -113,10 +116,10 @@ public function resolve($name = null)
*/
protected function resolveCluster($name)
{
$clusterOptions = $this->config['clusters']['options'] ?? [];

return $this->connector()->connectToCluster(
$this->config['clusters'][$name], $clusterOptions, $this->config['options'] ?? []
$this->parseConnectionConfigWithUrl($this->config['clusters'][$name]),
$this->config['clusters']['options'] ?? [],
$this->config['options'] ?? []
);
}

Expand Down Expand Up @@ -153,6 +156,21 @@ protected function connector()
}
}

/**
* Parse the redis configuration, hydrating options using a redis configuration URL if possible.
*
* @param array $config
* @return array
*/
protected function parseConnectionConfigWithUrl($config)
{
$parsedConfig = (new ConfigurationUrlParser)->parseConfiguration($config);

return array_filter($parsedConfig, function ($key) {
return ! in_array($key, ['driver', 'username']);
}, ARRAY_FILTER_USE_KEY);
}

/**
* Return all of the created connections.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

namespace Illuminate\Database;
namespace Illuminate\Support;
mathieutu marked this conversation as resolved.
Show resolved Hide resolved

use Illuminate\Support\Arr;
use InvalidArgumentException;

class ConfigurationUrlParser
Expand Down
5 changes: 3 additions & 2 deletions tests/Redis/RedisConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,9 @@ public function connections()
$prefixedPhpredis = new RedisManager(new Application, 'phpredis', [
'cluster' => false,
'default' => [
'host' => $host,
'port' => $port,
'url' => "redis://user@$host:$port",
'host' => 'overwrittenByUrl',
'port' => 'overwrittenByUrl',
'database' => 5,
'options' => ['prefix' => 'laravel:'],
'timeout' => 0.5,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Illuminate\Tests\Database;
namespace Illuminate\Tests\Support;

use PHPUnit\Framework\TestCase;
use Illuminate\Database\ConfigurationUrlParser;
use Illuminate\Support\ConfigurationUrlParser;

class DatabaseUrlParserTest extends TestCase
class ConfigurationUrlParserTest extends TestCase
{
/**
* @dataProvider databaseUrls
Expand All @@ -23,7 +23,7 @@ public function testDriversAliases()
'postgres' => 'pgsql',
'postgresql' => 'pgsql',
'sqlite3' => 'sqlite',
], ConfigurationUrlParser::getDriverAliases());
], \Illuminate\Support\ConfigurationUrlParser::getDriverAliases());

ConfigurationUrlParser::addDriverAlias('some-particular-alias', 'mysql');

Expand Down Expand Up @@ -334,6 +334,24 @@ public function databaseUrls()
'options' => ['foo' => 'bar'],
],
],
'Redis Example' => [
[
// Coming directly from Heroku documentation
'url' => 'redis://h:[email protected]:111',
'host' => '127.0.0.1',
'password' => null,
'port' => 6379,
'database' => 0,
],
[
'driver' => 'redis',
'host' => 'ec2-111-1-1-1.compute-1.amazonaws.com',
'port' => 111,
'database' => 0,
'username' => 'h',
'password' => 'asdfqwer1234asdf',
],
],
];
}
}