Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reduced arg-by-ref of MemcachedResourceManager
  • Loading branch information
marc-mabe committed Apr 18, 2016
commit 0c21ec44d7caed79d71678d64386eefe337c0dbb
87 changes: 43 additions & 44 deletions src/Storage/Adapter/MemcachedResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public function getServers($id)
throw new Exception\RuntimeException("No resource with id '{$id}'");
}

$resource = & $this->resources[$id];

$resource = $this->resources[$id];
if ($resource instanceof MemcachedResource) {
return $resource->getServerList();
}
Expand All @@ -51,10 +50,11 @@ public function getServers($id)
* Normalize one server into the following format:
* array('host' => <host>, 'port' => <port>, 'weight' => <weight>)
*
* @param string|array &$server
* @param string|array $server
* @return array
* @throws Exception\InvalidArgumentException
*/
protected function normalizeServer(&$server)
protected function normalizeServer($server)
{
$host = null;
$port = 11211;
Expand Down Expand Up @@ -107,7 +107,7 @@ protected function normalizeServer(&$server)
throw new Exception\InvalidArgumentException('Missing required server host');
}

$server = [
return [
'host' => $host,
'port' => $port,
'weight' => $weight,
Expand Down Expand Up @@ -195,9 +195,9 @@ public function setResource($id, $resource)
], $resource);

// normalize and validate params
$this->normalizePersistentId($resource['persistent_id']);
$this->normalizeLibOptions($resource['lib_options']);
$this->normalizeServers($resource['servers']);
$resource['persistent_id'] = $this->normalizePersistentId($resource['persistent_id']);
$resource['lib_options'] = $this->normalizeLibOptions($resource['lib_options']);
$resource['servers'] = $this->normalizeServers($resource['servers']);
}

$this->resources[$id] = $resource;
Expand Down Expand Up @@ -232,15 +232,14 @@ public function setPersistentId($id, $persistentId)
]);
}

$resource = & $this->resources[$id];
if ($resource instanceof MemcachedResource) {
if ($this->resources[$id] instanceof MemcachedResource) {
throw new Exception\RuntimeException(
"Can't change persistent id of resource {$id} after instanziation"
);
}

$this->normalizePersistentId($persistentId);
$resource['persistent_id'] = $persistentId;
$persistentId = $this->normalizePersistentId($persistentId);
$this->resources[$id]['persistent_id'] = $persistentId;

return $this;
}
Expand All @@ -258,8 +257,7 @@ public function getPersistentId($id)
throw new Exception\RuntimeException("No resource with id '{$id}'");
}

$resource = & $this->resources[$id];

