Пример #1
0
 /**
  * @param  array|\ArrayAccess|RedisSource $resource
  * @return $this
  * @throws \Yafrk\Cache\Exception\InvalidArgumentException
  */
 public function setResource($resource)
 {
     if ($resource instanceof RedisSource) {
         try {
             $resource->ping();
         } catch (\RedisException $ex) {
             throw new Exception\InvalidArgumentException('Invalid redis resource', $ex->getCode(), $ex);
         }
         if ($resource->getOption(RedisSource::OPT_SERIALIZER) == RedisSource::SERIALIZER_NONE) {
             $resource->setOption(RedisSource::OPT_SERIALIZER, RedisSource::SERIALIZER_PHP);
         }
         $this->resource = $resource;
         return $this;
     }
     if (is_string($resource)) {
         $resource = array($resource);
     }
     if (!is_array($resource) && !$resource instanceof \ArrayAccess) {
         throw new Exception\InvalidArgumentException(sprintf('%s: expects an string, array, or Traversable argument; received "%s"', __METHOD__, is_object($resource) ? get_class($resource) : gettype($resource)));
     }
     $host = $port = $auth = $database = null;
     // array(<host>[, <port>[, <auth>, [<database>]]])
     if (isset($resource[0])) {
         if (strpos(':', (string) $resource[0])) {
             list($host, $port) = explode(':', (string) $resource[0]);
         } else {
             $host = (string) $resource[0];
         }
         if (isset($resource[1])) {
             $port = (int) $resource[1];
         }
         if (isset($resource[2])) {
             $auth = (string) $resource[2];
         }
         if (isset($resource[3])) {
             $database = (int) $resource[3];
         }
     } elseif (isset($resource['host'])) {
         if (strpos(':', (string) $resource['host'])) {
             list($host, $port) = explode(':', (string) $resource['host']);
         } else {
             $host = (string) $resource['host'];
         }
         if (isset($resource['port'])) {
             $port = (int) $resource['port'];
         }
         if (isset($resource['auth'])) {
             $auth = (string) $resource['auth'];
         }
         if (isset($resource['database'])) {
             $database = (int) $resource['database'];
         }
     }
     if (!$host) {
         throw new Exception\InvalidArgumentException('Invalid redis resource, option "host" must be given');
     }
     $this->resource = array('host' => $host, 'port' => $port ?: 6379, 'auth' => $auth, 'database' => $database);
 }