1616namespace Phpfastcache \Drivers \Memcache ;
1717
1818use Phpfastcache \Config \ConfigurationOption ;
19+ use Phpfastcache \Exceptions \PhpfastcacheDriverException ;
1920use Phpfastcache \Exceptions \PhpfastcacheInvalidConfigurationException ;
2021use 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
0 commit comments