woocommerce-pos
/
vendor_prefixed
/
phpfastcache
/
phpfastcache
/
lib
/
Phpfastcache
/
Drivers
/
Couchbase
/
Driver.php
Driver.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.14, at vendor_prefixed/phpfastcache/phpfastcache/lib/Phpfastcache/Drivers/Couchbase/Driver.php
| 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\Couchbase; |
| 17 | |
| 18 | use WCPOS\Vendor\Couchbase\Exception as CouchbaseException; |
| 19 | use WCPOS\Vendor\Couchbase\PasswordAuthenticator; |
| 20 | use Couchbase\Bucket as CouchbaseBucket; |
| 21 | use Couchbase\Cluster as CouchbaseClient; |
| 22 | use DateTime; |
| 23 | use WCPOS\Vendor\Phpfastcache\Cluster\AggregatablePoolInterface; |
| 24 | use WCPOS\Vendor\Phpfastcache\Core\Pool\DriverBaseTrait; |
| 25 | use WCPOS\Vendor\Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
| 26 | use WCPOS\Vendor\Phpfastcache\Config\ConfigurationOption; |
| 27 | use WCPOS\Vendor\Phpfastcache\Entities\DriverStatistic; |
| 28 | use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheDriverCheckException; |
| 29 | use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
| 30 | use WCPOS\Vendor\Phpfastcache\Exceptions\PhpfastcacheLogicException; |
| 31 | use WCPOS\Vendor\Psr\Cache\CacheItemInterface; |
| 32 | /** |
| 33 | * Class Driver |
| 34 | * @package phpFastCache\Drivers |
| 35 | * @property CouchbaseClient $instance Instance of driver service |
| 36 | * @property Config $config Config object |
| 37 | * @method Config getConfig() Return the config object |
| 38 | */ |
| 39 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
| 40 | { |
| 41 | use DriverBaseTrait { |
| 42 | __construct as __baseConstruct; |
| 43 | } |
| 44 | /** |
| 45 | * @var CouchbaseBucket[] |
| 46 | */ |
| 47 | protected $bucketInstances = []; |
| 48 | /** |
| 49 | * @var CouchbaseBucket |
| 50 | */ |
| 51 | protected $bucketInstance; |
| 52 | /** |
| 53 | * @var string |
| 54 | */ |
| 55 | protected $currentBucket = ''; |
| 56 | public function __construct(ConfigurationOption $config, $instanceId) |
| 57 | { |
| 58 | // @todo Deprecation to enable in v8.1 |
| 59 | // \trigger_error('Couchbase driver is now deprecated and will be removed in the V9, use Couchbasev3 instead which will support SDK 3.', \E_USER_DEPRECATED); |
| 60 | $this->__baseConstruct($config, $instanceId); |
| 61 | } |
| 62 | /** |
| 63 | * @return bool |
| 64 | */ |
| 65 | public function driverCheck() : bool |
| 66 | { |
| 67 | return \extension_loaded('couchbase'); |
| 68 | } |
| 69 | /** |
| 70 | * @return DriverStatistic |
| 71 | */ |
| 72 | public function getStats() : DriverStatistic |
| 73 | { |
| 74 | $info = $this->getBucket()->manager()->info(); |
| 75 | return (new DriverStatistic())->setSize($info['basicStats']['diskUsed'])->setRawData($info)->setData(\implode(', ', \array_keys($this->itemInstances)))->setInfo('CouchBase version ' . $info['nodes'][0]['version'] . ', Uptime (in days): ' . \round($info['nodes'][0]['uptime'] / 86400, 1) . "\n For more information see RawData."); |
| 76 | } |
| 77 | /** |
| 78 | * @return bool |
| 79 | * @throws PhpfastcacheLogicException |
| 80 | */ |
| 81 | protected function driverConnect() : bool |
| 82 | { |
| 83 | if (\class_exists(\Couchbase\ClusterOptions::class)) { |
| 84 | throw new PhpfastcacheDriverCheckException('You are using the Couchbase PHP SDK 3.x so please use driver Couchbasev3'); |
| 85 | } |
| 86 | if ($this->instance instanceof CouchbaseClient) { |
| 87 | throw new PhpfastcacheLogicException('Already connected to Couchbase server'); |
| 88 | } |
| 89 | $clientConfig = $this->getConfig(); |
| 90 | $authenticator = new PasswordAuthenticator(); |
| 91 | $authenticator->username($clientConfig->getUsername())->password($clientConfig->getPassword()); |
| 92 | $this->instance = new CouchbaseClient('couchbase://' . $clientConfig->getHost() . ($clientConfig->getPort() ? ":{$clientConfig->getPort()}" : '')); |
| 93 | $this->instance->authenticate($authenticator); |
| 94 | $this->setBucket($this->instance->openBucket($clientConfig->getBucketName())); |
| 95 | return \true; |
| 96 | } |
| 97 | /** |
| 98 | * @param CouchbaseBucket $CouchbaseBucket |
| 99 | */ |
| 100 | protected function setBucket(CouchbaseBucket $CouchbaseBucket) |
| 101 | { |
| 102 | $this->bucketInstance = $CouchbaseBucket; |
| 103 | } |
| 104 | /** |
| 105 | * @param CacheItemInterface $item |
| 106 | * @return null|array |
| 107 | */ |
| 108 | protected function driverRead(CacheItemInterface $item) |
| 109 | { |
| 110 | try { |
| 111 | /** |
| 112 | * CouchbaseBucket::get() returns a CouchbaseMetaDoc object |
| 113 | */ |
| 114 | return $this->decodeDocument((array) $this->getBucket()->get($item->getEncodedKey())->value); |
| 115 | } catch (CouchbaseException $e) { |
| 116 | return null; |
| 117 | } |
| 118 | } |
| 119 | /** |
| 120 | * @return CouchbaseBucket |
| 121 | */ |
| 122 | protected function getBucket() : CouchbaseBucket |
| 123 | { |
| 124 | return $this->bucketInstance; |
| 125 | } |
| 126 | /** |
| 127 | * @param CacheItemInterface $item |
| 128 | * @return bool |
| 129 | * @throws PhpfastcacheInvalidArgumentException |
| 130 | */ |
| 131 | protected function driverWrite(CacheItemInterface $item) : bool |
| 132 | { |
| 133 | /** |
| 134 | * Check for Cross-Driver type confusion |
| 135 | */ |
| 136 | if ($item instanceof Item) { |
| 137 | try { |
| 138 | return (bool) $this->getBucket()->upsert($item->getEncodedKey(), $this->encodeDocument($this->driverPreWrap($item)), ['expiry' => $item->getTtl()]); |
| 139 | } catch (CouchbaseException $e) { |
| 140 | return \false; |
| 141 | } |
| 142 | } |
| 143 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
| 144 | } |
| 145 | /** |
| 146 | * @param CacheItemInterface $item |
| 147 | * @return bool |
| 148 | * @throws PhpfastcacheInvalidArgumentException |
| 149 | */ |
| 150 | protected function driverDelete(CacheItemInterface $item) : bool |
| 151 | { |
| 152 | /** |
| 153 | * Check for Cross-Driver type confusion |
| 154 | */ |
| 155 | if ($item instanceof Item) { |
| 156 | try { |
| 157 | return (bool) $this->getBucket()->remove($item->getEncodedKey()); |
| 158 | } catch (Exception $e) { |
| 159 | return $e->getCode() === COUCHBASE_KEY_ENOENT; |
| 160 | } |
| 161 | } |
| 162 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
| 163 | } |
| 164 | /** |
| 165 | * @param array $data |
| 166 | * @return array |
| 167 | */ |
| 168 | protected function encodeDocument(array $data) : array |
| 169 | { |
| 170 | $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
| 171 | $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
| 172 | if ($this->getConfig()->isItemDetailedDate()) { |
| 173 | $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
| 174 | $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]->format(\DateTime::ATOM); |
| 175 | } |
| 176 | return $data; |
| 177 | } |
| 178 | /** |
| 179 | * @param array $data |
| 180 | * @return array |
| 181 | */ |
| 182 | protected function decodeDocument(array $data) : array |
| 183 | { |
| 184 | $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->decode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
| 185 | $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = \DateTime::createFromFormat(\DateTime::ATOM, $data[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]); |
| 186 | if ($this->getConfig()->isItemDetailedDate()) { |
| 187 | $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = \DateTime::createFromFormat(\DateTime::ATOM, $data[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]); |
| 188 | $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = \DateTime::createFromFormat(\DateTime::ATOM, $data[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]); |
| 189 | } |
| 190 | return $data; |
| 191 | } |
| 192 | /******************** |
| 193 | * |
| 194 | * PSR-6 Extended Methods |
| 195 | * |
| 196 | *******************/ |
| 197 | /** |
| 198 | * @return bool |
| 199 | */ |
| 200 | protected function driverClear() : bool |
| 201 | { |
| 202 | $this->getBucket()->manager()->flush(); |
| 203 | return \true; |
| 204 | } |
| 205 | } |
| 206 |