|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mattbit\Flat\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use Mattbit\Flat\Query\Parser; |
|
6
|
|
|
use Mattbit\Flat\Query\Matcher; |
|
7
|
|
|
use Mattbit\Flat\Document\Document; |
|
8
|
|
|
use League\Flysystem\FilesystemInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Engine |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var FilesystemInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $filesystem; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Encoder |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $encoder; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(FilesystemInterface $filesystem, JsonEncoder $encoder) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->filesystem = $filesystem; |
|
25
|
|
|
$this->encoder = $encoder; |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Get the Filesystem instance. |
|
30
|
|
|
* |
|
31
|
|
|
* @return FilesystemInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getFilesystem() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->filesystem; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get the EncoderInterface instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @return EncoderInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getEncoder() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->encoder; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Create a new DocumentStore. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $namespace |
|
53
|
|
|
* @return DocumentStore |
|
54
|
|
|
*/ |
|
55
|
|
|
public function createDocumentStore($namespace) |
|
56
|
|
|
{ |
|
57
|
|
|
return new DocumentStore($this, $namespace); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function dropCollection($collection) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->filesystem->deleteDir($collection); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function createCollection($collection) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->filesystem->createDir($collection); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function truncateCollection($collection) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->dropCollection($collection); |
|
73
|
|
|
$this->createCollection($collection); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function update($collection, array $criteria, array $updates, $multiple = false) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
return $this->onMatch($collection, $criteria, function ($document) use ($updates) { |
|
79
|
|
|
$document = array_merge($document, $updates); |
|
80
|
|
|
|
|
81
|
|
|
return $this->insert($document); |
|
|
|
|
|
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function remove($collection, array $criteria, $multiple = false) |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
return $this->onMatch($collection, $criteria, function ($document, $path) { |
|
88
|
|
|
return $this->filesystem->delete($path); |
|
89
|
|
|
}); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function find($collection, array $criteria) |
|
93
|
|
|
{ |
|
94
|
|
|
$results = []; |
|
95
|
|
|
|
|
96
|
|
|
$this->onMatch($collection, $criteria, function ($document) use (&$results) { |
|
97
|
|
|
$results[] = $document; |
|
98
|
|
|
}, true); |
|
99
|
|
|
|
|
100
|
|
|
return $results; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function all($collection) |
|
104
|
|
|
{ |
|
105
|
|
|
$results = []; |
|
106
|
|
|
|
|
107
|
|
|
foreach ($this->filesystem->listContents($collection) as $meta) { |
|
108
|
|
|
$data = $this->filesystem->read($meta['path']); |
|
109
|
|
|
$results[] = new Document($this->encoder->decode($data)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $results; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function path($collection, $id) |
|
116
|
|
|
{ |
|
117
|
|
|
return sprintf('%s/%s.%s', $collection, $id, Encoder::EXTENSION); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
protected function onMatch($collection, $criteria, \Closure $closure, $multiple = false) |
|
121
|
|
|
{ |
|
122
|
|
|
$count = 0; |
|
123
|
|
|
|
|
124
|
|
|
$expression = $this->parser->parse($criteria); |
|
|
|
|
|
|
125
|
|
|
$matcher = new Matcher($expression); |
|
126
|
|
|
|
|
127
|
|
|
foreach ($this->filesystem->listContents($collection) as $meta) { |
|
128
|
|
|
$data = $this->filesystem->read($meta['path']); |
|
129
|
|
|
$document = new Document($this->encoder->decode($data)); |
|
130
|
|
|
|
|
131
|
|
|
if ($matcher->match($document)) { |
|
132
|
|
|
$count += (int) call_user_func_array($closure, [$document, $meta]); |
|
133
|
|
|
|
|
134
|
|
|
if (!$multiple) { |
|
135
|
|
|
break; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
}; |
|
139
|
|
|
|
|
140
|
|
|
return $count; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..