Explorar o código

Makes it easier to use a custom parameters class.

Simply overriding one method of the standard connection factory class,
used internally by Predis, allows developers to use their own custom
connection parameters classes through the whole library.

For example, in order to support Heroku-style URIs one can do:

  class HerokuParameters extends Predis\Connection\Parameters
  {
    protected function filter(array $parameters)
    {
      if (
        isset($parameters['scheme']) &&
        $parameters['scheme'] === 'redis'
      ) {
        $parameters['scheme'] = 'tcp';
      }

      if (isset($parameters['pass'])) {
        $parameters['password'] = $parameters['pass'];
      }

      unset($parameters['user'], $parameters['pass']);

      return $parameters;
    }
  }

  class ConnectionFactory extends Predis\Connection\Factory
  {
    protected function createParameters($parameters)
    {
      return HerokuParameters::create($parameters);
    }
  }

  $client = new Predis\Client($_ENV['REDISCLOUD_URL'], [
    'connections' => new ConnectionFactory()
  ]);

This idea comes in response to #196, but since we do not want to bake
support for SaaS-specific URIs in Predis this is the best compromise
we can offer leave developers free to implement their own logic for
handling these kind of URIs with the additional benefit of having it
available through the whole library (think of cluster or replication
where you have multiple nodes, thus multiple instances of connection
parameters to create).
Daniele Alessandri %!s(int64=11) %!d(string=hai) anos
pai
achega
31e2516156
Modificáronse 2 ficheiros con 14 adicións e 3 borrados
  1. 12 1
      src/Connection/Factory.php
  2. 2 2
      src/Connection/Parameters.php

+ 12 - 1
src/Connection/Factory.php

@@ -76,7 +76,7 @@ class Factory implements FactoryInterface
     public function create($parameters)
     {
         if (!$parameters instanceof ParametersInterface) {
-            $parameters = Parameters::create($parameters);
+            $parameters = $this->createParameters($parameters);
         }
 
         $scheme = $parameters->scheme;
@@ -114,6 +114,17 @@ class Factory implements FactoryInterface
         }
     }
 
+    /**
+     * Creates a connection parameters instance from the supplied argument.
+     *
+     * @param  mixed               $parameters Original connection parameters.
+     * @return ParametersInterface
+     */
+    protected function createParameters($parameters)
+    {
+        return Parameters::create($parameters);
+    }
+
     /**
      * Prepares a connection instance after its initialization.
      *

+ 2 - 2
src/Connection/Parameters.php

@@ -57,10 +57,10 @@ class Parameters implements ParametersInterface
     public static function create($parameters)
     {
         if (is_string($parameters)) {
-            $parameters = self::parse($parameters);
+            $parameters = static::parse($parameters);
         }
 
-        return new self($parameters ?: array());
+        return new static($parameters ?: array());
     }
 
     /**