$resource = $this->resources[$id];
if ($resource instanceof MemcachedResource) {
throw new Exception\RuntimeException(
"Can't get persistent id of an instantiated memcached resource"
Expand All @@ -272,11 +270,12 @@ public function getPersistentId($id)
/**
* Normalize the persistent id
*
* @param string $persistentId
* @param string $persistentId
* @return string
*/
protected function normalizePersistentId(& $persistentId)
protected function normalizePersistentId($persistentId)
{
$persistentId = (string) $persistentId;
return (string) $persistentId;
}

/**
Expand All @@ -294,9 +293,9 @@ public function setLibOptions($id, array $libOptions)
]);
}

$this->normalizeLibOptions($libOptions);
$libOptions = $this->normalizeLibOptions($libOptions);

$resource = & $this->resources[$id];
$resource = $this->resources[$id];
if ($resource instanceof MemcachedResource) {
if (method_exists($resource, 'setOptions')) {
$resource->setOptions($libOptions);
Expand All @@ -306,7 +305,7 @@ public function setLibOptions($id, array $libOptions)
}
}
} else {
$resource['lib_options'] = $libOptions;
$this->resources[$id]['lib_options'] = $libOptions;
}

return $this;
Expand All @@ -325,7 +324,7 @@ public function getLibOptions($id)
throw new Exception\RuntimeException("No resource with id '{$id}'");
}

$resource = & $this->resources[$id];
$resource = $this->resources[$id];

if ($resource instanceof MemcachedResource) {
$libOptions = [];
Expand Down Expand Up @@ -368,8 +367,8 @@ public function getLibOption($id, $key)
throw new Exception\RuntimeException("No resource with id '{$id}'");
}

$this->normalizeLibOptionKey($key);
$resource = & $this->resources[$id];
$key = $this->normalizeLibOptionKey($key);
$resource = $this->resources[$id];

if ($resource instanceof MemcachedResource) {
return $resource->getOption($key);
Expand All @@ -381,10 +380,11 @@ public function getLibOption($id, $key)
/**
* Normalize libmemcached options
*
* @param array|Traversable $libOptions
* @param array|Traversable $libOptions
* @return array
* @throws Exception\InvalidArgumentException
*/
protected function normalizeLibOptions(& $libOptions)
protected function normalizeLibOptions($libOptions)
{
if (!is_array($libOptions) && !($libOptions instanceof Traversable)) {
throw new Exception\InvalidArgumentException(
Expand All @@ -394,31 +394,31 @@ protected function normalizeLibOptions(& $libOptions)

$result = [];
foreach ($libOptions as $key => $value) {
$this->normalizeLibOptionKey($key);
$key = $this->normalizeLibOptionKey($key);
$result[$key] = $value;
}

$libOptions = $result;
return $result;
}

/**
* Convert option name into it's constant value
*
* @param string|int $key
* @param string|int $key
* @return int
* @throws Exception\InvalidArgumentException
*/
protected function normalizeLibOptionKey(& $key)
protected function normalizeLibOptionKey($key)
{
// convert option name into it's constant value
if (is_string($key)) {
$const = 'Memcached::OPT_' . str_replace([' ', '-'], '_', strtoupper($key));
if (!defined($const)) {
throw new Exception\InvalidArgumentException("Unknown libmemcached option '{$key}' ({$const})");
}
$key = constant($const);
} else {
$key = (int) $key;
return constant($const);
}
return (int) $key;
}

/**
Expand All @@ -442,17 +442,16 @@ public function setServers($id, $servers)
]);
}

$this->normalizeServers($servers);

$resource = & $this->resources[$id];
$servers = $this->normalizeServers($servers);
$resource = $this->resources[$id];
if ($resource instanceof MemcachedResource) {
// don't add servers twice
$servers = array_udiff($servers, $resource->getServerList(), [$this, 'compareServers']);
if ($servers) {
$resource->addServers($servers);
}
} else {
$resource['servers'] = $servers;
$this->resources[$id]['servers'] = $servers;
}

return $this;
Expand All @@ -473,9 +472,8 @@ public function addServers($id, $servers)
]);
}

$this->normalizeServers($servers);

$resource = & $this->resources[$id];
$servers = $this->normalizeServers($servers);
$resource = $this->resources[$id];
if ($resource instanceof MemcachedResource) {
// don't add servers twice
$servers = array_udiff($servers, $resource->getServerList(), [$this, 'compareServers']);
Expand All @@ -484,7 +482,7 @@ public function addServers($id, $servers)
}
} else {
// don't add servers twice
$resource['servers'] = array_merge(
$this->resources[$id]['servers'] = array_merge(
$resource['servers'],
array_udiff($servers, $resource['servers'], [$this, 'compareServers'])
);
Expand All @@ -509,9 +507,10 @@ public function addServer($id, $server)
* Normalize a list of servers into the following format:
* array(array('host' => <host>, 'port' => <port>, 'weight' => <weight>)[, ...])
*
* @param string|array $servers
* @param string|array $servers
* @return array[]
*/
protected function normalizeServers(& $servers)
protected function normalizeServers($servers)
{
if (!is_array($servers) && !$servers instanceof Traversable) {
// Convert string into a list of servers
Expand All @@ -520,11 +519,11 @@ protected function normalizeServers(& $servers)

$result = [];
foreach ($servers as $server) {
$this->normalizeServer($server);
$server = $this->normalizeServer($server);
$result[$server['host'] . ':' . $server['port']] = $server;
}

$servers = array_values($result);
return array_values($result);
}

/**
Expand Down