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] Add support for custom redis driver #29275

Merged
merged 6 commits into from
Jul 30, 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
25 changes: 25 additions & 0 deletions src/Illuminate/Contracts/Redis/Connector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Illuminate\Contracts\Redis;

interface Connector
{
/**
* Create a new clustered redis connection.
*
* @param array $config
* @param array $options
* @return \Illuminate\Redis\Connections\Connection
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've realized that \Illuminate\Contracts\Redis\Connection is not sufficient.

RedisManager has a method type hinted on \Illuminate\Redis\Connections\Connection.

It also appears that \Illuminate\Contracts\Redis\Connection does not have all of the methods defined in \Illuminate\Redis\Connections\Connection.

So extensions will need to subclass \Illuminate\Redis\Connections\Connection. That does not seems to be a problem as there are no private methods or properties.

The ideal solution would be to change the interface but that would break backwards compatibility.

We can also add a new interface inheriting from this one \Illuminate\Contracts\Redis\Connection. But having two interface for the same thing does not sounds ideal.

*/
public function connect(array $config, array $options);

/**
* Create a new clustered redis connection.
*
* @param array $config
* @param array $clusterOptions
* @param array $options
* @return \Illuminate\Redis\Connections\Connection
*/
public function connectToCluster(array $config, array $clusterOptions, array $options);
}
3 changes: 2 additions & 1 deletion src/Illuminate/Redis/Connectors/PhpRedisConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use Redis;
use RedisCluster;
use Illuminate\Support\Arr;
use Illuminate\Contracts\Redis\Connector;
use Illuminate\Redis\Connections\PhpRedisConnection;
use Illuminate\Redis\Connections\PhpRedisClusterConnection;

class PhpRedisConnector
class PhpRedisConnector implements Connector
{
/**
* Create a new clustered PhpRedis connection.
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Redis/Connectors/PredisConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Predis\Client;
use Illuminate\Support\Arr;
use Illuminate\Contracts\Redis\Connector;
use Illuminate\Redis\Connections\PredisConnection;
use Illuminate\Redis\Connections\PredisClusterConnection;

class PredisConnector
class PredisConnector implements Connector
{
/**
* Create a new clustered Predis connection.
Expand Down
29 changes: 28 additions & 1 deletion src/Illuminate/Redis/RedisManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class RedisManager implements Factory
*/
protected $driver;

/**
* The registered custom driver creators.
*
* @var array
*/
protected $customCreators = [];

/**
* The Redis server configurations.
*
Expand Down Expand Up @@ -147,10 +154,16 @@ protected function configure(Connection $connection, $name)
/**
* Get the connector instance for the current driver.
*
* @return \Illuminate\Redis\Connectors\PhpRedisConnector|\Illuminate\Redis\Connectors\PredisConnector
* @return \Illuminate\Contracts\Redis\Connector
*/
protected function connector()
{
$customCreator = $this->customCreators[$this->driver] ?? null;

if ($customCreator) {
return call_user_func($customCreator);
}

switch ($this->driver) {
case 'predis':
return new Connectors\PredisConnector;
Expand Down Expand Up @@ -215,6 +228,20 @@ public function setDriver($driver)
$this->driver = $driver;
}

/**
* Register a custom driver creator Closure.
*
* @param string $driver
* @param \Closure $callback
* @return $this
*/
public function extend($driver, \Closure $callback)
{
$this->customCreators[$driver] = $callback->bindTo($this, $this);

return $this;
}

/**
* Pass methods onto the default Redis connection.
*
Expand Down
88 changes: 88 additions & 0 deletions tests/Redis/RedisManagerExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Illuminate\Tests\Redis;

use PHPUnit\Framework\TestCase;
use Illuminate\Redis\RedisManager;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Redis\Connector;

class RedisManagerExtensionTest extends TestCase
{
/**
* Redis manager instance.
*
* @var RedisManager
*/
protected $redis;

protected function setUp(): void
{
parent::setUp();

$this->redis = new RedisManager(new Application(), 'my_custom_driver', [
'default' => [
'host' => 'some-host',
'port' => 'some-port',
'database' => 5,
'timeout' => 0.5,
],
'clusters' => [
'my-cluster' => [
[
'host' => 'some-host',
'port' => 'some-port',
'database' => 5,
'timeout' => 0.5,
],
],
],
]);

$this->redis->extend('my_custom_driver', function () {
return new FakeRedisConnnector();
});
}

public function test_using_custom_redis_connector_with_single_redis_instance()
{
$this->assertEquals(
'my-redis-connection', $this->redis->resolve()
);
}

public function test_using_custom_redis_connector_with_redis_cluster_instance()
{
$this->assertEquals(
'my-redis-cluster-connection', $this->redis->resolve('my-cluster')
);
}
}

class FakeRedisConnnector implements Connector
{
/**
* Create a new clustered Predis connection.
*
* @param array $config
* @param array $options
* @return \Illuminate\Contracts\Redis\Connection
*/
public function connect(array $config, array $options)
{
return 'my-redis-connection';
}

/**
* Create a new clustered Predis connection.
*
* @param array $config
* @param array $clusterOptions
* @param array $options
* @return \Illuminate\Contracts\Redis\Connection
*/
public function connectToCluster(array $config, array $clusterOptions, array $options)
{
return 'my-redis-cluster-connection';
}
}