Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
zendframework/zendframework#6495 - Cleaned up `Zend\Cache\Storage\Ada…
Browse files Browse the repository at this point in the history
…pter\RedisResourceManager#setServer` and `#setResource` implementation
  • Loading branch information
Ocramius committed Aug 6, 2014
1 parent 5ee9214 commit 73c1c89
Showing 1 changed file with 50 additions and 18 deletions.
68 changes: 50 additions & 18 deletions src/Storage/Adapter/RedisResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
class RedisResourceManager
{

/**
* Registered resources
*
Expand Down Expand Up @@ -162,15 +161,15 @@ public function getServer($id)
* array('host' => <host>[, 'port' => <port>[, 'timeout' => <timeout>]])
*
* @param string|array $server
* @return string|null password parsed from server URI string if any
*
* @throws Exception\InvalidArgumentException
*/
protected function normalizeServer(&$server)
{
$password = null;
$host = null;
$port = null;
$timeout = 0;

// convert a single server into an array
if ($server instanceof Traversable) {
$server = ArrayUtils::iteratorToArray($server);
Expand All @@ -197,7 +196,6 @@ protected function normalizeServer(&$server)
if (strpos($server, '/') !== 0) {
//non unix domain socket connection
$server = parse_url($server);
$password = isset($server['pass']) ? $server['pass'] : null;
} else {
$server = array('host' => $server);
}
Expand All @@ -219,7 +217,37 @@ protected function normalizeServer(&$server)
'port' => $port,
'timeout' => $timeout,
);
return $password;
}

/**
* Extract password to be used on connection
*
* @param mixed $resource
* @param mixed $serverUri
*
* @return string|null
*/
protected function extractPassword($resource, $serverUri)
{
if (! empty($resource['password'])) {
return $resource['password'];
}

if (! is_string($serverUri)) {
return null;
}

// parse server from URI host{:?port}
$server = trim($serverUri);

if (strpos($server, '/') === 0) {
return null;
}

//non unix domain socket connection
$server = parse_url($server);

return isset($server['pass']) ? $server['pass'] : null;
}

/**
Expand Down Expand Up @@ -292,10 +320,12 @@ public function setResource($id, $resource)
// normalize and validate params
$this->normalizePersistentId($resource['persistent_id']);
$this->normalizeLibOptions($resource['lib_options']);
$password = $this->normalizeServer($resource['server']);
if (empty($resource['password']) && $password !== null) {
$resource['password'] = $password;
}

// #6495 note: order is important here, as `normalizeServer` applies destructive
// transformations on $resource['server']
$resource['password'] = $this->extractPassword($resource, $resource['server']);

$this->normalizeServer($resource['server']);
} else {
//there are two ways of determining if redis is already initialized
//with connect function:
Expand Down Expand Up @@ -550,21 +580,23 @@ public function setServer($id, $server)
));
}

$password = $this->normalizeServer($server);
$this->normalizeServer($server);

$resource = & $this->resources[$id];
$resource['password'] = $this->extractPassword($resource, $server);

$resource = & $this->resources[$id];
if ($resource['resource'] instanceof RedisResource) {
if (empty($resource['password']) && $password) {
$this->setResource($id, array('server' => $server, 'password' => $password));
} else {
$this->setResource($id, array('server' => $server));
$resourceParams = array('server' => $server);

if (! empty($resource['password'])) {
$resourceParams['password'] = $resource['password'];
}

$this->setResource($id, $resourceParams);
} else {
$resource['server'] = $server;
if (empty($resource['password']) && $password) {
$resource['password'] = $password;
}
}

return $this;
}

Expand Down

0 comments on commit 73c1c89

Please sign in to comment.