factory = $factory; $this->serverList = $serverList; } /** * Get client for the given realm * * @param string $realm * @param boolean $allowDefault * * @return mixed */ public function getClient($realm = self::REALM_DEFAULT, $allowDefault = true) { if (!isset($this->clients[$realm])) { $client = $this->createClient($realm); if (false === $client) { if (self::REALM_DEFAULT !== $realm && $allowDefault) { $this->clients[$realm] = $this->getClient(self::REALM_DEFAULT); } else { throw new InvalidArgumentException(sprintf("Could not find client for realm '%s'", $realm)); } } else { $this->clients[$realm] = $client; } } return $this->clients[$realm]; } /** * Build connection parameters array from current Drupal settings * * @param string $realm * * @return boolean|string[] * A key-value pairs of configuration values or false if realm is * not defined per-configuration */ private function buildOptions($realm) { $info = null; if (isset($this->serverList[$realm])) { $info = $this->serverList[$realm]; } else { return false; } $info += array( 'host' => self::REDIS_DEFAULT_HOST, 'port' => self::REDIS_DEFAULT_PORT, 'base' => self::REDIS_DEFAULT_BASE, 'password' => self::REDIS_DEFAULT_PASSWORD, 'socket' => self::REDIS_DEFAULT_SOCKET ); return array_filter($info); } /** * Get client singleton */ private function createClient($realm) { $info = $this->buildOptions($realm); if (false === $info) { return false; } return $this->factory->getClient($info); } }