@@ -20,257 +20,257 @@ |
||
| 20 | 20 | |
| 21 | 21 | class Config extends ConfigurationOption |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - protected $host = '127.0.0.1'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var int |
|
| 30 | - */ |
|
| 31 | - protected $port = 27017; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var int |
|
| 35 | - */ |
|
| 36 | - protected $timeout = 3; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 41 | - protected $username = ''; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var string |
|
| 45 | - */ |
|
| 46 | - protected $password = ''; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @var array |
|
| 50 | - */ |
|
| 51 | - protected $servers = []; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - protected $collectionName = 'phpfastcache'; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @var string |
|
| 60 | - */ |
|
| 61 | - protected $databaseName = Driver::MONGODB_DEFAULT_DB_NAME; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @var array |
|
| 65 | - */ |
|
| 66 | - protected $options = []; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @var array |
|
| 70 | - */ |
|
| 71 | - protected $driverOptions = []; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - protected $protocol = 'mongodb'; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @return string |
|
| 80 | - */ |
|
| 81 | - public function getHost(): string |
|
| 82 | - { |
|
| 83 | - return $this->host; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @param string $host |
|
| 88 | - * @return self |
|
| 89 | - */ |
|
| 90 | - public function setHost(string $host): self |
|
| 91 | - { |
|
| 92 | - $this->host = $host; |
|
| 93 | - return $this; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @return int |
|
| 98 | - */ |
|
| 99 | - public function getPort(): int |
|
| 100 | - { |
|
| 101 | - return $this->port; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @param int $port |
|
| 106 | - * @return self |
|
| 107 | - */ |
|
| 108 | - public function setPort(int $port): self |
|
| 109 | - { |
|
| 110 | - $this->port = $port; |
|
| 111 | - return $this; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @return int |
|
| 116 | - */ |
|
| 117 | - public function getTimeout(): int |
|
| 118 | - { |
|
| 119 | - return $this->timeout; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @param int $timeout |
|
| 124 | - * @return self |
|
| 125 | - */ |
|
| 126 | - public function setTimeout(int $timeout): self |
|
| 127 | - { |
|
| 128 | - $this->timeout = $timeout; |
|
| 129 | - return $this; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @return string |
|
| 134 | - */ |
|
| 135 | - public function getUsername(): string |
|
| 136 | - { |
|
| 137 | - return $this->username; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param string $username |
|
| 142 | - * @return self |
|
| 143 | - */ |
|
| 144 | - public function setUsername(string $username): self |
|
| 145 | - { |
|
| 146 | - $this->username = $username; |
|
| 147 | - return $this; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @return string |
|
| 152 | - */ |
|
| 153 | - public function getPassword(): string |
|
| 154 | - { |
|
| 155 | - return $this->password; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @param string $password |
|
| 160 | - * @return self |
|
| 161 | - */ |
|
| 162 | - public function setPassword(string $password): self |
|
| 163 | - { |
|
| 164 | - $this->password = $password; |
|
| 165 | - return $this; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * @return array |
|
| 170 | - */ |
|
| 171 | - public function getServers(): array |
|
| 172 | - { |
|
| 173 | - return $this->servers; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @param array $servers |
|
| 178 | - * @return self |
|
| 179 | - */ |
|
| 180 | - public function setServers(array $servers): self |
|
| 181 | - { |
|
| 182 | - $this->servers = $servers; |
|
| 183 | - return $this; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * @return string |
|
| 188 | - */ |
|
| 189 | - public function getCollectionName(): string |
|
| 190 | - { |
|
| 191 | - return $this->collectionName; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @param string $collectionName |
|
| 196 | - * @return self |
|
| 197 | - */ |
|
| 198 | - public function setCollectionName(string $collectionName): self |
|
| 199 | - { |
|
| 200 | - $this->collectionName = $collectionName; |
|
| 201 | - return $this; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * @return string |
|
| 206 | - */ |
|
| 207 | - public function getDatabaseName(): string |
|
| 208 | - { |
|
| 209 | - return $this->databaseName; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * @param string $databaseName |
|
| 214 | - * @return self |
|
| 215 | - */ |
|
| 216 | - public function setDatabaseName(string $databaseName): self |
|
| 217 | - { |
|
| 218 | - $this->databaseName = $databaseName; |
|
| 219 | - return $this; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * @return array |
|
| 224 | - */ |
|
| 225 | - public function getOptions(): array |
|
| 226 | - { |
|
| 227 | - return $this->options; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * @see https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options |
|
| 232 | - * @param array $options |
|
| 233 | - * @return Config |
|
| 234 | - */ |
|
| 235 | - public function setOptions(array $options): self |
|
| 236 | - { |
|
| 237 | - $this->options = $options; |
|
| 238 | - return $this; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * @return array |
|
| 243 | - */ |
|
| 244 | - public function getDriverOptions(): array |
|
| 245 | - { |
|
| 246 | - return $this->driverOptions; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * @param array $driverOptions |
|
| 251 | - * @return self |
|
| 252 | - */ |
|
| 253 | - public function setDriverOptions(array $driverOptions): self |
|
| 254 | - { |
|
| 255 | - $this->driverOptions = $driverOptions; |
|
| 256 | - return $this; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * @return string |
|
| 261 | - */ |
|
| 262 | - public function getProtocol(): string |
|
| 263 | - { |
|
| 264 | - return $this->protocol; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * @param string $protocol |
|
| 269 | - * @return self |
|
| 270 | - */ |
|
| 271 | - public function setProtocol(string $protocol): self |
|
| 272 | - { |
|
| 273 | - $this->protocol = $protocol; |
|
| 274 | - return $this; |
|
| 275 | - } |
|
| 23 | + /** |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + protected $host = '127.0.0.1'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var int |
|
| 30 | + */ |
|
| 31 | + protected $port = 27017; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var int |
|
| 35 | + */ |
|
| 36 | + protected $timeout = 3; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | + protected $username = ''; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var string |
|
| 45 | + */ |
|
| 46 | + protected $password = ''; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @var array |
|
| 50 | + */ |
|
| 51 | + protected $servers = []; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + protected $collectionName = 'phpfastcache'; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @var string |
|
| 60 | + */ |
|
| 61 | + protected $databaseName = Driver::MONGODB_DEFAULT_DB_NAME; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @var array |
|
| 65 | + */ |
|
| 66 | + protected $options = []; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @var array |
|
| 70 | + */ |
|
| 71 | + protected $driverOptions = []; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + protected $protocol = 'mongodb'; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @return string |
|
| 80 | + */ |
|
| 81 | + public function getHost(): string |
|
| 82 | + { |
|
| 83 | + return $this->host; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @param string $host |
|
| 88 | + * @return self |
|
| 89 | + */ |
|
| 90 | + public function setHost(string $host): self |
|
| 91 | + { |
|
| 92 | + $this->host = $host; |
|
| 93 | + return $this; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @return int |
|
| 98 | + */ |
|
| 99 | + public function getPort(): int |
|
| 100 | + { |
|
| 101 | + return $this->port; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @param int $port |
|
| 106 | + * @return self |
|
| 107 | + */ |
|
| 108 | + public function setPort(int $port): self |
|
| 109 | + { |
|
| 110 | + $this->port = $port; |
|
| 111 | + return $this; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @return int |
|
| 116 | + */ |
|
| 117 | + public function getTimeout(): int |
|
| 118 | + { |
|
| 119 | + return $this->timeout; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @param int $timeout |
|
| 124 | + * @return self |
|
| 125 | + */ |
|
| 126 | + public function setTimeout(int $timeout): self |
|
| 127 | + { |
|
| 128 | + $this->timeout = $timeout; |
|
| 129 | + return $this; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @return string |
|
| 134 | + */ |
|
| 135 | + public function getUsername(): string |
|
| 136 | + { |
|
| 137 | + return $this->username; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param string $username |
|
| 142 | + * @return self |
|
| 143 | + */ |
|
| 144 | + public function setUsername(string $username): self |
|
| 145 | + { |
|
| 146 | + $this->username = $username; |
|
| 147 | + return $this; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @return string |
|
| 152 | + */ |
|
| 153 | + public function getPassword(): string |
|
| 154 | + { |
|
| 155 | + return $this->password; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @param string $password |
|
| 160 | + * @return self |
|
| 161 | + */ |
|
| 162 | + public function setPassword(string $password): self |
|
| 163 | + { |
|
| 164 | + $this->password = $password; |
|
| 165 | + return $this; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * @return array |
|
| 170 | + */ |
|
| 171 | + public function getServers(): array |
|
| 172 | + { |
|
| 173 | + return $this->servers; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @param array $servers |
|
| 178 | + * @return self |
|
| 179 | + */ |
|
| 180 | + public function setServers(array $servers): self |
|
| 181 | + { |
|
| 182 | + $this->servers = $servers; |
|
| 183 | + return $this; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * @return string |
|
| 188 | + */ |
|
| 189 | + public function getCollectionName(): string |
|
| 190 | + { |
|
| 191 | + return $this->collectionName; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @param string $collectionName |
|
| 196 | + * @return self |
|
| 197 | + */ |
|
| 198 | + public function setCollectionName(string $collectionName): self |
|
| 199 | + { |
|
| 200 | + $this->collectionName = $collectionName; |
|
| 201 | + return $this; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * @return string |
|
| 206 | + */ |
|
| 207 | + public function getDatabaseName(): string |
|
| 208 | + { |
|
| 209 | + return $this->databaseName; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * @param string $databaseName |
|
| 214 | + * @return self |
|
| 215 | + */ |
|
| 216 | + public function setDatabaseName(string $databaseName): self |
|
| 217 | + { |
|
| 218 | + $this->databaseName = $databaseName; |
|
| 219 | + return $this; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * @return array |
|
| 224 | + */ |
|
| 225 | + public function getOptions(): array |
|
| 226 | + { |
|
| 227 | + return $this->options; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * @see https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options |
|
| 232 | + * @param array $options |
|
| 233 | + * @return Config |
|
| 234 | + */ |
|
| 235 | + public function setOptions(array $options): self |
|
| 236 | + { |
|
| 237 | + $this->options = $options; |
|
| 238 | + return $this; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * @return array |
|
| 243 | + */ |
|
| 244 | + public function getDriverOptions(): array |
|
| 245 | + { |
|
| 246 | + return $this->driverOptions; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * @param array $driverOptions |
|
| 251 | + * @return self |
|
| 252 | + */ |
|
| 253 | + public function setDriverOptions(array $driverOptions): self |
|
| 254 | + { |
|
| 255 | + $this->driverOptions = $driverOptions; |
|
| 256 | + return $this; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * @return string |
|
| 261 | + */ |
|
| 262 | + public function getProtocol(): string |
|
| 263 | + { |
|
| 264 | + return $this->protocol; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * @param string $protocol |
|
| 269 | + * @return self |
|
| 270 | + */ |
|
| 271 | + public function setProtocol(string $protocol): self |
|
| 272 | + { |
|
| 273 | + $this->protocol = $protocol; |
|
| 274 | + return $this; |
|
| 275 | + } |
|
| 276 | 276 | } |
@@ -27,34 +27,34 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class Item implements ExtendedCacheItemInterface |
| 29 | 29 | { |
| 30 | - use ItemBaseTrait { |
|
| 31 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Item constructor. |
|
| 36 | - * @param Driver $driver |
|
| 37 | - * @param $key |
|
| 38 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | - */ |
|
| 40 | - public function __construct(MongodbDriver $driver, $key) |
|
| 41 | - { |
|
| 42 | - $this->__BaseConstruct($driver, $key); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | - * @return static |
|
| 48 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | - { |
|
| 52 | - if ($driver instanceof MongodbDriver) { |
|
| 53 | - $this->driver = $driver; |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | - } |
|
| 30 | + use ItemBaseTrait { |
|
| 31 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Item constructor. |
|
| 36 | + * @param Driver $driver |
|
| 37 | + * @param $key |
|
| 38 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | + */ |
|
| 40 | + public function __construct(MongodbDriver $driver, $key) |
|
| 41 | + { |
|
| 42 | + $this->__BaseConstruct($driver, $key); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | + * @return static |
|
| 48 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | + { |
|
| 52 | + if ($driver instanceof MongodbDriver) { |
|
| 53 | + $this->driver = $driver; |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |
@@ -36,331 +36,331 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
| 38 | 38 | { |
| 39 | - public const MONGODB_DEFAULT_DB_NAME = 'phpfastcache'; // Public because used in config |
|
| 40 | - |
|
| 41 | - use DriverBaseTrait; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var Collection |
|
| 45 | - */ |
|
| 46 | - public $collection; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @var Database |
|
| 50 | - */ |
|
| 51 | - public $database; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @return bool |
|
| 55 | - */ |
|
| 56 | - public function driverCheck(): bool |
|
| 57 | - { |
|
| 58 | - $mongoExtensionExists = class_exists(Manager::class); |
|
| 59 | - |
|
| 60 | - if (!$mongoExtensionExists && class_exists(MongoClient::class)) { |
|
| 61 | - trigger_error( |
|
| 62 | - 'This driver is used to support the pecl MongoDb extension with mongo-php-library. |
|
| 39 | + public const MONGODB_DEFAULT_DB_NAME = 'phpfastcache'; // Public because used in config |
|
| 40 | + |
|
| 41 | + use DriverBaseTrait; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var Collection |
|
| 45 | + */ |
|
| 46 | + public $collection; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @var Database |
|
| 50 | + */ |
|
| 51 | + public $database; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @return bool |
|
| 55 | + */ |
|
| 56 | + public function driverCheck(): bool |
|
| 57 | + { |
|
| 58 | + $mongoExtensionExists = class_exists(Manager::class); |
|
| 59 | + |
|
| 60 | + if (!$mongoExtensionExists && class_exists(MongoClient::class)) { |
|
| 61 | + trigger_error( |
|
| 62 | + 'This driver is used to support the pecl MongoDb extension with mongo-php-library. |
|
| 63 | 63 | For MongoDb with Mongo PECL support use Mongo Driver.', |
| 64 | - E_USER_ERROR |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - return $mongoExtensionExists && class_exists(Collection::class); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @return DriverStatistic |
|
| 73 | - */ |
|
| 74 | - public function getStats(): DriverStatistic |
|
| 75 | - { |
|
| 76 | - $serverStats = $this->instance->getManager()->executeCommand( |
|
| 77 | - $this->getConfig()->getDatabaseName(), |
|
| 78 | - new Command( |
|
| 79 | - [ |
|
| 80 | - 'serverStatus' => 1, |
|
| 81 | - 'recordStats' => 0, |
|
| 82 | - 'repl' => 0, |
|
| 83 | - 'metrics' => 0, |
|
| 84 | - ] |
|
| 85 | - ) |
|
| 86 | - )->toArray()[0]; |
|
| 87 | - |
|
| 88 | - $collectionStats = $this->instance->getManager()->executeCommand( |
|
| 89 | - $this->getConfig()->getDatabaseName(), |
|
| 90 | - new Command( |
|
| 91 | - [ |
|
| 92 | - 'collStats' => $this->getConfig()->getCollectionName(), |
|
| 93 | - 'verbose' => true, |
|
| 94 | - ] |
|
| 95 | - ) |
|
| 96 | - )->toArray()[0]; |
|
| 97 | - |
|
| 98 | - $array_filter_recursive = static function ($array, callable $callback = null) use (&$array_filter_recursive) { |
|
| 99 | - $array = $callback($array); |
|
| 100 | - |
|
| 101 | - if (\is_object($array) || \is_array($array)) { |
|
| 102 | - foreach ($array as &$value) { |
|
| 103 | - $value = $array_filter_recursive($value, $callback); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return $array; |
|
| 108 | - }; |
|
| 109 | - |
|
| 110 | - $callback = static function ($item) { |
|
| 111 | - /** |
|
| 112 | - * Remove unserializable properties |
|
| 113 | - */ |
|
| 114 | - if ($item instanceof UTCDateTime) { |
|
| 115 | - return (string)$item; |
|
| 116 | - } |
|
| 117 | - return $item; |
|
| 118 | - }; |
|
| 119 | - |
|
| 120 | - $serverStats = $array_filter_recursive($serverStats, $callback); |
|
| 121 | - $collectionStats = $array_filter_recursive($collectionStats, $callback); |
|
| 122 | - |
|
| 123 | - $stats = (new DriverStatistic()) |
|
| 124 | - ->setInfo( |
|
| 125 | - 'MongoDB version ' . $serverStats->version . ', Uptime (in days): ' . round( |
|
| 126 | - $serverStats->uptime / 86400, |
|
| 127 | - 1 |
|
| 128 | - ) . "\n For more information see RawData." |
|
| 129 | - ) |
|
| 130 | - ->setSize($collectionStats->size) |
|
| 131 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 132 | - ->setRawData( |
|
| 133 | - [ |
|
| 134 | - 'serverStatus' => $serverStats, |
|
| 135 | - 'collStats' => $collectionStats, |
|
| 136 | - ] |
|
| 137 | - ); |
|
| 138 | - |
|
| 139 | - return $stats; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param CacheItemInterface $item |
|
| 144 | - * @return null|array |
|
| 145 | - */ |
|
| 146 | - protected function driverRead(CacheItemInterface $item) |
|
| 147 | - { |
|
| 148 | - $document = $this->getCollection()->findOne(['_id' => $this->getMongoDbItemKey($item)]); |
|
| 149 | - |
|
| 150 | - if ($document) { |
|
| 151 | - $return = [ |
|
| 152 | - self::DRIVER_DATA_WRAPPER_INDEX => $this->decode($document[self::DRIVER_DATA_WRAPPER_INDEX]->getData()), |
|
| 153 | - self::DRIVER_TAGS_WRAPPER_INDEX => $document[self::DRIVER_TAGS_WRAPPER_INDEX]->jsonSerialize(), |
|
| 154 | - self::DRIVER_EDATE_WRAPPER_INDEX => $document[self::DRIVER_EDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 155 | - ]; |
|
| 156 | - |
|
| 157 | - if (!empty($this->getConfig()->isItemDetailedDate())) { |
|
| 158 | - $return += [ |
|
| 159 | - self::DRIVER_MDATE_WRAPPER_INDEX => $document[self::DRIVER_MDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 160 | - self::DRIVER_CDATE_WRAPPER_INDEX => $document[self::DRIVER_CDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 161 | - ]; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - return $return; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return null; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @return Collection |
|
| 172 | - */ |
|
| 173 | - protected function getCollection(): Collection |
|
| 174 | - { |
|
| 175 | - return $this->collection; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @param CacheItemInterface $item |
|
| 180 | - * @return mixed |
|
| 181 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 182 | - * @throws PhpfastcacheDriverException |
|
| 183 | - */ |
|
| 184 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 185 | - { |
|
| 186 | - /** |
|
| 187 | - * Check for Cross-Driver type confusion |
|
| 188 | - */ |
|
| 189 | - if ($item instanceof Item) { |
|
| 190 | - try { |
|
| 191 | - $set = [ |
|
| 192 | - self::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), |
|
| 193 | - self::DRIVER_DATA_WRAPPER_INDEX => new Binary($this->encode($item->get()), Binary::TYPE_GENERIC), |
|
| 194 | - self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
| 195 | - self::DRIVER_EDATE_WRAPPER_INDEX => new UTCDateTime($item->getExpirationDate()), |
|
| 196 | - ]; |
|
| 197 | - |
|
| 198 | - if (!empty($this->getConfig()->isItemDetailedDate())) { |
|
| 199 | - $set += [ |
|
| 200 | - self::DRIVER_MDATE_WRAPPER_INDEX => new UTCDateTime($item->getModificationDate()), |
|
| 201 | - self::DRIVER_CDATE_WRAPPER_INDEX => new UTCDateTime($item->getCreationDate()), |
|
| 202 | - ]; |
|
| 203 | - } |
|
| 204 | - $result = (array)$this->getCollection()->updateOne( |
|
| 205 | - ['_id' => $this->getMongoDbItemKey($item)], |
|
| 206 | - [ |
|
| 207 | - '$set' => $set, |
|
| 208 | - ], |
|
| 209 | - ['upsert' => true, 'multiple' => false] |
|
| 210 | - ); |
|
| 211 | - } catch (MongoDBException $e) { |
|
| 212 | - throw new PhpfastcacheDriverException('Got an exception while trying to write data to MongoDB server: ' . $e->getMessage(), 0, $e); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return isset($result['ok']) ? $result['ok'] == 1 : true; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param CacheItemInterface $item |
|
| 223 | - * @return bool |
|
| 224 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 225 | - */ |
|
| 226 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 227 | - { |
|
| 228 | - /** |
|
| 229 | - * Check for Cross-Driver type confusion |
|
| 230 | - */ |
|
| 231 | - if ($item instanceof Item) { |
|
| 232 | - /** |
|
| 233 | - * @var DeleteResult $deletionResult |
|
| 234 | - */ |
|
| 235 | - $deletionResult = $this->getCollection()->deleteOne(['_id' => $this->getMongoDbItemKey($item)]); |
|
| 236 | - |
|
| 237 | - return $deletionResult->isAcknowledged(); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * @return bool |
|
| 245 | - */ |
|
| 246 | - protected function driverClear(): bool |
|
| 247 | - { |
|
| 248 | - try { |
|
| 249 | - return $this->collection->deleteMany([])->isAcknowledged(); |
|
| 250 | - } catch (MongoDBException $e) { |
|
| 251 | - throw new PhpfastcacheDriverException('Got error while trying to empty the collection: ' . $e->getMessage(), 0, $e); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * @return bool |
|
| 257 | - * @throws MongodbException |
|
| 258 | - * @throws LogicException |
|
| 259 | - */ |
|
| 260 | - protected function driverConnect(): bool |
|
| 261 | - { |
|
| 262 | - if ($this->instance instanceof Client) { |
|
| 263 | - throw new LogicException('Already connected to Mongodb server'); |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - $timeout = $this->getConfig()->getTimeout() * 1000; |
|
| 267 | - $collectionName = $this->getConfig()->getCollectionName(); |
|
| 268 | - $databaseName = $this->getConfig()->getDatabaseName(); |
|
| 269 | - $driverOptions = $this->getConfig()->getDriverOptions(); |
|
| 270 | - |
|
| 271 | - $this->instance = $this->instance ?: new Client($this->buildConnectionURI($databaseName), ['connectTimeoutMS' => $timeout], $driverOptions); |
|
| 272 | - $this->database = $this->database ?: $this->instance->selectDatabase($databaseName); |
|
| 273 | - |
|
| 274 | - if (!$this->collectionExists($collectionName)) { |
|
| 275 | - $this->database->createCollection($collectionName); |
|
| 276 | - $this->database->selectCollection($collectionName) |
|
| 277 | - ->createIndex( |
|
| 278 | - [self::DRIVER_KEY_WRAPPER_INDEX => 1], |
|
| 279 | - ['unique' => true, 'name' => 'unique_key_index'] |
|
| 280 | - ); |
|
| 281 | - $this->database->selectCollection($collectionName) |
|
| 282 | - ->createIndex( |
|
| 283 | - [self::DRIVER_EDATE_WRAPPER_INDEX => 1], |
|
| 284 | - ['expireAfterSeconds' => 0, 'name' => 'auto_expire_index'] |
|
| 285 | - ); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - $this->collection = $this->database->selectCollection($collectionName); |
|
| 289 | - |
|
| 290 | - return true; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Builds the connection URI from the given parameters. |
|
| 295 | - * |
|
| 296 | - * @param string $databaseName |
|
| 297 | - * @return string The connection URI. |
|
| 298 | - */ |
|
| 299 | - protected function buildConnectionURI(string $databaseName): string |
|
| 300 | - { |
|
| 301 | - $databaseName = \urlencode($databaseName); |
|
| 302 | - $servers = $this->getConfig()->getServers(); |
|
| 303 | - $options = $this->getConfig()->getOptions(); |
|
| 304 | - |
|
| 305 | - $protocol = $this->getConfig()->getProtocol(); |
|
| 306 | - $host = $this->getConfig()->getHost(); |
|
| 307 | - $port = $this->getConfig()->getPort(); |
|
| 308 | - $username = $this->getConfig()->getUsername(); |
|
| 309 | - $password = $this->getConfig()->getPassword(); |
|
| 310 | - |
|
| 311 | - if (count($servers) > 0) { |
|
| 312 | - $host = array_reduce( |
|
| 313 | - $servers, |
|
| 314 | - static function ($carry, $data) { |
|
| 315 | - $carry .= ($carry === '' ? '' : ',') . $data['host'] . ':' . $data['port']; |
|
| 316 | - return $carry; |
|
| 317 | - }, |
|
| 318 | - '' |
|
| 319 | - ); |
|
| 320 | - $port = false; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - return implode( |
|
| 324 | - '', |
|
| 325 | - [ |
|
| 326 | - "{$protocol}://", |
|
| 327 | - $username ?: '', |
|
| 328 | - $password ? ":{$password}" : '', |
|
| 329 | - $username ? '@' : '', |
|
| 330 | - $host, |
|
| 331 | - $port !== 27017 && $port !== false ? ":{$port}" : '', |
|
| 332 | - $databaseName ? "/{$databaseName}" : '', |
|
| 333 | - count($options) > 0 ? '?' . http_build_query($options) : '', |
|
| 334 | - ] |
|
| 335 | - ); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - protected function getMongoDbItemKey(CacheItemInterface $item) |
|
| 339 | - { |
|
| 340 | - return 'pfc_' . $item->getEncodedKey(); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /******************** |
|
| 64 | + E_USER_ERROR |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + return $mongoExtensionExists && class_exists(Collection::class); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @return DriverStatistic |
|
| 73 | + */ |
|
| 74 | + public function getStats(): DriverStatistic |
|
| 75 | + { |
|
| 76 | + $serverStats = $this->instance->getManager()->executeCommand( |
|
| 77 | + $this->getConfig()->getDatabaseName(), |
|
| 78 | + new Command( |
|
| 79 | + [ |
|
| 80 | + 'serverStatus' => 1, |
|
| 81 | + 'recordStats' => 0, |
|
| 82 | + 'repl' => 0, |
|
| 83 | + 'metrics' => 0, |
|
| 84 | + ] |
|
| 85 | + ) |
|
| 86 | + )->toArray()[0]; |
|
| 87 | + |
|
| 88 | + $collectionStats = $this->instance->getManager()->executeCommand( |
|
| 89 | + $this->getConfig()->getDatabaseName(), |
|
| 90 | + new Command( |
|
| 91 | + [ |
|
| 92 | + 'collStats' => $this->getConfig()->getCollectionName(), |
|
| 93 | + 'verbose' => true, |
|
| 94 | + ] |
|
| 95 | + ) |
|
| 96 | + )->toArray()[0]; |
|
| 97 | + |
|
| 98 | + $array_filter_recursive = static function ($array, callable $callback = null) use (&$array_filter_recursive) { |
|
| 99 | + $array = $callback($array); |
|
| 100 | + |
|
| 101 | + if (\is_object($array) || \is_array($array)) { |
|
| 102 | + foreach ($array as &$value) { |
|
| 103 | + $value = $array_filter_recursive($value, $callback); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return $array; |
|
| 108 | + }; |
|
| 109 | + |
|
| 110 | + $callback = static function ($item) { |
|
| 111 | + /** |
|
| 112 | + * Remove unserializable properties |
|
| 113 | + */ |
|
| 114 | + if ($item instanceof UTCDateTime) { |
|
| 115 | + return (string)$item; |
|
| 116 | + } |
|
| 117 | + return $item; |
|
| 118 | + }; |
|
| 119 | + |
|
| 120 | + $serverStats = $array_filter_recursive($serverStats, $callback); |
|
| 121 | + $collectionStats = $array_filter_recursive($collectionStats, $callback); |
|
| 122 | + |
|
| 123 | + $stats = (new DriverStatistic()) |
|
| 124 | + ->setInfo( |
|
| 125 | + 'MongoDB version ' . $serverStats->version . ', Uptime (in days): ' . round( |
|
| 126 | + $serverStats->uptime / 86400, |
|
| 127 | + 1 |
|
| 128 | + ) . "\n For more information see RawData." |
|
| 129 | + ) |
|
| 130 | + ->setSize($collectionStats->size) |
|
| 131 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 132 | + ->setRawData( |
|
| 133 | + [ |
|
| 134 | + 'serverStatus' => $serverStats, |
|
| 135 | + 'collStats' => $collectionStats, |
|
| 136 | + ] |
|
| 137 | + ); |
|
| 138 | + |
|
| 139 | + return $stats; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param CacheItemInterface $item |
|
| 144 | + * @return null|array |
|
| 145 | + */ |
|
| 146 | + protected function driverRead(CacheItemInterface $item) |
|
| 147 | + { |
|
| 148 | + $document = $this->getCollection()->findOne(['_id' => $this->getMongoDbItemKey($item)]); |
|
| 149 | + |
|
| 150 | + if ($document) { |
|
| 151 | + $return = [ |
|
| 152 | + self::DRIVER_DATA_WRAPPER_INDEX => $this->decode($document[self::DRIVER_DATA_WRAPPER_INDEX]->getData()), |
|
| 153 | + self::DRIVER_TAGS_WRAPPER_INDEX => $document[self::DRIVER_TAGS_WRAPPER_INDEX]->jsonSerialize(), |
|
| 154 | + self::DRIVER_EDATE_WRAPPER_INDEX => $document[self::DRIVER_EDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 155 | + ]; |
|
| 156 | + |
|
| 157 | + if (!empty($this->getConfig()->isItemDetailedDate())) { |
|
| 158 | + $return += [ |
|
| 159 | + self::DRIVER_MDATE_WRAPPER_INDEX => $document[self::DRIVER_MDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 160 | + self::DRIVER_CDATE_WRAPPER_INDEX => $document[self::DRIVER_CDATE_WRAPPER_INDEX]->toDateTime(), |
|
| 161 | + ]; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + return $return; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return null; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @return Collection |
|
| 172 | + */ |
|
| 173 | + protected function getCollection(): Collection |
|
| 174 | + { |
|
| 175 | + return $this->collection; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @param CacheItemInterface $item |
|
| 180 | + * @return mixed |
|
| 181 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 182 | + * @throws PhpfastcacheDriverException |
|
| 183 | + */ |
|
| 184 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 185 | + { |
|
| 186 | + /** |
|
| 187 | + * Check for Cross-Driver type confusion |
|
| 188 | + */ |
|
| 189 | + if ($item instanceof Item) { |
|
| 190 | + try { |
|
| 191 | + $set = [ |
|
| 192 | + self::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), |
|
| 193 | + self::DRIVER_DATA_WRAPPER_INDEX => new Binary($this->encode($item->get()), Binary::TYPE_GENERIC), |
|
| 194 | + self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
| 195 | + self::DRIVER_EDATE_WRAPPER_INDEX => new UTCDateTime($item->getExpirationDate()), |
|
| 196 | + ]; |
|
| 197 | + |
|
| 198 | + if (!empty($this->getConfig()->isItemDetailedDate())) { |
|
| 199 | + $set += [ |
|
| 200 | + self::DRIVER_MDATE_WRAPPER_INDEX => new UTCDateTime($item->getModificationDate()), |
|
| 201 | + self::DRIVER_CDATE_WRAPPER_INDEX => new UTCDateTime($item->getCreationDate()), |
|
| 202 | + ]; |
|
| 203 | + } |
|
| 204 | + $result = (array)$this->getCollection()->updateOne( |
|
| 205 | + ['_id' => $this->getMongoDbItemKey($item)], |
|
| 206 | + [ |
|
| 207 | + '$set' => $set, |
|
| 208 | + ], |
|
| 209 | + ['upsert' => true, 'multiple' => false] |
|
| 210 | + ); |
|
| 211 | + } catch (MongoDBException $e) { |
|
| 212 | + throw new PhpfastcacheDriverException('Got an exception while trying to write data to MongoDB server: ' . $e->getMessage(), 0, $e); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return isset($result['ok']) ? $result['ok'] == 1 : true; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param CacheItemInterface $item |
|
| 223 | + * @return bool |
|
| 224 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 225 | + */ |
|
| 226 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 227 | + { |
|
| 228 | + /** |
|
| 229 | + * Check for Cross-Driver type confusion |
|
| 230 | + */ |
|
| 231 | + if ($item instanceof Item) { |
|
| 232 | + /** |
|
| 233 | + * @var DeleteResult $deletionResult |
|
| 234 | + */ |
|
| 235 | + $deletionResult = $this->getCollection()->deleteOne(['_id' => $this->getMongoDbItemKey($item)]); |
|
| 236 | + |
|
| 237 | + return $deletionResult->isAcknowledged(); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * @return bool |
|
| 245 | + */ |
|
| 246 | + protected function driverClear(): bool |
|
| 247 | + { |
|
| 248 | + try { |
|
| 249 | + return $this->collection->deleteMany([])->isAcknowledged(); |
|
| 250 | + } catch (MongoDBException $e) { |
|
| 251 | + throw new PhpfastcacheDriverException('Got error while trying to empty the collection: ' . $e->getMessage(), 0, $e); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * @return bool |
|
| 257 | + * @throws MongodbException |
|
| 258 | + * @throws LogicException |
|
| 259 | + */ |
|
| 260 | + protected function driverConnect(): bool |
|
| 261 | + { |
|
| 262 | + if ($this->instance instanceof Client) { |
|
| 263 | + throw new LogicException('Already connected to Mongodb server'); |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + $timeout = $this->getConfig()->getTimeout() * 1000; |
|
| 267 | + $collectionName = $this->getConfig()->getCollectionName(); |
|
| 268 | + $databaseName = $this->getConfig()->getDatabaseName(); |
|
| 269 | + $driverOptions = $this->getConfig()->getDriverOptions(); |
|
| 270 | + |
|
| 271 | + $this->instance = $this->instance ?: new Client($this->buildConnectionURI($databaseName), ['connectTimeoutMS' => $timeout], $driverOptions); |
|
| 272 | + $this->database = $this->database ?: $this->instance->selectDatabase($databaseName); |
|
| 273 | + |
|
| 274 | + if (!$this->collectionExists($collectionName)) { |
|
| 275 | + $this->database->createCollection($collectionName); |
|
| 276 | + $this->database->selectCollection($collectionName) |
|
| 277 | + ->createIndex( |
|
| 278 | + [self::DRIVER_KEY_WRAPPER_INDEX => 1], |
|
| 279 | + ['unique' => true, 'name' => 'unique_key_index'] |
|
| 280 | + ); |
|
| 281 | + $this->database->selectCollection($collectionName) |
|
| 282 | + ->createIndex( |
|
| 283 | + [self::DRIVER_EDATE_WRAPPER_INDEX => 1], |
|
| 284 | + ['expireAfterSeconds' => 0, 'name' => 'auto_expire_index'] |
|
| 285 | + ); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + $this->collection = $this->database->selectCollection($collectionName); |
|
| 289 | + |
|
| 290 | + return true; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Builds the connection URI from the given parameters. |
|
| 295 | + * |
|
| 296 | + * @param string $databaseName |
|
| 297 | + * @return string The connection URI. |
|
| 298 | + */ |
|
| 299 | + protected function buildConnectionURI(string $databaseName): string |
|
| 300 | + { |
|
| 301 | + $databaseName = \urlencode($databaseName); |
|
| 302 | + $servers = $this->getConfig()->getServers(); |
|
| 303 | + $options = $this->getConfig()->getOptions(); |
|
| 304 | + |
|
| 305 | + $protocol = $this->getConfig()->getProtocol(); |
|
| 306 | + $host = $this->getConfig()->getHost(); |
|
| 307 | + $port = $this->getConfig()->getPort(); |
|
| 308 | + $username = $this->getConfig()->getUsername(); |
|
| 309 | + $password = $this->getConfig()->getPassword(); |
|
| 310 | + |
|
| 311 | + if (count($servers) > 0) { |
|
| 312 | + $host = array_reduce( |
|
| 313 | + $servers, |
|
| 314 | + static function ($carry, $data) { |
|
| 315 | + $carry .= ($carry === '' ? '' : ',') . $data['host'] . ':' . $data['port']; |
|
| 316 | + return $carry; |
|
| 317 | + }, |
|
| 318 | + '' |
|
| 319 | + ); |
|
| 320 | + $port = false; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + return implode( |
|
| 324 | + '', |
|
| 325 | + [ |
|
| 326 | + "{$protocol}://", |
|
| 327 | + $username ?: '', |
|
| 328 | + $password ? ":{$password}" : '', |
|
| 329 | + $username ? '@' : '', |
|
| 330 | + $host, |
|
| 331 | + $port !== 27017 && $port !== false ? ":{$port}" : '', |
|
| 332 | + $databaseName ? "/{$databaseName}" : '', |
|
| 333 | + count($options) > 0 ? '?' . http_build_query($options) : '', |
|
| 334 | + ] |
|
| 335 | + ); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + protected function getMongoDbItemKey(CacheItemInterface $item) |
|
| 339 | + { |
|
| 340 | + return 'pfc_' . $item->getEncodedKey(); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /******************** |
|
| 344 | 344 | * |
| 345 | 345 | * PSR-6 Extended Methods |
| 346 | 346 | * |
| 347 | 347 | *******************/ |
| 348 | 348 | |
| 349 | - /** |
|
| 350 | - * Checks if a collection name exists on the Mongo database. |
|
| 351 | - * |
|
| 352 | - * @param string $collectionName The collection name to check. |
|
| 353 | - * |
|
| 354 | - * @return bool True if the collection exists, false if not. |
|
| 355 | - */ |
|
| 356 | - protected function collectionExists($collectionName): bool |
|
| 357 | - { |
|
| 358 | - foreach ($this->database->listCollections() as $collection) { |
|
| 359 | - if ($collection->getName() === $collectionName) { |
|
| 360 | - return true; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return false; |
|
| 365 | - } |
|
| 349 | + /** |
|
| 350 | + * Checks if a collection name exists on the Mongo database. |
|
| 351 | + * |
|
| 352 | + * @param string $collectionName The collection name to check. |
|
| 353 | + * |
|
| 354 | + * @return bool True if the collection exists, false if not. |
|
| 355 | + */ |
|
| 356 | + protected function collectionExists($collectionName): bool |
|
| 357 | + { |
|
| 358 | + foreach ($this->database->listCollections() as $collection) { |
|
| 359 | + if ($collection->getName() === $collectionName) { |
|
| 360 | + return true; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return false; |
|
| 365 | + } |
|
| 366 | 366 | } |
@@ -27,34 +27,34 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class Item implements ExtendedCacheItemInterface |
| 29 | 29 | { |
| 30 | - use ItemBaseTrait { |
|
| 31 | - ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Item constructor. |
|
| 36 | - * @param Driver $driver |
|
| 37 | - * @param $key |
|
| 38 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | - */ |
|
| 40 | - public function __construct(DevtrueDriver $driver, $key) |
|
| 41 | - { |
|
| 42 | - $this->__BaseConstruct($driver, $key); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | - * @return static |
|
| 48 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | - { |
|
| 52 | - if ($driver instanceof DevtrueDriver) { |
|
| 53 | - $this->driver = $driver; |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | - } |
|
| 30 | + use ItemBaseTrait { |
|
| 31 | + ItemBaseTrait::__construct as __BaseConstruct; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Item constructor. |
|
| 36 | + * @param Driver $driver |
|
| 37 | + * @param $key |
|
| 38 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 39 | + */ |
|
| 40 | + public function __construct(DevtrueDriver $driver, $key) |
|
| 41 | + { |
|
| 42 | + $this->__BaseConstruct($driver, $key); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 47 | + * @return static |
|
| 48 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 51 | + { |
|
| 52 | + if ($driver instanceof DevtrueDriver) { |
|
| 53 | + $this->driver = $driver; |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |
@@ -31,96 +31,96 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class Driver implements ExtendedCacheItemPoolInterface |
| 33 | 33 | { |
| 34 | - use DriverBaseTrait; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @return bool |
|
| 38 | - */ |
|
| 39 | - public function driverCheck(): bool |
|
| 40 | - { |
|
| 41 | - return true; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @return DriverStatistic |
|
| 46 | - */ |
|
| 47 | - public function getStats(): DriverStatistic |
|
| 48 | - { |
|
| 49 | - $stat = new DriverStatistic(); |
|
| 50 | - $stat->setInfo('[Devtrue] A void info string') |
|
| 51 | - ->setSize(0) |
|
| 52 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 53 | - ->setRawData(true); |
|
| 54 | - |
|
| 55 | - return $stat; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @param CacheItemInterface $item |
|
| 60 | - * @return mixed |
|
| 61 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 62 | - */ |
|
| 63 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 64 | - { |
|
| 65 | - /** |
|
| 66 | - * Check for Cross-Driver type confusion |
|
| 67 | - */ |
|
| 68 | - if ($item instanceof Item) { |
|
| 69 | - return false; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param CacheItemInterface $item |
|
| 77 | - * @return array |
|
| 78 | - */ |
|
| 79 | - protected function driverRead(CacheItemInterface $item): array |
|
| 80 | - { |
|
| 81 | - return [ |
|
| 82 | - self::DRIVER_DATA_WRAPPER_INDEX => true, |
|
| 83 | - self::DRIVER_TAGS_WRAPPER_INDEX => [], |
|
| 84 | - self::DRIVER_EDATE_WRAPPER_INDEX => new DateTime(), |
|
| 85 | - ]; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param CacheItemInterface $item |
|
| 90 | - * @return bool |
|
| 91 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 92 | - */ |
|
| 93 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 94 | - { |
|
| 95 | - /** |
|
| 96 | - * Check for Cross-Driver type confusion |
|
| 97 | - */ |
|
| 98 | - if ($item instanceof Item) { |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @return bool |
|
| 107 | - */ |
|
| 108 | - protected function driverClear(): bool |
|
| 109 | - { |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /******************** |
|
| 34 | + use DriverBaseTrait; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @return bool |
|
| 38 | + */ |
|
| 39 | + public function driverCheck(): bool |
|
| 40 | + { |
|
| 41 | + return true; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @return DriverStatistic |
|
| 46 | + */ |
|
| 47 | + public function getStats(): DriverStatistic |
|
| 48 | + { |
|
| 49 | + $stat = new DriverStatistic(); |
|
| 50 | + $stat->setInfo('[Devtrue] A void info string') |
|
| 51 | + ->setSize(0) |
|
| 52 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 53 | + ->setRawData(true); |
|
| 54 | + |
|
| 55 | + return $stat; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @param CacheItemInterface $item |
|
| 60 | + * @return mixed |
|
| 61 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 62 | + */ |
|
| 63 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 64 | + { |
|
| 65 | + /** |
|
| 66 | + * Check for Cross-Driver type confusion |
|
| 67 | + */ |
|
| 68 | + if ($item instanceof Item) { |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param CacheItemInterface $item |
|
| 77 | + * @return array |
|
| 78 | + */ |
|
| 79 | + protected function driverRead(CacheItemInterface $item): array |
|
| 80 | + { |
|
| 81 | + return [ |
|
| 82 | + self::DRIVER_DATA_WRAPPER_INDEX => true, |
|
| 83 | + self::DRIVER_TAGS_WRAPPER_INDEX => [], |
|
| 84 | + self::DRIVER_EDATE_WRAPPER_INDEX => new DateTime(), |
|
| 85 | + ]; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param CacheItemInterface $item |
|
| 90 | + * @return bool |
|
| 91 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 92 | + */ |
|
| 93 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 94 | + { |
|
| 95 | + /** |
|
| 96 | + * Check for Cross-Driver type confusion |
|
| 97 | + */ |
|
| 98 | + if ($item instanceof Item) { |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @return bool |
|
| 107 | + */ |
|
| 108 | + protected function driverClear(): bool |
|
| 109 | + { |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /******************** |
|
| 114 | 114 | * |
| 115 | 115 | * PSR-6 Extended Methods |
| 116 | 116 | * |
| 117 | 117 | *******************/ |
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * @return bool |
|
| 121 | - */ |
|
| 122 | - protected function driverConnect(): bool |
|
| 123 | - { |
|
| 124 | - return false; |
|
| 125 | - } |
|
| 119 | + /** |
|
| 120 | + * @return bool |
|
| 121 | + */ |
|
| 122 | + protected function driverConnect(): bool |
|
| 123 | + { |
|
| 124 | + return false; |
|
| 125 | + } |
|
| 126 | 126 | } |
| 127 | 127 | \ No newline at end of file |
@@ -20,48 +20,48 @@ |
||
| 20 | 20 | |
| 21 | 21 | class Config extends CoubaseV2Config |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - protected $bucketName = 'phpfastcache'; |
|
| 23 | + /** |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + protected $bucketName = 'phpfastcache'; |
|
| 27 | 27 | |
| 28 | - protected $scopeName = self::DEFAULT_VALUE; |
|
| 28 | + protected $scopeName = self::DEFAULT_VALUE; |
|
| 29 | 29 | |
| 30 | - protected $collectionName = self::DEFAULT_VALUE; |
|
| 30 | + protected $collectionName = self::DEFAULT_VALUE; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @return string |
|
| 34 | - */ |
|
| 35 | - public function getScopeName(): string |
|
| 36 | - { |
|
| 37 | - return $this->scopeName; |
|
| 38 | - } |
|
| 32 | + /** |
|
| 33 | + * @return string |
|
| 34 | + */ |
|
| 35 | + public function getScopeName(): string |
|
| 36 | + { |
|
| 37 | + return $this->scopeName; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param string $scopeName |
|
| 42 | - * @return Config |
|
| 43 | - */ |
|
| 44 | - public function setScopeName(string $scopeName): Config |
|
| 45 | - { |
|
| 46 | - $this->scopeName = $scopeName; |
|
| 47 | - return $this; |
|
| 48 | - } |
|
| 40 | + /** |
|
| 41 | + * @param string $scopeName |
|
| 42 | + * @return Config |
|
| 43 | + */ |
|
| 44 | + public function setScopeName(string $scopeName): Config |
|
| 45 | + { |
|
| 46 | + $this->scopeName = $scopeName; |
|
| 47 | + return $this; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @return string |
|
| 52 | - */ |
|
| 53 | - public function getCollectionName(): string |
|
| 54 | - { |
|
| 55 | - return $this->collectionName; |
|
| 56 | - } |
|
| 50 | + /** |
|
| 51 | + * @return string |
|
| 52 | + */ |
|
| 53 | + public function getCollectionName(): string |
|
| 54 | + { |
|
| 55 | + return $this->collectionName; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @param string $collectionName |
|
| 60 | - * @return Config |
|
| 61 | - */ |
|
| 62 | - public function setCollectionName(string $collectionName): Config |
|
| 63 | - { |
|
| 64 | - $this->collectionName = $collectionName; |
|
| 65 | - return $this; |
|
| 66 | - } |
|
| 58 | + /** |
|
| 59 | + * @param string $collectionName |
|
| 60 | + * @return Config |
|
| 61 | + */ |
|
| 62 | + public function setCollectionName(string $collectionName): Config |
|
| 63 | + { |
|
| 64 | + $this->collectionName = $collectionName; |
|
| 65 | + return $this; |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -27,30 +27,30 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class Item extends CoubaseV2Item |
| 29 | 29 | { |
| 30 | - /** |
|
| 31 | - * Item constructor. |
|
| 32 | - * @param Driver $driver |
|
| 33 | - * @param $key |
|
| 34 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 35 | - */ |
|
| 36 | - public function __construct(CouchbaseDriver $driver, $key) |
|
| 37 | - { |
|
| 38 | - parent::__construct($driver, $key); |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Item constructor. |
|
| 32 | + * @param Driver $driver |
|
| 33 | + * @param $key |
|
| 34 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 35 | + */ |
|
| 36 | + public function __construct(CouchbaseDriver $driver, $key) |
|
| 37 | + { |
|
| 38 | + parent::__construct($driver, $key); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @param ExtendedCacheItemPoolInterface $driver |
|
| 43 | - * @return static |
|
| 44 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 45 | - */ |
|
| 46 | - public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 47 | - { |
|
| 48 | - if ($driver instanceof CouchbaseDriver) { |
|
| 49 | - $this->driver = $driver; |
|
| 41 | + /** |
|
| 42 | + * @param ExtendedCacheItemPoolInterface $driver |
|
| 43 | + * @return static |
|
| 44 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 45 | + */ |
|
| 46 | + public function setDriver(ExtendedCacheItemPoolInterface $driver) |
|
| 47 | + { |
|
| 48 | + if ($driver instanceof CouchbaseDriver) { |
|
| 49 | + $this->driver = $driver; |
|
| 50 | 50 | |
| 51 | - return $this; |
|
| 52 | - } |
|
| 51 | + return $this; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 55 | - } |
|
| 54 | + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -33,170 +33,170 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class Driver extends CoubaseV2Driver |
| 35 | 35 | { |
| 36 | - /** |
|
| 37 | - * @var Scope |
|
| 38 | - */ |
|
| 39 | - protected $scope; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var Collection |
|
| 43 | - */ |
|
| 44 | - protected $collection; |
|
| 45 | - |
|
| 46 | - public function __construct(ConfigurationOption $config, $instanceId) |
|
| 47 | - { |
|
| 48 | - $this->__baseConstruct($config, $instanceId); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @return bool |
|
| 53 | - * @throws PhpfastcacheLogicException |
|
| 54 | - */ |
|
| 55 | - protected function driverConnect(): bool |
|
| 56 | - { |
|
| 57 | - if (!\class_exists(ClusterOptions::class)) { |
|
| 58 | - throw new PhpfastcacheDriverCheckException('You are using the Couchbase PHP SDK 2.x so please use driver Couchbasev3'); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $connectionString = "couchbase://{$this->getConfig()->getHost()}:{$this->getConfig()->getPort()}"; |
|
| 62 | - |
|
| 63 | - $options = new ClusterOptions(); |
|
| 64 | - $options->credentials($this->getConfig()->getUsername(), $this->getConfig()->getPassword()); |
|
| 65 | - $this->instance = new Cluster($connectionString, $options); |
|
| 66 | - |
|
| 67 | - $this->setBucket($this->instance->bucket($this->getConfig()->getBucketName())); |
|
| 68 | - $this->setScope($this->getBucket()->scope($this->getConfig()->getScopeName())); |
|
| 69 | - $this->setCollection($this->getScope()->collection($this->getConfig()->getCollectionName())); |
|
| 70 | - |
|
| 71 | - return true; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param CacheItemInterface $item |
|
| 76 | - * @return null|array |
|
| 77 | - */ |
|
| 78 | - protected function driverRead(CacheItemInterface $item) |
|
| 79 | - { |
|
| 80 | - try { |
|
| 81 | - /** |
|
| 82 | - * CouchbaseBucket::get() returns a GetResult interface |
|
| 83 | - */ |
|
| 84 | - return $this->decodeDocument((array)$this->getCollection()->get($item->getEncodedKey())->content()); |
|
| 85 | - } catch (DocumentNotFoundException $e) { |
|
| 86 | - return null; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param CacheItemInterface $item |
|
| 92 | - * @return bool |
|
| 93 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 94 | - */ |
|
| 95 | - protected function driverWrite(CacheItemInterface $item): bool |
|
| 96 | - { |
|
| 97 | - /** |
|
| 98 | - * Check for Cross-Driver type confusion |
|
| 99 | - */ |
|
| 100 | - if ($item instanceof Item) { |
|
| 101 | - try { |
|
| 102 | - $this->getCollection()->upsert( |
|
| 103 | - $item->getEncodedKey(), |
|
| 104 | - $this->encodeDocument($this->driverPreWrap($item)), |
|
| 105 | - (new UpsertOptions())->expiry($item->getTtl()) |
|
| 106 | - ); |
|
| 107 | - return true; |
|
| 108 | - } catch (CouchbaseException $e) { |
|
| 109 | - return false; |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param CacheItemInterface $item |
|
| 118 | - * @return bool |
|
| 119 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 120 | - */ |
|
| 121 | - protected function driverDelete(CacheItemInterface $item): bool |
|
| 122 | - { |
|
| 123 | - /** |
|
| 124 | - * Check for Cross-Driver type confusion |
|
| 125 | - */ |
|
| 126 | - if ($item instanceof Item) { |
|
| 127 | - try { |
|
| 128 | - $this->getCollection()->remove($item->getEncodedKey()); |
|
| 129 | - return true; |
|
| 130 | - } catch (DocumentNotFoundException $e) { |
|
| 131 | - return true; |
|
| 132 | - } catch (CouchbaseException $e) { |
|
| 133 | - return false; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @return bool |
|
| 142 | - */ |
|
| 143 | - protected function driverClear(): bool |
|
| 144 | - { |
|
| 145 | - $this->instance->buckets()->flush($this->getConfig()->getBucketName()); |
|
| 146 | - return true; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @return DriverStatistic |
|
| 151 | - */ |
|
| 152 | - public function getStats(): DriverStatistic |
|
| 153 | - { |
|
| 154 | - /** |
|
| 155 | - * Between SDK 2 and 3 we lost a lot of useful information :( |
|
| 156 | - * @see https://docs.couchbase.com/java-sdk/current/project-docs/migrating-sdk-code-to-3.n.html#management-apis |
|
| 157 | - */ |
|
| 158 | - $info = $this->getBucket()->diagnostics(\bin2hex(\random_bytes(16))); |
|
| 159 | - |
|
| 160 | - return (new DriverStatistic()) |
|
| 161 | - ->setSize(0) |
|
| 162 | - ->setRawData($info) |
|
| 163 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 164 | - ->setInfo( $info['sdk'] . "\n For more information see RawData."); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @return Collection |
|
| 169 | - */ |
|
| 170 | - public function getCollection(): Collection |
|
| 171 | - { |
|
| 172 | - return $this->collection; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @param Collection $collection |
|
| 177 | - * @return Driver |
|
| 178 | - */ |
|
| 179 | - public function setCollection(Collection $collection): Driver |
|
| 180 | - { |
|
| 181 | - $this->collection = $collection; |
|
| 182 | - return $this; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @return Scope |
|
| 187 | - */ |
|
| 188 | - public function getScope(): Scope |
|
| 189 | - { |
|
| 190 | - return $this->scope; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param Scope $scope |
|
| 195 | - * @return Driver |
|
| 196 | - */ |
|
| 197 | - public function setScope(Scope $scope): Driver |
|
| 198 | - { |
|
| 199 | - $this->scope = $scope; |
|
| 200 | - return $this; |
|
| 201 | - } |
|
| 36 | + /** |
|
| 37 | + * @var Scope |
|
| 38 | + */ |
|
| 39 | + protected $scope; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var Collection |
|
| 43 | + */ |
|
| 44 | + protected $collection; |
|
| 45 | + |
|
| 46 | + public function __construct(ConfigurationOption $config, $instanceId) |
|
| 47 | + { |
|
| 48 | + $this->__baseConstruct($config, $instanceId); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @return bool |
|
| 53 | + * @throws PhpfastcacheLogicException |
|
| 54 | + */ |
|
| 55 | + protected function driverConnect(): bool |
|
| 56 | + { |
|
| 57 | + if (!\class_exists(ClusterOptions::class)) { |
|
| 58 | + throw new PhpfastcacheDriverCheckException('You are using the Couchbase PHP SDK 2.x so please use driver Couchbasev3'); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $connectionString = "couchbase://{$this->getConfig()->getHost()}:{$this->getConfig()->getPort()}"; |
|
| 62 | + |
|
| 63 | + $options = new ClusterOptions(); |
|
| 64 | + $options->credentials($this->getConfig()->getUsername(), $this->getConfig()->getPassword()); |
|
| 65 | + $this->instance = new Cluster($connectionString, $options); |
|
| 66 | + |
|
| 67 | + $this->setBucket($this->instance->bucket($this->getConfig()->getBucketName())); |
|
| 68 | + $this->setScope($this->getBucket()->scope($this->getConfig()->getScopeName())); |
|
| 69 | + $this->setCollection($this->getScope()->collection($this->getConfig()->getCollectionName())); |
|
| 70 | + |
|
| 71 | + return true; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param CacheItemInterface $item |
|
| 76 | + * @return null|array |
|
| 77 | + */ |
|
| 78 | + protected function driverRead(CacheItemInterface $item) |
|
| 79 | + { |
|
| 80 | + try { |
|
| 81 | + /** |
|
| 82 | + * CouchbaseBucket::get() returns a GetResult interface |
|
| 83 | + */ |
|
| 84 | + return $this->decodeDocument((array)$this->getCollection()->get($item->getEncodedKey())->content()); |
|
| 85 | + } catch (DocumentNotFoundException $e) { |
|
| 86 | + return null; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param CacheItemInterface $item |
|
| 92 | + * @return bool |
|
| 93 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 94 | + */ |
|
| 95 | + protected function driverWrite(CacheItemInterface $item): bool |
|
| 96 | + { |
|
| 97 | + /** |
|
| 98 | + * Check for Cross-Driver type confusion |
|
| 99 | + */ |
|
| 100 | + if ($item instanceof Item) { |
|
| 101 | + try { |
|
| 102 | + $this->getCollection()->upsert( |
|
| 103 | + $item->getEncodedKey(), |
|
| 104 | + $this->encodeDocument($this->driverPreWrap($item)), |
|
| 105 | + (new UpsertOptions())->expiry($item->getTtl()) |
|
| 106 | + ); |
|
| 107 | + return true; |
|
| 108 | + } catch (CouchbaseException $e) { |
|
| 109 | + return false; |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param CacheItemInterface $item |
|
| 118 | + * @return bool |
|
| 119 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 120 | + */ |
|
| 121 | + protected function driverDelete(CacheItemInterface $item): bool |
|
| 122 | + { |
|
| 123 | + /** |
|
| 124 | + * Check for Cross-Driver type confusion |
|
| 125 | + */ |
|
| 126 | + if ($item instanceof Item) { |
|
| 127 | + try { |
|
| 128 | + $this->getCollection()->remove($item->getEncodedKey()); |
|
| 129 | + return true; |
|
| 130 | + } catch (DocumentNotFoundException $e) { |
|
| 131 | + return true; |
|
| 132 | + } catch (CouchbaseException $e) { |
|
| 133 | + return false; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @return bool |
|
| 142 | + */ |
|
| 143 | + protected function driverClear(): bool |
|
| 144 | + { |
|
| 145 | + $this->instance->buckets()->flush($this->getConfig()->getBucketName()); |
|
| 146 | + return true; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @return DriverStatistic |
|
| 151 | + */ |
|
| 152 | + public function getStats(): DriverStatistic |
|
| 153 | + { |
|
| 154 | + /** |
|
| 155 | + * Between SDK 2 and 3 we lost a lot of useful information :( |
|
| 156 | + * @see https://docs.couchbase.com/java-sdk/current/project-docs/migrating-sdk-code-to-3.n.html#management-apis |
|
| 157 | + */ |
|
| 158 | + $info = $this->getBucket()->diagnostics(\bin2hex(\random_bytes(16))); |
|
| 159 | + |
|
| 160 | + return (new DriverStatistic()) |
|
| 161 | + ->setSize(0) |
|
| 162 | + ->setRawData($info) |
|
| 163 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 164 | + ->setInfo( $info['sdk'] . "\n For more information see RawData."); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @return Collection |
|
| 169 | + */ |
|
| 170 | + public function getCollection(): Collection |
|
| 171 | + { |
|
| 172 | + return $this->collection; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @param Collection $collection |
|
| 177 | + * @return Driver |
|
| 178 | + */ |
|
| 179 | + public function setCollection(Collection $collection): Driver |
|
| 180 | + { |
|
| 181 | + $this->collection = $collection; |
|
| 182 | + return $this; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @return Scope |
|
| 187 | + */ |
|
| 188 | + public function getScope(): Scope |
|
| 189 | + { |
|
| 190 | + return $this->scope; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param Scope $scope |
|
| 195 | + * @return Driver |
|
| 196 | + */ |
|
| 197 | + public function setScope(Scope $scope): Driver |
|
| 198 | + { |
|
| 199 | + $this->scope = $scope; |
|
| 200 | + return $this; |
|
| 201 | + } |
|
| 202 | 202 | } |
@@ -23,113 +23,113 @@ |
||
| 23 | 23 | |
| 24 | 24 | class Config extends ConfigurationOption |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * @var array |
|
| 28 | - * |
|
| 29 | - * Multiple server can be added this way: |
|
| 30 | - * $cfg->setServers([ |
|
| 31 | - * [ |
|
| 32 | - * // If you use an UNIX socket set the host and port to null |
|
| 33 | - * 'host' => '127.0.0.1', |
|
| 34 | - * //'path' => 'path/to/unix/socket', |
|
| 35 | - * 'port' => 11211, |
|
| 36 | - * ] |
|
| 37 | - * ]); |
|
| 38 | - */ |
|
| 39 | - protected $servers = []; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - protected $host = '127.0.0.1'; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var int |
|
| 48 | - */ |
|
| 49 | - protected $port = 11211; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @return array |
|
| 53 | - */ |
|
| 54 | - public function getServers(): array |
|
| 55 | - { |
|
| 56 | - return $this->servers; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param array $servers |
|
| 61 | - * @return self |
|
| 62 | - * @throws PhpfastcacheInvalidConfigurationException |
|
| 63 | - */ |
|
| 64 | - public function setServers(array $servers): self |
|
| 65 | - { |
|
| 66 | - foreach ($servers as $server) { |
|
| 67 | - if (\array_key_exists('saslUser', $server) || array_key_exists('saslPassword', $server)) { |
|
| 68 | - throw new PhpfastcacheInvalidConfigurationException('Unlike Memcached, Memcache does not support SASL authentication'); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - if ($diff = array_diff(array_keys($server), ['host', 'port', 'path'])) { |
|
| 72 | - throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff)); |
|
| 73 | - } |
|
| 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'); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port'])|| $server['port'] < 1)) { |
|
| 88 | - throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array'); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - if (!empty($server['port']) && !empty($server['path'])) { |
|
| 92 | - throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path'); |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - $this->servers = $servers; |
|
| 96 | - return $this; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @return string |
|
| 101 | - */ |
|
| 102 | - public function getHost(): string |
|
| 103 | - { |
|
| 104 | - return $this->host; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param string $host |
|
| 109 | - * @return self |
|
| 110 | - */ |
|
| 111 | - public function setHost(string $host): self |
|
| 112 | - { |
|
| 113 | - $this->host = $host; |
|
| 114 | - |
|
| 115 | - return $this; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @return int |
|
| 120 | - */ |
|
| 121 | - public function getPort(): int |
|
| 122 | - { |
|
| 123 | - return $this->port; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param int $port |
|
| 128 | - * @return self |
|
| 129 | - */ |
|
| 130 | - public function setPort(int $port): self |
|
| 131 | - { |
|
| 132 | - $this->port = $port; |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 26 | + /** |
|
| 27 | + * @var array |
|
| 28 | + * |
|
| 29 | + * Multiple server can be added this way: |
|
| 30 | + * $cfg->setServers([ |
|
| 31 | + * [ |
|
| 32 | + * // If you use an UNIX socket set the host and port to null |
|
| 33 | + * 'host' => '127.0.0.1', |
|
| 34 | + * //'path' => 'path/to/unix/socket', |
|
| 35 | + * 'port' => 11211, |
|
| 36 | + * ] |
|
| 37 | + * ]); |
|
| 38 | + */ |
|
| 39 | + protected $servers = []; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + protected $host = '127.0.0.1'; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var int |
|
| 48 | + */ |
|
| 49 | + protected $port = 11211; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @return array |
|
| 53 | + */ |
|
| 54 | + public function getServers(): array |
|
| 55 | + { |
|
| 56 | + return $this->servers; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param array $servers |
|
| 61 | + * @return self |
|
| 62 | + * @throws PhpfastcacheInvalidConfigurationException |
|
| 63 | + */ |
|
| 64 | + public function setServers(array $servers): self |
|
| 65 | + { |
|
| 66 | + foreach ($servers as $server) { |
|
| 67 | + if (\array_key_exists('saslUser', $server) || array_key_exists('saslPassword', $server)) { |
|
| 68 | + throw new PhpfastcacheInvalidConfigurationException('Unlike Memcached, Memcache does not support SASL authentication'); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + if ($diff = array_diff(array_keys($server), ['host', 'port', 'path'])) { |
|
| 72 | + throw new PhpfastcacheInvalidConfigurationException('Unknown keys for memcached server: ' . implode(', ', $diff)); |
|
| 73 | + } |
|
| 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'); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + if (!empty($server['host']) && (empty($server['port']) || !is_int($server['port'])|| $server['port'] < 1)) { |
|
| 88 | + throw new PhpfastcacheInvalidConfigurationException('Port must be a valid integer in "$server" configuration array'); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + if (!empty($server['port']) && !empty($server['path'])) { |
|
| 92 | + throw new PhpfastcacheInvalidConfigurationException('Port should not be defined along with path'); |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + $this->servers = $servers; |
|
| 96 | + return $this; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @return string |
|
| 101 | + */ |
|
| 102 | + public function getHost(): string |
|
| 103 | + { |
|
| 104 | + return $this->host; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param string $host |
|
| 109 | + * @return self |
|
| 110 | + */ |
|
| 111 | + public function setHost(string $host): self |
|
| 112 | + { |
|
| 113 | + $this->host = $host; |
|
| 114 | + |
|
| 115 | + return $this; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @return int |
|
| 120 | + */ |
|
| 121 | + public function getPort(): int |
|
| 122 | + { |
|
| 123 | + return $this->port; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param int $port |
|
| 128 | + * @return self |
|
| 129 | + */ |
|
| 130 | + public function setPort(int $port): self |
|
| 131 | + { |
|
| 132 | + $this->port = $port; |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | 135 | } |