Skip to content

Commit 3d382ed

Browse files
committed
Fixed #853 // Configuration validation issue with Memcached socket (path)
1 parent 052f677 commit 3d382ed

File tree

8 files changed

+438
-73
lines changed

8 files changed

+438
-73
lines changed

lib/Phpfastcache/Drivers/Memcache/Config.php

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace Phpfastcache\Drivers\Memcache;
1717

1818
use Phpfastcache\Config\ConfigurationOption;
19+
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
1920
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
2021
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
2122

@@ -27,10 +28,10 @@ class Config extends ConfigurationOption
2728
* Multiple server can be added this way:
2829
* $cfg->setServers([
2930
* [
31+
* // If you use an UNIX socket set the host and port to null
3032
* 'host' => '127.0.0.1',
33+
* //'path' => 'path/to/unix/socket',
3134
* 'port' => 11211,
32-
* 'saslUser' => false,
33-
* 'saslPassword' => false,
3435
* ]
3536
* ]);
3637
*/
@@ -40,49 +41,6 @@ class Config extends ConfigurationOption
4041

4142
protected int $port = 11211;
4243

43-
protected string $saslUser = '';
44-
45-
protected string $saslPassword = '';
46-
47-
/**
48-
* @return string
49-
*/
50-
public function getSaslUser(): string
51-
{
52-
return $this->saslUser;
53-
}
54-
55-
/**
56-
* @param string $saslUser
57-
* @return self
58-
* @throws PhpfastcacheLogicException
59-
*/
60-
public function setSaslUser(string $saslUser): static
61-
{
62-
$this->enforceLockedProperty(__FUNCTION__);
63-
$this->saslUser = $saslUser;
64-
return $this;
65-
}
66-
67-
/**
68-
* @return string
69-
*/
70-
public function getSaslPassword(): string
71-
{
72-
return $this->saslPassword;
73-
}
74-
75-
/**
76-
* @param string $saslPassword
77-
* @return self
78-
* @throws PhpfastcacheLogicException
79-
*/
80-
public function setSaslPassword(string $saslPassword): static
81-
{
82-
$this->enforceLockedProperty(__FUNCTION__);
83-
$this->saslPassword = $saslPassword;
84-
return $this;
85-
}
8644

