1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace AloiaCms\Models; |
5
|
|
|
|
6
|
|
|
use ContentParser\ContentParser; |
7
|
|
|
use AloiaCms\InlineBlockParser; |
8
|
|
|
use AloiaCms\Models\Contracts\ModelInterface; |
9
|
|
|
use AloiaCms\Models\Contracts\StorableInterface; |
10
|
|
|
use AloiaCms\Writer\FolderCreator; |
11
|
|
|
use AloiaCms\Writer\FrontMatterCreator; |
12
|
|
|
use Illuminate\Support\Arr; |
13
|
|
|
use Illuminate\Support\Collection; |
14
|
|
|
use Illuminate\Support\Facades\Config; |
15
|
|
|
use Illuminate\Support\Facades\File; |
16
|
|
|
use Spatie\YamlFrontMatter\YamlFrontMatter; |
17
|
|
|
|
18
|
|
|
class Model implements ModelInterface, StorableInterface |
19
|
|
|
{ |
20
|
|
|
protected $folder = ''; |
21
|
|
|
|
22
|
|
|
protected $file_name = null; |
23
|
|
|
|
24
|
|
|
protected $extension = 'md'; |
25
|
|
|
|
26
|
|
|
protected $matter = []; |
27
|
|
|
|
28
|
|
|
protected $body = ''; |
29
|
|
|
|
30
|
|
|
protected $required_fields = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Find a single model |
34
|
|
|
* |
35
|
|
|
* @param string $file_name |
36
|
|
|
* @return ModelInterface |
37
|
|
|
*/ |
38
|
61 |
|
public static function find(string $file_name): ModelInterface |
39
|
|
|
{ |
40
|
61 |
|
$instance = new static(); |
41
|
|
|
|
42
|
61 |
|
$instance->setFileName($file_name); |
43
|
|
|
|
44
|
61 |
|
return $instance; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return all instances of the model |
49
|
|
|
* |
50
|
|
|
* @return Collection|ModelInterface[] |
51
|
|
|
*/ |
52
|
7 |
|
public static function all(): Collection |
53
|
|
|
{ |
54
|
7 |
|
$instance = new static(); |
55
|
|
|
|
56
|
7 |
|
$files = File::allFiles($instance->getFolderPath()); |
57
|
|
|
|
58
|
7 |
|
return Collection::make($files) |
59
|
7 |
|
->map(function (\SplFileInfo $fileInfo): ModelInterface { |
60
|
7 |
|
return self::find(pathinfo($fileInfo->getFilename(), PATHINFO_FILENAME)); |
61
|
7 |
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Rename this file to the given name |
66
|
|
|
* |
67
|
|
|
* @param string $new_name |
68
|
|
|
* @return Model |
69
|
|
|
*/ |
70
|
1 |
|
public function rename(string $new_name): ModelInterface |
71
|
|
|
{ |
72
|
1 |
|
$old_file_path = $this->getFilePath(); |
73
|
|
|
|
74
|
1 |
|
$this->file_name = $new_name; |
75
|
|
|
|
76
|
1 |
|
$new_file_path = $this->getFilePath(); |
77
|
|
|
|
78
|
1 |
|
File::move($old_file_path, $new_file_path); |
79
|
|
|
|
80
|
1 |
|
return self::find($new_name); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the raw content of the file + front matter |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
61 |
|
public function rawContent(): string |
89
|
|
|
{ |
90
|
61 |
|
$file_path = $this->getFilePath(); |
91
|
|
|
|
92
|
61 |
|
if ($this->exists()) { |
93
|
47 |
|
return file_get_contents($file_path); |
94
|
|
|
} |
95
|
|
|
|
96
|
60 |
|
return ""; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Parse the file for this model into model variables |
101
|
|
|
*/ |
102
|
61 |
|
private function parseFile(): void |
103
|
|
|
{ |
104
|
61 |
|
$parsed_file = YamlFrontMatter::parse($this->rawContent()); |
105
|
|
|
|
106
|
61 |
|
$this->matter = $parsed_file->matter(); |
107
|
61 |
|
$this->body = $parsed_file->body(); |
108
|
61 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Save this instance to file |
112
|
|
|
* |
113
|
|
|
* @return ModelInterface |
114
|
|
|
* @throws \Exception |
115
|
|
|
*/ |
116
|
52 |
|
public function save(): ModelInterface |
117
|
|
|
{ |
118
|
52 |
|
$file_content = FrontMatterCreator::seed($this->matter, $this->body)->create(); |
119
|
|
|
|
120
|
52 |
|
$this->assertFilenameExists(); |
121
|
|
|
|
122
|
51 |
|
$this->assertRequiredMatterIsPresent(); |
123
|
|
|
|
124
|
49 |
|
$file_path = $this->getFilePath(); |
125
|
|
|
|
126
|
49 |
|
file_put_contents($file_path, $file_content); |
127
|
|
|
|
128
|
49 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the file path for this instance |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
61 |
|
private function getFilePath(): string |
137
|
|
|
{ |
138
|
61 |
|
$folder_path = $this->getFolderPath(); |
139
|
|
|
|
140
|
61 |
|
if (!is_null($matching_filepath = $this->getFileMatchFromDisk())) { |
141
|
47 |
|
$this->setExtension(pathinfo($matching_filepath, PATHINFO_EXTENSION)); |
142
|
|
|
} |
143
|
|
|
|
144
|
61 |
|
return "{$folder_path}/{$this->file_name}.{$this->extension}"; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the folder path for this model |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
64 |
|
public function getFolderPath(): string |
153
|
|
|
{ |
154
|
64 |
|
$folder_path = Config::get('aloiacms.collections_path') . "/{$this->folder}"; |
155
|
|
|
|
156
|
64 |
|
FolderCreator::forPath($folder_path); |
157
|
|
|
|
158
|
64 |
|
return $folder_path; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the front matter |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
2 |
|
public function matter(): array |
167
|
|
|
{ |
168
|
2 |
|
return $this->matter; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set a value on the specified key in the configuration |
173
|
|
|
* |
174
|
|
|
* Kept around for backward compatibility |
175
|
|
|
* |
176
|
|
|
* @param string $key |
177
|
|
|
* @param $value |
178
|
|
|
* @return ModelInterface |
179
|
|
|
* |
180
|
|
|
* @deprecated since 3.2.0 |
181
|
|
|
*/ |
182
|
|
|
public function addMatter(string $key, $value): ModelInterface |
183
|
|
|
{ |
184
|
|
|
return $this->set($key, $value); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set data in the front matter, but only for the keys specified in the input array |
189
|
|
|
* |
190
|
|
|
* @param array $matter |
191
|
|
|
* @return ModelInterface |
192
|
|
|
*/ |
193
|
45 |
|
public function setMatter(array $matter): ModelInterface |
194
|
|
|
{ |
195
|
45 |
|
foreach (array_keys($matter) as $key) { |
196
|
45 |
|
$this->matter[$key] = $matter[$key]; |
197
|
|
|
} |
198
|
|
|
|
199
|
45 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Determine whether a key is present in the configuration |
204
|
|
|
* |
205
|
|
|
* @param string $key |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
public function has(string $key): bool |
209
|
|
|
{ |
210
|
|
|
return isset($this->matter[$key]); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get the value of the specified key, return null if it doesn't exist |
215
|
|
|
* |
216
|
|
|
* @param string $key |
217
|
|
|
* @return mixed|null |
218
|
|
|
*/ |
219
|
14 |
|
public function get(string $key) |
220
|
|
|
{ |
221
|
14 |
|
return $this->matter[$key] ?? null; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set a value on the specified key in the configuration |
226
|
|
|
* |
227
|
|
|
* @param string $key |
228
|
|
|
* @param $value |
229
|
|
|
* @return $this|ModelInterface |
230
|
|
|
*/ |
231
|
2 |
|
public function set(string $key, $value): ModelInterface |
232
|
|
|
{ |
233
|
2 |
|
$this->matter[$key] = $value; |
234
|
|
|
|
235
|
2 |
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get the raw file body |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
20 |
|
public function rawBody(): string |
244
|
|
|
{ |
245
|
20 |
|
return $this->body; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get the parse file body |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
18 |
|
public function body(): string |
254
|
|
|
{ |
255
|
18 |
|
$content = new ContentParser($this->rawBody(), $this->extension()); |
256
|
|
|
|
257
|
18 |
|
return (new InlineBlockParser)->parseHtmlString($content->parse()); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Set the file body |
262
|
|
|
* |
263
|
|
|
* @param string $body |
264
|
|
|
* @return ModelInterface |
265
|
|
|
*/ |
266
|
38 |
|
public function setBody(string $body): ModelInterface |
267
|
|
|
{ |
268
|
38 |
|
$this->body = $body; |
269
|
|
|
|
270
|
38 |
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Get the file extension |
275
|
|
|
* |
276
|
|
|
* @return string |
277
|
|
|
*/ |
278
|
19 |
|
public function extension(): string |
279
|
|
|
{ |
280
|
19 |
|
return $this->extension; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Set the file extension |
285
|
|
|
* |
286
|
|
|
* @param string $extension |
287
|
|
|
* @return ModelInterface |
288
|
|
|
*/ |
289
|
47 |
|
public function setExtension(string $extension): ModelInterface |
290
|
|
|
{ |
291
|
47 |
|
$this->extension = $extension; |
292
|
|
|
|
293
|
47 |
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Get the file name for this instance |
298
|
|
|
* |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
1 |
|
public function filename(): ?string |
302
|
|
|
{ |
303
|
1 |
|
return $this->file_name; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Set the file name for this instance |
308
|
|
|
* |
309
|
|
|
* @param string $file_name |
310
|
|
|
* @return ModelInterface |
311
|
|
|
*/ |
312
|
61 |
|
protected function setFileName(string $file_name): ModelInterface |
313
|
|
|
{ |
314
|
61 |
|
$this->file_name = $file_name; |
315
|
|
|
|
316
|
61 |
|
$this->parseFile(); |
317
|
|
|
|
318
|
61 |
|
return $this; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Get all models for this type |
323
|
|
|
* |
324
|
|
|
* @return array |
325
|
|
|
*/ |
326
|
61 |
|
private function getModelFiles(): array |
327
|
|
|
{ |
328
|
61 |
|
return File::allFiles($this->getFolderPath()); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Get the filename from disk |
333
|
|
|
* |
334
|
|
|
* @return string|null |
335
|
|
|
*/ |
336
|
61 |
|
private function getFileMatchFromDisk(): ?string |
337
|
|
|
{ |
338
|
61 |
|
return Arr::first($this->getModelFiles(), function (string $file_name) { |
339
|
47 |
|
return strpos($file_name, "/{$this->file_name}."); |
340
|
61 |
|
}); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Determine whether the current model exists |
345
|
|
|
* |
346
|
|
|
* @return bool |
347
|
|
|
*/ |
348
|
61 |
|
public function exists(): bool |
349
|
|
|
{ |
350
|
61 |
|
return !is_null($this->getFileMatchFromDisk()); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Delete the current model |
355
|
|
|
* |
356
|
|
|
* @return bool |
357
|
|
|
*/ |
358
|
2 |
|
public function delete(): bool |
359
|
|
|
{ |
360
|
2 |
|
return File::delete($this->getFilePath()); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Get front matter information through an accessor |
365
|
|
|
* |
366
|
|
|
* @param $key |
367
|
|
|
* @return mixed|null |
368
|
|
|
*/ |
369
|
12 |
|
public function __get($key) |
370
|
|
|
{ |
371
|
12 |
|
return $this->get($key); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Throw exception when the file name is not set for this instance |
376
|
|
|
* |
377
|
|
|
* @throws \Exception |
378
|
|
|
*/ |
379
|
52 |
|
private function assertFilenameExists() |
380
|
|
|
{ |
381
|
52 |
|
if (is_null($this->file_name)) { |
382
|
1 |
|
throw new \Exception("Filename is required"); |
383
|
|
|
} |
384
|
51 |
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Throw exception if at least one required matter attribute is not present |
388
|
|
|
* |
389
|
|
|
* @throws \Exception |
390
|
|
|
*/ |
391
|
51 |
|
private function assertRequiredMatterIsPresent() |
392
|
|
|
{ |
393
|
51 |
|
foreach ($this->required_fields as $required_field) { |
394
|
43 |
|
if (!isset($this->matter[$required_field])) { |
395
|
43 |
|
throw new \Exception("Attribute {$required_field} is required"); |
396
|
|
|
} |
397
|
|
|
} |
398
|
49 |
|
} |
399
|
|
|
} |
400
|
|
|
|