Skip to content

Commit f69821b

Browse files
authored
Merge pull request #21 from formapro/lazy-storages
Make storage lazy.
2 parents df6134f + a74d98f commit f69821b

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

src/LocatorRegistry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function getStorages(): array
4040
public function getStorage(string $name): Storage
4141
{
4242
if (false == $this->container->has($name)) {
43-
throw new \InvalidArgumentException(sprintf('The storage with name "%s" does not exist', $id));
43+
throw new \InvalidArgumentException(sprintf('The storage with name "%s" does not exist', $name));
4444
}
4545

4646
return $this->container->get($name);

src/Storage.php

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,33 @@
99

1010
class Storage
1111
{
12-
/**
13-
* @var Collection
14-
*/
15-
private $collection;
12+
private $collectionName;
13+
14+
private $collectionFactory;
1615

17-
/**
18-
* @var Hydrator
19-
*/
2016
private $hydrator;
2117

22-
/**
23-
* @var ChangesCollector
24-
*/
2518
private $changesCollector;
2619

27-
/**
28-
* @var PessimisticLock
29-
*/
3020
private $pessimisticLock;
3121

32-
/**
33-
* @var ConvertValues
34-
*/
3522
private $convertValues;
3623

37-
/**
38-
* @var StorageMetaInterface
39-
*/
4024
private $storageMeta;
4125

26+
private $collection;
27+
4228
public function __construct(
43-
Collection $collection,
29+
string $collectionName,
30+
CollectionFactory $collectionFactory,
4431
Hydrator $hydrator,
4532
ChangesCollector $changesCollector = null,
4633
PessimisticLock $pessimisticLock = null,
4734
ConvertValues $convertValues = null,
4835
StorageMetaInterface $storageMeta = null
4936
) {
50-
$this->collection = $collection;
37+
$this->collectionName = $collectionName;
38+
$this->collectionFactory = $collectionFactory;
5139
$this->hydrator = $hydrator;
5240
$this->pessimisticLock = $pessimisticLock;
5341

@@ -81,7 +69,7 @@ public function insert($model, array $options = [])
8169
{
8270
$values = $this->convertValues->convertToMongoValues(get_values($model), []);
8371

84-
$result = $this->collection->insertOne($values, $options);
72+
$result = $this->getCollection()->insertOne($values, $options);
8573
if (false == $result->isAcknowledged()) {
8674
throw new \LogicException('Operation is not acknowledged');
8775
}
@@ -105,7 +93,7 @@ public function insertMany(array $models, array $options = [])
10593
$data[$key] = get_values($model, false);
10694
}
10795

108-
$result = $this->collection->insertMany($data, $options);
96+
$result = $this->getCollection()->insertMany($data, $options);
10997
if (false == $result->isAcknowledged()) {
11098
throw new \LogicException('Operation is not acknowledged');
11199
}
@@ -168,13 +156,13 @@ public function update($model, $filter = null, array $options = [])
168156
$pushUpdate['$push'] = $update['$push'];
169157
unset($update['$push']);
170158

171-
$this->collection->updateOne($filter, $pushUpdate, $options);
159+
$this->getCollection()->updateOne($filter, $pushUpdate, $options);
172160

173161
if ($update) {
174-
$result = $this->collection->updateOne($filter, $update, $options);
162+
$result = $this->getCollection()->updateOne($filter, $update, $options);
175163
}
176164
} else {
177-
$result = $this->collection->updateOne($filter, $update, $options);
165+
$result = $this->getCollection()->updateOne($filter, $update, $options);
178166
}
179167

180168
if ($useOptimisticLock && 0 === $result->getModifiedCount()) {
@@ -216,7 +204,7 @@ public function replace($model, array $options = [])
216204
*/
217205
public function delete($model, array $options = [])
218206
{
219-
return $this->collection->deleteOne(['_id' => get_object_id($model)], $options);
207+
return $this->getCollection()->deleteOne(['_id' => get_object_id($model)], $options);
220208
}
221209

222210
/**
@@ -229,7 +217,7 @@ public function findOne(array $filter = [], array $options = [])
229217
{
230218
$options['typeMap'] = ['root' => 'array', 'document' => 'array', 'array' => 'array'];
231219

232-
if ($originalValues = $this->collection->findOne($filter, $options)) {
220+
if ($originalValues = $this->getCollection()->findOne($filter, $options)) {
233221
$values = $this->convertValues->convertToPHPValues($originalValues);
234222

235223

@@ -249,7 +237,7 @@ public function findOne(array $filter = [], array $options = [])
249237
*/
250238
public function find(array $filter = [], array $options = [])
251239
{
252-
$cursor = $this->collection->find($filter, $options);
240+
$cursor = $this->getCollection()->find($filter, $options);
253241
$cursor->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']);
254242

255243
foreach ($cursor as $originalValues) {
@@ -276,7 +264,7 @@ public function register($object, array $originalValues)
276264
*/
277265
public function count(array $filter = [], array $options = [])
278266
{
279-
return $this->collection->count($filter, $options);
267+
return $this->getCollection()->count($filter, $options);
280268
}
281269

282270
/**
@@ -326,6 +314,10 @@ public function getMeta(): StorageMetaInterface
326314
*/
327315
public function getCollection(): Collection
328316
{
317+
if (null === $this->collection) {
318+
$this->collection = $this->collectionFactory->create($this->collectionName);
319+
}
320+
329321
return $this->collection;
330322
}
331323

0 commit comments

Comments
 (0)