|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace hanneskod\yaysondb\Engine; |
|
6
|
|
|
|
|
7
|
|
|
use hanneskod\yaysondb\Exception\LogicException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Simple engine for logging purposes |
|
11
|
|
|
*/ |
|
12
|
|
|
class LogEngine implements EngineInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var resource Log stream |
|
16
|
|
|
*/ |
|
17
|
|
|
private $stream; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var DecoderInterface Decoder used to encode and deconde content |
|
21
|
|
|
*/ |
|
22
|
|
|
private $decoder; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array List of non-commited documents |
|
26
|
|
|
*/ |
|
27
|
|
|
private $newDocs = []; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(string $fname, DecoderInterface $decoder = null) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->stream = fopen($fname, 'a+'); |
|
32
|
|
|
$this->decoder = $decoder ?: new SerializingDecoder; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getIterator(): \Generator |
|
36
|
|
|
{ |
|
37
|
|
|
rewind($this->stream); |
|
38
|
|
|
|
|
39
|
|
|
while ($line = fgets($this->stream)) { |
|
40
|
|
|
yield $this->decoder->decode($line); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @throws LogicException If id is specified |
|
46
|
|
|
*/ |
|
47
|
|
|
public function write(string $id, array $doc): string |
|
48
|
|
|
{ |
|
49
|
|
|
if ($id) { |
|
50
|
|
|
throw new LogicException('Specifying a document id at write is not supported by LogEngine'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->newDocs[] = $doc; |
|
54
|
|
|
return ''; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function inTransaction(): bool |
|
58
|
|
|
{ |
|
59
|
|
|
return !empty($this->newDocs); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function reset() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->newDocs = []; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function commit() |
|
68
|
|
|
{ |
|
69
|
|
|
foreach ($this->newDocs as $doc) { |
|
70
|
|
|
fwrite( |
|
71
|
|
|
$this->stream, |
|
72
|
|
|
$this->decoder->encode($doc) . PHP_EOL |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->reset(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @throws LogicException Not supported by LogEngine |
|
81
|
|
|
*/ |
|
82
|
|
|
public function has(string $id): bool |
|
83
|
|
|
{ |
|
84
|
|
|
throw new LogicException('has() is not supported by LogEngine'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @throws LogicException Not supported by LogEngine |
|
89
|
|
|
*/ |
|
90
|
|
|
public function read(string $id): array |
|
91
|
|
|
{ |
|
92
|
|
|
throw new LogicException('read() is not supported by LogEngine'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @throws LogicException Not supported by LogEngine |
|
97
|
|
|
*/ |
|
98
|
|
|
public function delete(string $id): bool |
|
99
|
|
|
{ |
|
100
|
|
|
throw new LogicException('delete() is not supported by LogEngine'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @throws LogicException Not supported by LogEngine |
|
105
|
|
|
*/ |
|
106
|
|
|
public function clear() |
|
107
|
|
|
{ |
|
108
|
|
|
throw new LogicException('clear() is not supported by LogEngine'); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|