PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.9.15
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.9.15
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / vendor_prefixed / phpfastcache / phpfastcache / lib / Phpfastcache / Drivers / Couchdb / Driver.php

Driver.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.15, at vendor_prefixed/phpfastcache/phpfastcache/lib/Phpfastcache/Drivers/Couchdb/Driver.php

239 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 *
5 * This file is part of phpFastCache.
6 *
7 * @license MIT License (MIT)
8 *
9 * For full copyright and license information, please see the docs/CREDITS.txt file.
10 *
11 * @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
12 * @author Georges.L (Geolim4) <contact@geolim4.com>
13 *
14 */
15 declare (strict_types=1);
16 namespace WCPOS\Vendor\Phpfastcache\Drivers\Couchdb;
17
18 use WCPOS\Vendor\Doctrine\CouchDB\CouchDBClient;
19 use WCPOS\Vendor\Doctrine\CouchDB\CouchDBException;
20 use WCPOS\Vendor\Doctrine\CouchDB\HTTP\HTTPException;
21 use WCPOS\Vendor\Phpfastcache\Cluster\AggregatablePoolInterface;
22 use WCPOS\Vendor\Phpfastcache\Core\Pool\DriverBaseTrait;
23 use WCPOS\Vendor\Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
24 use WCPOS\Vendor\Phpfastcache\Entities\DriverStatistic;
25 use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheDriverException;
26 use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
27 use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheLogicException;
28 use WCPOS\Vendor\Psr\Cache\CacheItemInterface;
29 /**
30 * Class Driver
31 * @package phpFastCache\Drivers
32 * @property CouchdbClient $instance Instance of driver service
33 * @property Config $config Config object
34 * @method Config getConfig() Return the config object
35 */
36 class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface
37 {
38 public const COUCHDB_DEFAULT_DB_NAME = 'phpfastcache';
39 // Public because used in config
40 use DriverBaseTrait;
41 /**
42 * @return bool
43 */
44 public function driverCheck() : bool
45 {
46 return \class_exists(CouchDBClient::class);
47 }
48 /**
49 * @return string
50 */
51 public function getHelp() : string
52 {
53 return <<<HELP
54 <p>
55 To install the Couchdb HTTP client library via Composer:
56 <code>composer require "doctrine/couchdb" "@dev"</code>
57 </p>
58 HELP;
59 }
60 /**
61 * @return DriverStatistic
62 */
63 public function getStats() : DriverStatistic
64 {
65 $info = $this->instance->getDatabaseInfo();
66 return (new DriverStatistic())->setSize($info['sizes']['active'] ?? 0)->setRawData($info)->setData(\implode(', ', \array_keys($this->itemInstances)))->setInfo('Couchdb version ' . $this->instance->getVersion() . "\n For more information see RawData.");
67 }
68 /**
69 * @return bool
70 * @throws PhpfastcacheLogicException
71 */
72 protected function driverConnect() : bool
73 {
74 if ($this->instance instanceof CouchDBClient) {
75 throw new PhpfastcacheLogicException('Already connected to Couchdb server');
76 }
77 $clientConfig = $this->getConfig();
78 $url = $clientConfig->isSsl() ? 'https://' : 'http://';
79 if ($clientConfig->getUsername()) {
80 $url .= $clientConfig->getUsername();
81 if ($clientConfig->getPassword()) {
82 $url .= ":{$clientConfig->getPassword()}";
83 }
84 $url .= '@';
85 }
86 $url .= $clientConfig->getHost();
87 $url .= ":{$clientConfig->getPort()}";
88 $url .= '/' . \urlencode($this->getDatabaseName());
89 $this->instance = CouchDBClient::create(['dbname' => $this->getDatabaseName(), 'url' => $url, 'timeout' => $clientConfig->getTimeout()]);
90 $this->createDatabase();
91 return \true;
92 }
93 /**
94 * @return string
95 */
96 protected function getDatabaseName() : string
97 {
98 return $this->getConfig()->getDatabase() ?: self::COUCHDB_DEFAULT_DB_NAME;
99 }
100 /**
101 * @return void
102 */
103 protected function createDatabase()
104 {
105 try {
106 $this->instance->getDatabaseInfo($this->getDatabaseName());
107 } catch (HTTPException $e) {
108 $this->instance->createDatabase($this->getDatabaseName());
109 }
110 }
111 protected function getCouchDbItemKey(CacheItemInterface $item)
112 {
113 return 'pfc_' . $item->getEncodedKey();
114 }
115 /**
116 * @param CacheItemInterface $item
117 * @return null|array
118 * @throws PhpfastcacheDriverException
119 */
120 protected function driverRead(CacheItemInterface $item)
121 {
122 try {
123 $response = $this->instance->findDocument($this->getCouchDbItemKey($item));
124 } catch (CouchDBException $e) {
125 throw new PhpfastcacheDriverException('Got error while trying to get a document: ' . $e->getMessage(), 0, $e);
126 }
127 if ($response->status === 404 || empty($response->body[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX])) {
128 return null;
129 }
130 if ($response->status === 200) {
131 return $this->decode($response->body);
132 }
133 throw new PhpfastcacheDriverException('Got unexpected HTTP status: ' . $response->status);
134 }
135 /**
136 * @param CacheItemInterface $item
137 * @return bool
138 * @throws PhpfastcacheDriverException
139 * @throws PhpfastcacheInvalidArgumentException
140 */
141 protected function driverWrite(CacheItemInterface $item) : bool
142 {
143 /**
144 * Check for Cross-Driver type confusion
145 */
146 if ($item instanceof Item) {
147 try {
148 $this->instance->putDocument($this->encodeDocument($this->driverPreWrap($item)), $this->getCouchDbItemKey($item), $this->getLatestDocumentRevision($this->getCouchDbItemKey($item)));
149 } catch (CouchDBException $e) {
150 throw new PhpfastcacheDriverException('Got error while trying to upsert a document: ' . $e->getMessage(), 0, $e);
151 }
152 return \true;
153 }
154 throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected');
155 }
156 /**
157 * @return string|null
158 */
159 protected function getLatestDocumentRevision($docId)
160 {
161 $path = '/' . \urlencode($this->getDatabaseName()) . '/' . \urlencode($docId);
162 $response = $this->instance->getHttpClient()->request('HEAD', $path, null, \false);
163 if (!empty($response->headers['etag'])) {
164 return \trim($response->headers['etag'], " '\"\t\n\r\x00\v");
165 }
166 return null;
167 }
168 /********************
169 *
170 * PSR-6 Extended Methods
171 *
172 *******************/
173 /**
174 * @param CacheItemInterface $item
175 * @return bool
176 * @throws PhpfastcacheDriverException
177 * @throws PhpfastcacheInvalidArgumentException
178 */
179 protected function driverDelete(CacheItemInterface $item) : bool
180 {
181 /**
182 * Check for Cross-Driver type confusion
183 */
184 if ($item instanceof Item) {
185 try {
186 $this->instance->deleteDocument($this->getCouchDbItemKey($item), $this->getLatestDocumentRevision($this->getCouchDbItemKey($item)));
187 } catch (CouchDBException $e) {
188 throw new PhpfastcacheDriverException('Got error while trying to delete a document: ' . $e->getMessage(), 0, $e);
189 }
190 return \true;
191 }
192 throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected');
193 }
194 /**
195 * @return bool
196 * @throws PhpfastcacheDriverException
197 */
198 protected function driverClear() : bool
199 {
200 try {
201 $this->instance->deleteDatabase($this->getDatabaseName());
202 $this->createDatabase();
203 } catch (CouchDBException $e) {
204 throw new PhpfastcacheDriverException('Got error while trying to delete and recreate the database: ' . $e->getMessage(), 0, $e);
205 }
206 return \true;
207 }
208 /**
209 * @param array $data
210 * @return array
211 */
212 protected function encodeDocument(array $data) : array
213 {
214 $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]);
215 return $data;
216 }
217 /**
218 * Specific document decoder for Couchdb
219 * since we dont store encoded version
220 * for performance purposes
221 *
222 * @param $value
223 * @return mixed
224 * @throws \Exception
225 */
226 protected function decode($value)
227 {
228 $value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = \unserialize($value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX], ['allowed_classes' => \true]);
229 $value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = new \DateTime($value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['date'], new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['timezone']));
230 if (isset($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX])) {
231 $value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = new \DateTime($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['date'], new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['timezone']));
232 }
233 if (isset($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX])) {
234 $value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new \DateTime($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['date'], new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['timezone']));
235 }
236 return $value;
237 }
238 }
239