composer package updates
[openemr.git] / vendor / zendframework / zend-cache / src / Storage / Adapter / ExtMongoDbResourceManager.php
blobf9f9b518b1c6cb697f944d177e08ad9966bc04f9
1 <?php
2 /**
3 * @see https://github.com/zendframework/zend-cache for the canonical source repository
4 * @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
6 */
8 namespace Zend\Cache\Storage\Adapter;
10 use MongoDB\Client;
11 use MongoDB\Collection;
12 use MongoDB\Driver\Exception\Exception as MongoDriverException;
13 use Zend\Cache\Exception;
15 /**
16 * Resource manager for the ext-mongodb adapter.
18 * If you are using ext-mongo, use the MongoDbResourceManager instead.
20 class ExtMongoDbResourceManager
22 /**
23 * Registered resources
25 * @var array[]
27 private $resources = [];
29 /**
30 * Check if a resource exists
32 * @param string $id
34 * @return bool
36 public function hasResource($id)
38 return isset($this->resources[$id]);
41 /**
42 * Set a resource
44 * @param string $id
45 * @param array|Collection $resource
46 * @return self Provides a fluent interface
47 * @throws Exception\RuntimeException
49 public function setResource($id, $resource)
51 if ($resource instanceof Collection) {
52 $this->resources[$id] = [
53 'db' => (string) $resource->db,
54 'db_instance' => $resource->db,
55 'collection' => (string) $resource,
56 'collection_instance' => $resource,
58 return $this;
61 if (! is_array($resource)) {
62 throw new Exception\InvalidArgumentException(sprintf(
63 '%s expects an array or %s; received %s',
64 __METHOD__,
65 Collection::class,
66 is_object($resource) ? get_class($resource) : gettype($resource)
67 ));
70 $this->resources[$id] = $resource;
71 return $this;
74 /**
75 * Instantiate and return the Collection resource
77 * @param string $id
78 * @return Collection
79 * @throws Exception\RuntimeException
81 public function getResource($id)
83 if (! $this->hasResource($id)) {
84 throw new Exception\RuntimeException("No resource with id '{$id}'");
87 $resource = $this->resources[$id];
88 if (! isset($resource['collection_instance'])) {
89 try {
90 if (! isset($resource['db_instance'])) {
91 if (! isset($resource['client_instance'])) {
92 $resource['client_instance'] = new Client(
93 isset($resource['server']) ? $resource['server'] : null,
94 isset($resource['connection_options']) ? $resource['connection_options'] : [],
95 isset($resource['driver_options']) ? $resource['driver_options'] : []
100 $collection = $resource['client_instance']->selectCollection(
101 isset($resouce['db']) ? $resource['db'] : 'zend',
102 isset($resource['collection']) ? $resource['collection'] : 'cache'
104 $collection->createIndex(['key' => 1]);
106 $this->resources[$id]['collection_instance'] = $collection;
107 } catch (MongoDriverException $e) {
108 throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
112 return $this->resources[$id]['collection_instance'];
116 * @param string $id
117 * @param string $server
118 * @return void
120 public function setServer($id, $server)
122 $this->resources[$id]['server'] = (string) $server;
124 unset($this->resources[$id]['client_instance']);
125 unset($this->resources[$id]['db_instance']);
126 unset($this->resources[$id]['collection_instance']);
130 * @param string $id
131 * @return null|string
132 * @throws Exception\RuntimeException if no matching resource discovered
134 public function getServer($id)
136 if (! $this->hasResource($id)) {
137 throw new Exception\RuntimeException("No resource with id '{$id}'");
140 return isset($this->resources[$id]['server']) ? $this->resources[$id]['server'] : null;
144 * @param string $id
145 * @param array $connectionOptions
146 * @return void
148 public function setConnectionOptions($id, array $connectionOptions)
150 $this->resources[$id]['connection_options'] = $connectionOptions;
152 unset($this->resources[$id]['client_instance']);
153 unset($this->resources[$id]['db_instance']);
154 unset($this->resources[$id]['collection_instance']);
158 * @param string $id
159 * @return array
160 * @throws Exception\RuntimeException if no matching resource discovered
162 public function getConnectionOptions($id)
164 if (! $this->hasResource($id)) {
165 throw new Exception\RuntimeException("No resource with id '{$id}'");
168 return isset($this->resources[$id]['connection_options'])
169 ? $this->resources[$id]['connection_options']
170 : [];
174 * @param string $id
175 * @param array $driverOptions
176 * @return void
178 public function setDriverOptions($id, array $driverOptions)
180 $this->resources[$id]['driver_options'] = $driverOptions;
182 unset($this->resources[$id]['client_instance']);
183 unset($this->resources[$id]['db_instance']);
184 unset($this->resources[$id]['collection_instance']);
188 * @param string $id
189 * @return array
190 * @throws Exception\RuntimeException if no matching resource discovered
192 public function getDriverOptions($id)
194 if (! $this->hasResource($id)) {
195 throw new Exception\RuntimeException("No resource with id '{$id}'");
198 return isset($this->resources[$id]['driver_options']) ? $this->resources[$id]['driver_options'] : [];
202 * @param string $id
203 * @param string $database
204 * @return void
206 public function setDatabase($id, $database)
208 $this->resources[$id]['db'] = (string) $database;
210 unset($this->resources[$id]['db_instance']);
211 unset($this->resources[$id]['collection_instance']);
215 * @param string $id
216 * @return string
217 * @throws Exception\RuntimeException if no matching resource discovered
219 public function getDatabase($id)
221 if (! $this->hasResource($id)) {
222 throw new Exception\RuntimeException("No resource with id '{$id}'");
225 return isset($this->resources[$id]['db']) ? $this->resources[$id]['db'] : '';
229 * @param string $id
230 * @param string $collection
231 * @return void
233 public function setCollection($id, $collection)
235 $this->resources[$id]['collection'] = (string) $collection;
237 unset($this->resources[$id]['collection_instance']);
241 * @param string $id
242 * @return string
243 * @throws Exception\RuntimeException if no matching resource discovered
245 public function getCollection($id)
247 if (! $this->hasResource($id)) {
248 throw new Exception\RuntimeException("No resource with id '{$id}'");
251 return isset($this->resources[$id]['collection']) ? $this->resources[$id]['collection'] : '';