Ejemplo n.º 1
0
 /**
  * @param array|\ArrayAccess|MemcachedSource $resource
  * @throws \Cache\Exception\InvalidArgumentException
  * @return Memcached
  */
 public function setResource($resource)
 {
     if ($resource instanceof MemcachedSource) {
         if (!$resource->getVersion()) {
             throw new Exception\InvalidArgumentException('Invalid memcached resource');
         }
         $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 = $weight = $persistent_id = null;
     // array(<host>[, <port>[, <weight> [,<persistent_id>]]])
     if (isset($resource[0])) {
         $host = (string) $resource[0];
         if (isset($resource[1])) {
             $port = (int) $resource[1];
         }
         if (isset($resource[2])) {
             $weight = (string) $resource[2];
         }
         if (isset($resource[3])) {
             $persistent_id = (string) $resource[3];
         }
     } elseif (isset($resource['host'])) {
         $host = (string) $resource['host'];
         if (isset($resource['port'])) {
             $port = (int) $resource['port'];
         }
         if (isset($resource['weight'])) {
             $weight = (int) $resource['weight'];
         }
         if (isset($resource['persistent_id'])) {
             $persistent_id = (string) $resource['persistent_id'];
         }
     }
     if (!$host) {
         throw new Exception\InvalidArgumentException('Invalid memcached resource, option "host" must be given');
     }
     $this->resource = array('host' => $host, 'port' => $port, 'weight' => $weight, 'persistent_id' => $persistent_id);
 }