1717namespace Phpfastcache \Drivers \Memcache ;
1818
1919use Phpfastcache \Config \ConfigurationOption ;
20+ use Phpfastcache \Exceptions \PhpfastcacheDriverException ;
2021use Phpfastcache \Exceptions \PhpfastcacheInvalidConfigurationException ;
2122
2223
@@ -28,10 +29,10 @@ class Config extends ConfigurationOption
2829 * Multiple server can be added this way:
2930 * $cfg->setServers([
3031 * [
32+ * // If you use an UNIX socket set the host and port to null
3133 * 'host' => '127.0.0.1',
34+ * //'path' => 'path/to/unix/socket',
3235 * 'port' => 11211,
33- * 'saslUser' => false,
34- * 'saslPassword' => false,
3536 * ]
3637 * ]);
3738 */
@@ -47,52 +48,6 @@ class Config extends ConfigurationOption
4748 */
4849 protected $ port = 11211 ;
4950
50- /**
51- * @var string
52- */
53- protected $ saslUser = '' ;
54-
55- /**
56- * @var string
57- */
58- protected $ saslPassword = '' ;
59-
60- /**
61- * @return bool
62- */
63- public function getSaslUser (): string
64- {
65- return $ this ->saslUser ;
66- }
67-
68- /**
69- * @param string $saslUser
70- * @return self
71- */
72- public function setSaslUser (string $ saslUser ): self
73- {
74- $ this ->saslUser = $ saslUser ;
75- return $ this ;
76- }
77-
78- /**
79- * @return string
80- */
81- public function getSaslPassword (): string
82- {
83- return $ this ->saslPassword ;
84- }
85-
86- /**
87- * @param string $saslPassword
88- * @return self
89- */
90- public function setSaslPassword (string $ saslPassword ): self
91- {
92- $ this ->saslPassword = $ saslPassword ;
93- return $ this ;
94- }
95-
9651 /**
9752 * @return array
9853 */
@@ -109,18 +64,33 @@ public function getServers(): array
10964 public function setServers (array $ servers ): self
11065 {
11166 foreach ($ servers as $ server ) {
112- if ($ diff = array_diff ([ ' host ' , ' port ' , ' saslUser ' , 'saslPassword ' ], array_keys ( $ server) )) {
113- throw new PhpfastcacheInvalidConfigurationException ('Missing keys for memcached server: ' . implode ( ' , ' , $ diff ) );
67+ if (\array_key_exists ( ' saslUser ' , $ server ) || array_key_exists ( 'saslPassword ' , $ server )) {
68+ throw new PhpfastcacheInvalidConfigurationException ('Unlike Memcached, Memcache does not support SASL authentication ' );
11469 }
115- if ($ diff = array_diff (array_keys ($ server ), ['host ' , 'port ' , 'saslUser ' , 'saslPassword ' ])) {
70+
71+ if ($ diff = array_diff (array_keys ($ server ), ['host ' , 'port ' , 'path ' ])) {
11672 throw new PhpfastcacheInvalidConfigurationException ('Unknown keys for memcached server: ' . implode (', ' , $ diff ));
11773 }
118- if (!is_string ($ server ['host ' ])) {
119- throw new PhpfastcacheInvalidConfigurationException ('Host must be a valid string in "$server" configuration array ' );
74+
75+ if (!empty ($ server ['host ' ]) && !empty ($ server ['path ' ])) {
76+ throw new PhpfastcacheInvalidConfigurationException ('Host and path cannot be simultaneous defined. ' );
77+ }
78+
79+ if ((isset ($ server ['host ' ]) && !is_string ($ server ['host ' ])) || (empty ($ server ['path ' ]) && empty ($ server ['host ' ]))) {
80+ throw new PhpfastcacheInvalidConfigurationException ('Host must be a valid string in "$server" configuration array if path is not defined ' );
81+ }
82+
83+ if ((isset ($ server ['path ' ]) && !is_string ($ server ['path ' ])) || (empty ($ server ['host ' ]) && empty ($ server ['path ' ]))) {
84+ throw new PhpfastcacheInvalidConfigurationException ('Path must be a valid string in "$server" configuration array if host is not defined ' );
12085 }
121- if (!is_int ($ server ['port ' ])) {
86+
87+ if (!empty ($ server ['host ' ]) && (empty ($ server ['port ' ]) || !is_int ($ server ['port ' ])|| $ server ['port ' ] < 1 )) {
12288 throw new PhpfastcacheInvalidConfigurationException ('Port must be a valid integer in "$server" configuration array ' );
12389 }
90+
91+ if (!empty ($ server ['port ' ]) && !empty ($ server ['path ' ])) {
92+ throw new PhpfastcacheInvalidConfigurationException ('Port should not be defined along with path ' );
93+ }
12494 }
12595 $ this ->servers = $ servers ;
12696 return $ this ;
@@ -141,6 +111,7 @@ public function getHost(): string
141111 public function setHost (string $ host ): self
142112 {
143113 $ this ->host = $ host ;
114+
144115 return $ this ;
145116 }
146117
@@ -161,4 +132,4 @@ public function setPort(int $port): self
161132 $ this ->port = $ port ;
162133 return $ this ;
163134 }
164- }
135+ }
0 commit comments