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
8 namespace Zend\Cache\Storage\Adapter
;
11 use MongoDB\Collection
;
12 use MongoDB\Driver\Exception\Exception
as MongoDriverException
;
13 use Zend\Cache\Exception
;
16 * Resource manager for the ext-mongodb adapter.
18 * If you are using ext-mongo, use the MongoDbResourceManager instead.
20 class ExtMongoDbResourceManager
23 * Registered resources
27 private $resources = [];
30 * Check if a resource exists
36 public function hasResource($id)
38 return isset($this->resources
[$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,
61 if (! is_array($resource)) {
62 throw new Exception\
InvalidArgumentException(sprintf(
63 '%s expects an array or %s; received %s',
66 is_object($resource) ?
get_class($resource) : gettype($resource)
70 $this->resources
[$id] = $resource;
75 * Instantiate and return the Collection resource
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'])) {
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'];
117 * @param string $server
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']);
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;
145 * @param array $connectionOptions
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']);
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']
175 * @param array $driverOptions
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']);
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'] : [];
203 * @param string $database
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']);
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'] : '';
230 * @param string $collection
233 public function setCollection($id, $collection)
235 $this->resources
[$id]['collection'] = (string) $collection;
237 unset($this->resources
[$id]['collection_instance']);
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'] : '';