|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace hanneskod\yaysondb; |
|
6
|
|
|
|
|
7
|
|
|
use hanneskod\yaysondb\Engine\EngineInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Handle to a defined set of collections |
|
11
|
|
|
*/ |
|
12
|
|
|
class Yaysondb implements TransactableInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Collection[] Loaded collections |
|
16
|
|
|
*/ |
|
17
|
|
|
private $collections; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Optionally load collection engines |
|
21
|
|
|
* |
|
22
|
|
|
* @param EngineInterface[] $engines Maps identifiers to engines |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(array $engines = []) |
|
25
|
|
|
{ |
|
26
|
|
|
foreach ($engines as $engineId => $engine) { |
|
27
|
|
|
$this->load( |
|
28
|
|
|
$engine, |
|
29
|
|
|
is_string($engineId) ? $engineId : '' |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Load collection engine |
|
36
|
|
|
*/ |
|
37
|
|
|
public function load(EngineInterface $engine, string $engineId = ''): self |
|
38
|
|
|
{ |
|
39
|
|
|
$this->collections[$engineId ?: $engine->getId()] = new Collection($engine); |
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Check if handle contains collection with id |
|
45
|
|
|
*/ |
|
46
|
|
|
public function hasCollection(string $id): bool |
|
47
|
|
|
{ |
|
48
|
|
|
return isset($this->collections[$id]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Magic method to allow collection exploration through property names |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __isset(string $id): bool |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->hasCollection($id); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get collection |
|
61
|
|
|
* |
|
62
|
|
|
* @throws Exception\LogicException If id is not defined |
|
63
|
|
|
*/ |
|
64
|
|
|
public function collection(string $id): CollectionInterface |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$this->hasCollection($id)) { |
|
67
|
|
|
throw new Exception\LogicException("Trying to access undefined collection $id"); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $this->collections[$id]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Magic method to allow collection access through property names |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __get(string $id): CollectionInterface |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->collection($id); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function commit() |
|
82
|
|
|
{ |
|
83
|
|
|
foreach ($this->collections as $collection) { |
|
84
|
|
|
if ($collection->inTransaction()) { |
|
85
|
|
|
$collection->commit(); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function inTransaction(): bool |
|
91
|
|
|
{ |
|
92
|
|
|
foreach ($this->collections as $collection) { |
|
93
|
|
|
if ($collection->inTransaction()) { |
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function reset() |
|
102
|
|
|
{ |
|
103
|
|
|
foreach ($this->collections as $collection) { |
|
104
|
|
|
if ($collection->inTransaction()) { |
|
105
|
|
|
$collection->reset(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|