diff options
Diffstat (limited to 'vendor/symfony/cache/Adapter/CouchbaseBucketAdapter.php')
-rw-r--r-- | vendor/symfony/cache/Adapter/CouchbaseBucketAdapter.php | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Adapter/CouchbaseBucketAdapter.php b/vendor/symfony/cache/Adapter/CouchbaseBucketAdapter.php new file mode 100644 index 0000000..106d7fd --- /dev/null +++ b/vendor/symfony/cache/Adapter/CouchbaseBucketAdapter.php | |||
@@ -0,0 +1,237 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Cache\Adapter; | ||
13 | |||
14 | use Symfony\Component\Cache\Exception\CacheException; | ||
15 | use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
16 | use Symfony\Component\Cache\Marshaller\DefaultMarshaller; | ||
17 | use Symfony\Component\Cache\Marshaller\MarshallerInterface; | ||
18 | |||
19 | trigger_deprecation('symfony/cache', '7.1', 'The "%s" class is deprecated, use "%s" instead.', CouchbaseBucketAdapter::class, CouchbaseCollectionAdapter::class); | ||
20 | |||
21 | /** | ||
22 | * @author Antonio Jose Cerezo Aranda <aj.cerezo@gmail.com> | ||
23 | * | ||
24 | * @deprecated since Symfony 7.1, use {@see CouchbaseCollectionAdapter} instead | ||
25 | */ | ||
26 | class CouchbaseBucketAdapter extends AbstractAdapter | ||
27 | { | ||
28 | private const THIRTY_DAYS_IN_SECONDS = 2592000; | ||
29 | private const MAX_KEY_LENGTH = 250; | ||
30 | private const KEY_NOT_FOUND = 13; | ||
31 | private const VALID_DSN_OPTIONS = [ | ||
32 | 'operationTimeout', | ||
33 | 'configTimeout', | ||
34 | 'configNodeTimeout', | ||
35 | 'n1qlTimeout', | ||
36 | 'httpTimeout', | ||
37 | 'configDelay', | ||
38 | 'htconfigIdleTimeout', | ||
39 | 'durabilityInterval', | ||
40 | 'durabilityTimeout', | ||
41 | ]; | ||
42 | |||
43 | private MarshallerInterface $marshaller; | ||
44 | |||
45 | public function __construct( | ||
46 | private \CouchbaseBucket $bucket, | ||
47 | string $namespace = '', | ||
48 | int $defaultLifetime = 0, | ||
49 | ?MarshallerInterface $marshaller = null, | ||
50 | ) { | ||
51 | if (!static::isSupported()) { | ||
52 | throw new CacheException('Couchbase >= 2.6.0 < 3.0.0 is required.'); | ||
53 | } | ||
54 | |||
55 | $this->maxIdLength = static::MAX_KEY_LENGTH; | ||
56 | |||
57 | parent::__construct($namespace, $defaultLifetime); | ||
58 | $this->enableVersioning(); | ||
59 | $this->marshaller = $marshaller ?? new DefaultMarshaller(); | ||
60 | } | ||
61 | |||
62 | public static function createConnection(#[\SensitiveParameter] array|string $servers, array $options = []): \CouchbaseBucket | ||
63 | { | ||
64 | if (\is_string($servers)) { | ||
65 | $servers = [$servers]; | ||
66 | } | ||
67 | |||
68 | if (!static::isSupported()) { | ||
69 | throw new CacheException('Couchbase >= 2.6.0 < 3.0.0 is required.'); | ||
70 | } | ||
71 | |||
72 | set_error_handler(static fn ($type, $msg, $file, $line) => throw new \ErrorException($msg, 0, $type, $file, $line)); | ||
73 | |||
74 | $dsnPattern = '/^(?<protocol>couchbase(?:s)?)\:\/\/(?:(?<username>[^\:]+)\:(?<password>[^\@]{6,})@)?' | ||
75 | .'(?<host>[^\:]+(?:\:\d+)?)(?:\/(?<bucketName>[^\?]+))(?:\?(?<options>.*))?$/i'; | ||
76 | |||
77 | $newServers = []; | ||
78 | $protocol = 'couchbase'; | ||
79 | try { | ||
80 | $options = self::initOptions($options); | ||
81 | $username = $options['username']; | ||
82 | $password = $options['password']; | ||
83 | |||
84 | foreach ($servers as $dsn) { | ||
85 | if (!str_starts_with($dsn, 'couchbase:')) { | ||
86 | throw new InvalidArgumentException('Invalid Couchbase DSN: it does not start with "couchbase:".'); | ||
87 | } | ||
88 | |||
89 | preg_match($dsnPattern, $dsn, $matches); | ||
90 | |||
91 | $username = $matches['username'] ?: $username; | ||
92 | $password = $matches['password'] ?: $password; | ||
93 | $protocol = $matches['protocol'] ?: $protocol; | ||
94 | |||
95 | if (isset($matches['options'])) { | ||
96 | $optionsInDsn = self::getOptions($matches['options']); | ||
97 | |||
98 | foreach ($optionsInDsn as $parameter => $value) { | ||
99 | $options[$parameter] = $value; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | $newServers[] = $matches['host']; | ||
104 | } | ||
105 | |||
106 | $connectionString = $protocol.'://'.implode(',', $newServers); | ||
107 | |||
108 | $client = new \CouchbaseCluster($connectionString); | ||
109 | $client->authenticateAs($username, $password); | ||
110 | |||
111 | $bucket = $client->openBucket($matches['bucketName']); | ||
112 | |||
113 | unset($options['username'], $options['password']); | ||
114 | foreach ($options as $option => $value) { | ||
115 | if ($value) { | ||
116 | $bucket->$option = $value; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | return $bucket; | ||
121 | } finally { | ||
122 | restore_error_handler(); | ||
123 | } | ||
124 | } | ||
125 | |||
126 | public static function isSupported(): bool | ||
127 | { | ||
128 | return \extension_loaded('couchbase') && version_compare(phpversion('couchbase'), '2.6.0', '>=') && version_compare(phpversion('couchbase'), '3.0', '<'); | ||
129 | } | ||
130 | |||
131 | private static function getOptions(string $options): array | ||
132 | { | ||
133 | $results = []; | ||
134 | $optionsInArray = explode('&', $options); | ||
135 | |||
136 | foreach ($optionsInArray as $option) { | ||
137 | [$key, $value] = explode('=', $option); | ||
138 | |||
139 | if (\in_array($key, static::VALID_DSN_OPTIONS, true)) { | ||
140 | $results[$key] = $value; | ||
141 | } | ||
142 | } | ||
143 | |||
144 | return $results; | ||
145 | } | ||
146 | |||
147 | private static function initOptions(array $options): array | ||
148 | { | ||
149 | $options['username'] ??= ''; | ||
150 | $options['password'] ??= ''; | ||
151 | $options['operationTimeout'] ??= 0; | ||
152 | $options['configTimeout'] ??= 0; | ||
153 | $options['configNodeTimeout'] ??= 0; | ||
154 | $options['n1qlTimeout'] ??= 0; | ||
155 | $options['httpTimeout'] ??= 0; | ||
156 | $options['configDelay'] ??= 0; | ||
157 | $options['htconfigIdleTimeout'] ??= 0; | ||
158 | $options['durabilityInterval'] ??= 0; | ||
159 | $options['durabilityTimeout'] ??= 0; | ||
160 | |||
161 | return $options; | ||
162 | } | ||
163 | |||
164 | protected function doFetch(array $ids): iterable | ||
165 | { | ||
166 | $resultsCouchbase = $this->bucket->get($ids); | ||
167 | |||
168 | $results = []; | ||
169 | foreach ($resultsCouchbase as $key => $value) { | ||
170 | if (null !== $value->error) { | ||
171 | continue; | ||
172 | } | ||
173 | $results[$key] = $this->marshaller->unmarshall($value->value); | ||
174 | } | ||
175 | |||
176 | return $results; | ||
177 | } | ||
178 | |||
179 | protected function doHave(string $id): bool | ||
180 | { | ||
181 | return false !== $this->bucket->get($id); | ||
182 | } | ||
183 | |||
184 | protected function doClear(string $namespace): bool | ||
185 | { | ||
186 | if ('' === $namespace) { | ||
187 | $this->bucket->manager()->flush(); | ||
188 | |||
189 | return true; | ||
190 | } | ||
191 | |||
192 | return false; | ||
193 | } | ||
194 | |||
195 | protected function doDelete(array $ids): bool | ||
196 | { | ||
197 | $results = $this->bucket->remove(array_values($ids)); | ||
198 | |||
199 | foreach ($results as $key => $result) { | ||
200 | if (null !== $result->error && static::KEY_NOT_FOUND !== $result->error->getCode()) { | ||
201 | continue; | ||
202 | } | ||
203 | unset($results[$key]); | ||
204 | } | ||
205 | |||
206 | return 0 === \count($results); | ||
207 | } | ||
208 | |||
209 | protected function doSave(array $values, int $lifetime): array|bool | ||
210 | { | ||
211 | if (!$values = $this->marshaller->marshall($values, $failed)) { | ||
212 | return $failed; | ||
213 | } | ||
214 | |||
215 | $lifetime = $this->normalizeExpiry($lifetime); | ||
216 | |||
217 | $ko = []; | ||
218 | foreach ($values as $key => $value) { | ||
219 | $result = $this->bucket->upsert($key, $value, ['expiry' => $lifetime]); | ||
220 | |||
221 | if (null !== $result->error) { | ||
222 | $ko[$key] = $result; | ||
223 | } | ||
224 | } | ||
225 | |||
226 | return [] === $ko ? true : $ko; | ||
227 | } | ||
228 | |||
229 | private function normalizeExpiry(int $expiry): int | ||
230 | { | ||
231 | if ($expiry && $expiry > static::THIRTY_DAYS_IN_SECONDS) { | ||
232 | $expiry += time(); | ||
233 | } | ||
234 | |||
235 | return $expiry; | ||
236 | } | ||
237 | } | ||