forked from octobercms/library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoDatasource.php
More file actions
233 lines (190 loc) · 6.22 KB
/
AutoDatasource.php
File metadata and controls
233 lines (190 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php namespace October\Rain\Halcyon\Datasource;
use Illuminate\Support\Arr;
use October\Rain\Halcyon\Model;
use October\Rain\Halcyon\Processors\Processor;
/**
* AutoDatasource loads templates from multiple data sources
*
* @package october\halcyon
* @author Alexey Bobkov, Samuel Georges
*/
class AutoDatasource extends Datasource implements DatasourceInterface
{
/**
* @var array datasources
*/
protected $datasources;
/**
* @var Datasource primaryDatasource
*/
protected $primaryDatasource;
/**
* __construct create a new datasource instance
*/
public function __construct(array $datasources)
{
$this->datasources = $datasources;
$this->primaryDatasource = Arr::first($datasources);
$this->postProcessor = new Processor;
}
/**
* hasTemplate checks if a template is found in the datasource
*/
public function hasTemplate(string $dirName, string $fileName, string $extension): bool
{
foreach ($this->datasources as $datasource) {
if ($datasource->hasTemplate($dirName, $fileName, $extension)) {
return true;
}
}
return false;
}
/**
* selectOne returns a single template
*/
public function selectOne(string $dirName, string $fileName, string $extension)
{
foreach ($this->datasources as $source) {
if (!$source->hasTemplate($dirName, $fileName, $extension)) {
continue;
}
return $source->selectOne($dirName, $fileName, $extension);
}
return null;
}
/**
* select returns all templates
*/
public function select(string $dirName, array $options = []): array
{
$result = [];
// Build from the ground up
foreach (array_reverse($this->datasources) as $datasource) {
$result = array_merge($result, $datasource->select($dirName, $options));
}
return collect($result)->keyBy('fileName')->all();
}
/**
* insert creates a new template
*/
public function insert(string $dirName, string $fileName, string $extension, string $content): bool
{
return $this->primaryDatasource->insert($dirName, $fileName, $extension, $content);
}
/**
* update an existing template
*/
public function update(string $dirName, string $fileName, string $extension, string $content, $oldFileName = null, $oldExtension = null): int
{
$findFileName = $oldFileName ?: $fileName;
$findExt = $oldExtension ?: $extension;
if ($this->primaryDatasource->selectOne($dirName, $findFileName, $findExt)) {
$result = $this->primaryDatasource->update($dirName, $fileName, $extension, $content, $oldFileName, $oldExtension);
}
else {
$result = $this->primaryDatasource->insert($dirName, $fileName, $extension, $content);
}
return $result;
}
/**
* delete against the datasource
*/
public function delete(string $dirName, string $fileName, string $extension): bool
{
return $this->primaryDatasource->delete($dirName, $fileName, $extension);
}
/**
* forceDelete against the datasource, forcing the complete removal of the template
*/
public function forceDelete(string $dirName, string $fileName, string $extension): bool
{
$result = false;
foreach ($this->datasources as $datasource) {
if ($datasource->delete($dirName, $fileName, $extension)) {
$result = true;
}
}
return $result;
}
/**
* lastModified returns the last modified date of an object
*/
public function lastModified(string $dirName, string $fileName, string $extension): ?int
{
foreach ($this->datasources as $source) {
if (!$source->hasTemplate($dirName, $fileName, $extension)) {
continue;
}
return $source->lastModified($dirName, $fileName, $extension);
}
return null;
}
/**
* makeCacheKey unique to this datasource
*/
public function makeCacheKey(string $name = ''): string
{
$key = '';
foreach ($this->datasources as $datasource) {
$key .= $datasource->makeCacheKey($name) . '-';
}
$key .= $name;
return (string) crc32($key);
}
//
// Models
//
/**
* hasIndex returns true if the specified index exists in datasources
*/
public function hasIndex(int $index)
{
return array_key_exists($index, $this->datasources);
}
/**
* hasModelAtIndex
*/
public function hasModelAtIndex($index, Model $model): bool
{
if (!$this->hasIndex($index)) {
return false;
}
// Get the path parts
$dirName = $model->getObjectTypeDirName();
list($fileName, $extension) = $model->getFileNameParts();
// Model doesn't exist
if ($fileName === null) {
return false;
}
return $this->datasources[$index]->hasTemplate($dirName, $fileName, $extension);
}
/**
* updateModelAtIndex updates at a specific datasource index
*/
public function updateModelAtIndex(int $index, Model $model): int
{
if (!$this->hasIndex($index)) {
return 0;
}
// Get the path parts
$dirName = $model->getObjectTypeDirName();
list($fileName, $extension) = $model->getFileNameParts();
$datasource = $this->datasources[$index];
// Get the file content
$content = $datasource->getPostProcessor()->processUpdate($model->newQuery(), []);
return $datasource->update($dirName, $fileName, $extension, $content);
}
/**
* deleteModelAtIndex against a specific datasource index
*/
public function forceDeleteModelAtIndex(int $index, Model $model): bool
{
if (!$this->hasIndex($index)) {
return false;
}
// Get the path parts
$dirName = $model->getObjectTypeDirName();
list($fileName, $extension) = $model->getFileNameParts();
return $this->datasources[$index]->forceDelete($dirName, $fileName, $extension);
}
}