123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
-
- namespace Illuminate\Redis;
-
- use InvalidArgumentException;
- use Illuminate\Contracts\Redis\Factory;
-
- /**
- * @mixin \Illuminate\Redis\Connections\Connection
- */
- class RedisManager implements Factory
- {
- /**
- * The name of the default driver.
- *
- * @var string
- */
- protected $driver;
-
- /**
- * The Redis server configurations.
- *
- * @var array
- */
- protected $config;
-
- /**
- * The Redis connections.
- *
- * @var mixed
- */
- protected $connections;
-
- /**
- * Create a new Redis manager instance.
- *
- * @param string $driver
- * @param array $config
- * @return void
- */
- public function __construct($driver, array $config)
- {
- $this->driver = $driver;
- $this->config = $config;
- }
-
- /**
- * Get a Redis connection by name.
- *
- * @param string|null $name
- * @return \Illuminate\Redis\Connections\Connection
- */
- public function connection($name = null)
- {
- $name = $name ?: 'default';
-
- if (isset($this->connections[$name])) {
- return $this->connections[$name];
- }
-
- return $this->connections[$name] = $this->resolve($name);
- }
-
- /**
- * Resolve the given connection by name.
- *
- * @param string|null $name
- * @return \Illuminate\Redis\Connections\Connection
- *
- * @throws \InvalidArgumentException
- */
- public function resolve($name = null)
- {
- $name = $name ?: 'default';
-
- $options = $this->config['options'] ?? [];
-
- if (isset($this->config[$name])) {
- return $this->connector()->connect($this->config[$name], $options);
- }
-
- if (isset($this->config['clusters'][$name])) {
- return $this->resolveCluster($name);
- }
-
- throw new InvalidArgumentException("Redis connection [{$name}] not configured.");
- }
-
- /**
- * Resolve the given cluster connection by name.
- *
- * @param string $name
- * @return \Illuminate\Redis\Connections\Connection
- */
- protected function resolveCluster($name)
- {
- $clusterOptions = $this->config['clusters']['options'] ?? [];
-
- return $this->connector()->connectToCluster(
- $this->config['clusters'][$name], $clusterOptions, $this->config['options'] ?? []
- );
- }
-
- /**
- * Get the connector instance for the current driver.
- *
- * @return \Illuminate\Redis\Connectors\PhpRedisConnector|\Illuminate\Redis\Connectors\PredisConnector
- */
- protected function connector()
- {
- switch ($this->driver) {
- case 'predis':
- return new Connectors\PredisConnector;
- case 'phpredis':
- return new Connectors\PhpRedisConnector;
- }
- }
-
- /**
- * Return all of the created connections.
- *
- * @return array
- */
- public function connections()
- {
- return $this->connections;
- }
-
- /**
- * Pass methods onto the default Redis connection.
- *
- * @param string $method
- * @param array $parameters
- * @return mixed
- */
- public function __call($method, $parameters)
- {
- return $this->connection()->{$method}(...$parameters);
- }
- }
|