8745
/**
8846
* @return array
@@ -95,8 +53,6 @@ public function getServers(): array
9553
'host' => $this->getHost(),
9654
'path' => $this->getPath(),
9755
'port' => $this->getPort(),
98-
'saslUser' => $this->getSaslUser() ?: false,
99-
'saslPassword' => $this->getSaslPassword() ?: false,
10056
],
10157
];
10258
}
@@ -114,18 +70,33 @@ public function setServers(array $servers): static
11470
{
11571
$this->enforceLockedProperty(__FUNCTION__);
11672
foreach ($servers as $server) {
117-
if ($diff = array_diff(['host', 'port', 'saslUser', 'saslPassword'], array_keys($server))) {
118-
throw new PhpfastcacheInvalidConfigurationException('Missing keys for memcached server: ' . implode(', ', $diff));
73+
if (\array_key_exists('saslUser', $server) || array_key_exists('saslPassword', $server)) {
74+
throw new PhpfastcacheInvalidConfigurationException('Unlike Memcached, Memcache does not support SASL authentication');
11975
}
120-
if ($diff = array_diff(array_keys($server), ['host', 'port', 'saslUser', 'saslPassword'])) {
76+
77+
if ($diff = array_diff(array_keys($server), ['host', 'port', 'path'])) {
12178
throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff));
12279
}
123-
if (!is_string($server['host'])) {
124-
throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array');
80+
81+
if (!empty($server['host']) && !empty($server['path'])) {
82+
throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.');
83+
}
84+
85+
if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) {
86+
throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined');
87+
}
88+
89+
if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) {
90+
throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined');
12591
}
126-
if (!is_int($server['port'])) {
92+
93+
if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port'])|| $server['port'] < 1)) {
12794
throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array');
12895
}
96+
97+
if (!empty($server['port']) && !empty($server['path'])) {
98+
throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path');
99+
}
129100
}
130101
$this->servers = $servers;
131102
return $this;
@@ -148,6 +119,7 @@ public function setHost(string $host): static
148119
{
149120
$this->enforceLockedProperty(__FUNCTION__);
150121
$this->host = $host;
122+
151123
return $this;
152124
}
153125

lib/Phpfastcache/Drivers/Memcache/Driver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ public function getStats(): DriverStatistic
9898
protected function driverConnect(): bool
9999
{
100100
$this->instance = new MemcacheSoftware();
101-
$servers = $this->getConfig()->getServers();
102101

103-
foreach ($servers as $server) {
102+
foreach ($this->getConfig()->getServers() as $server) {
104103
try {
105104
/**
106105
* If path is provided we consider it as a UNIX Socket

lib/Phpfastcache/Drivers/Memcached/Config.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ class Config extends ConfigurationOption
2727
* Multiple server can be added this way:
2828
* $cfg->setServers([
2929
* [
30+
* // If you use an UNIX socket set the host and port to null
3031
* 'host' => '127.0.0.1',
32+
* //'path' => 'path/to/unix/socket',
3133
* 'port' => 11211,
32-
* 'saslUser' => false,
33-
* 'saslPassword' => false,
34+
* 'saslUser' => null,
35+
* 'saslPassword' => null,
3436
* ]
3537
* ]);
3638
*/
@@ -97,8 +99,8 @@ public function getServers(): array
9799
'host' => $this->getHost(),
98100
'path' => $this->getPath(),
99101
'port' => $this->getPort(),
100-
'saslUser' => $this->getSaslUser() ?: false,
101-
'saslPassword' => $this->getSaslPassword() ?: false,
102+
'saslUser' => $this->getSaslUser() ?: null,
103+
'saslPassword' => $this->getSaslPassword() ?: null,
102104
],
103105
];
104106
}
@@ -116,18 +118,33 @@ public function setServers(array $servers): static
116118
{
117119
$this->enforceLockedProperty(__FUNCTION__);
118120
foreach ($servers as $server) {
119-
if ($diff = array_diff(['host', 'port', 'saslUser', 'saslPassword'], array_keys($server))) {
120-
throw new PhpfastcacheInvalidConfigurationException('Missing keys for memcached server: ' . implode(', ', $diff));
121-
}
122-
if ($diff = array_diff(array_keys($server), ['host', 'port', 'saslUser', 'saslPassword'])) {
121+
if ($diff = array_diff(array_keys($server), ['host', 'port', 'saslUser', 'saslPassword', 'path'])) {
123122
throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff));
124123
}
125-
if (!is_string($server['host'])) {
126-
throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array');
124+
125+
if (!empty($server['host']) && !empty($server['path'])) {
126+
throw new PhpfastcacheInvalidConfigurationException('Host and path cannot be simultaneous defined.');
127+
}
128+
129+
if ((isset($server['host']) && !is_string($server['host'])) || (empty($server['path']) && empty($server['host']))) {
130+
throw new PhpfastcacheInvalidConfigurationException('Host must be a valid string in "$server" configuration array if path is not defined');
131+
}
132+
133+
if ((isset($server['path']) && !is_string($server['path'])) || (empty($server['host']) && empty($server['path']))) {
134+
throw new PhpfastcacheInvalidConfigurationException('Path must be a valid string in "$server" configuration array if host is not defined');
127135
}
128-
if (!is_int($server['port'])) {
136+
137+
if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port'])|| $server['port'] < 1)) {
129138
throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array');
130139
}
140+
141+
if (!empty($server['port']) && !empty($server['path'])) {
142+
throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path');
143+
}
144+
145+
if (!empty($server['saslUser']) && !empty($server['saslPassword']) && (!is_string($server['saslUser']) || !is_string($server['saslPassword']))) {
146+
throw new PhpfastcacheInvalidConfigurationException('If provided, saslUser and saslPassword must be a string');
147+
}
131148
}
132149
$this->servers = $servers;
133150
return $this;

lib/Phpfastcache/Drivers/Memcached/Driver.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ protected function driverConnect(): bool
104104
$this->instance->setOption(MemcachedSoftware::OPT_PREFIX_KEY, $optPrefix);
105105
}
106106

107-
$servers = $this->getConfig()->getServers();
108-
109-
foreach ($servers as $server) {
107+
foreach ($this->getConfig()->getServers() as $server) {
110108
$connected = false;
111109
/**
112110
* If path is provided we consider it as an UNIX Socket

tests/Memcache.test.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@
1313
*/
1414

1515
use Phpfastcache\CacheManager;
16-
use Phpfastcache\Drivers\Memcache\Config as MemcachedConfig;
16+
use Phpfastcache\Drivers\Memcache\Config as MemcacheConfig;
1717
use Phpfastcache\Tests\Helper\TestHelper;
1818

1919
chdir(__DIR__);
2020
require_once __DIR__ . '/../vendor/autoload.php';
2121
$testHelper = new TestHelper('Memcache driver');
2222

2323

24-
$config = new MemcachedConfig();
24+
$config = new MemcacheConfig([
25+
'servers' => [
26+
[
27+
'path' => '',
28+
'host' => '127.0.0.1',
29+
'port' => 11211,
30+
]
31+
]
32+
]);
2533
$config->setItemDetailedDate(true);
2634
$cacheInstance = CacheManager::getInstance('Memcache', $config);
2735
$testHelper->runCRUDTests($cacheInstance);

tests/MemcachedAlternativeConfigurationSyntax.test.php renamed to tests/Memcached.test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
chdir(__DIR__);
2020
require_once __DIR__ . '/../vendor/autoload.php';
21-
$testHelper = new TestHelper('Memcached alternative configuration syntax');
21+
$testHelper = new TestHelper('Memcached');
2222

2323
$cacheInstanceDefSyntax = CacheManager::getInstance('Memcached');
2424

0 commit comments

Comments
 (0)