|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flaviozantut\Storage\Posts; |
|
4
|
|
|
|
|
5
|
|
|
use Config; |
|
6
|
|
|
use DOMDocument; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use File; |
|
9
|
|
|
use Flaviozantut\Parser; |
|
10
|
|
|
use StdClass; |
|
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class FilePostRepository implements PostRepositoryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
public $parser; |
|
16
|
|
|
public $directory; |
|
17
|
|
|
|
|
18
|
11 |
|
public function __construct($directory = null) |
|
19
|
|
|
{ |
|
20
|
11 |
|
$this->parser = new Parser(); |
|
21
|
11 |
|
$this->directory = $directory; |
|
22
|
11 |
|
} |
|
23
|
|
|
|
|
24
|
3 |
|
public function intro($post) |
|
25
|
|
|
{ |
|
26
|
3 |
|
if (!$post) { |
|
27
|
|
|
return ''; |
|
28
|
|
|
} |
|
29
|
3 |
|
$doc = new DOMDocument(); |
|
30
|
3 |
|
$doc->loadHTML(mb_convert_encoding($post, 'HTML-ENTITIES', 'UTF-8')); |
|
31
|
|
|
|
|
32
|
|
|
/* Gets all the paragraphs */ |
|
33
|
3 |
|
$p = $doc->getElementsByTagName('p'); |
|
34
|
|
|
/* extracts the first one */ |
|
35
|
|
|
|
|
36
|
3 |
|
if ($p->length > 0) { |
|
37
|
3 |
|
$p = $p->item(0); |
|
38
|
3 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/* returns the paragraph's content */ |
|
41
|
|
|
|
|
42
|
3 |
|
return $p->textContent; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
4 |
|
public function all($type = null) |
|
46
|
|
|
{ |
|
47
|
4 |
|
$all = []; |
|
48
|
4 |
|
foreach (scandir($this->directory) as $file) { |
|
49
|
4 |
|
if (File::extension("{$this->directory}{$file}") != 'md') { |
|
50
|
4 |
|
continue; |
|
51
|
|
|
} |
|
52
|
3 |
|
$this->parser->parse(File::get("{$this->directory}{$file}")); |
|
53
|
|
|
|
|
54
|
3 |
|
if ($type and $type != $this->parser->metadata('type')) { |
|
55
|
|
|
continue; |
|
56
|
|
|
} |
|
57
|
3 |
|
$row = new StdClass(); |
|
58
|
3 |
|
$row->id = $file; |
|
59
|
3 |
|
$row->post = $this->parser->content(); |
|
60
|
|
|
|
|
61
|
3 |
|
$row->intro = $this->intro($row->post); |
|
62
|
3 |
|
$row->type = $this->parser->metadata('type'); |
|
63
|
3 |
|
$row->title = $this->parser->metadata('title'); |
|
64
|
3 |
|
$row->date = date(Config::get('skorry.date_format'), strtotime($this->parser->metadata('date'))); |
|
65
|
3 |
|
$row->comments = $this->parser->metadata('comments'); |
|
66
|
3 |
|
$row->external_url = $this->parser->metadata('external_url'); |
|
67
|
3 |
|
$row->tags = $this->parser->metadata('tags'); |
|
68
|
3 |
|
$row->permalink = $this->parser->metadata('permalink'); |
|
69
|
3 |
|
array_push($all, $row); |
|
70
|
4 |
|
}; |
|
71
|
4 |
|
usort($all, function ($a, $b) { |
|
72
|
|
|
return strtotime($b->date) - strtotime($a->date); |
|
73
|
4 |
|
}); |
|
74
|
|
|
|
|
75
|
4 |
|
return $all; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
public function find($condition) |
|
79
|
|
|
{ |
|
80
|
2 |
|
if (!is_array($condition)) { |
|
81
|
2 |
|
$key = 'id'; |
|
82
|
2 |
|
$value = $condition; |
|
83
|
2 |
|
} else { |
|
84
|
|
|
$key = key($condition); |
|
85
|
|
|
$value = $condition[$key]; |
|
86
|
1 |
|
} |
|
87
|
2 |
|
foreach ($this->all() as $post) { |
|
88
|
2 |
|
if ($post->$key == $value) { |
|
89
|
1 |
|
return $post; |
|
90
|
|
|
} |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
public function store($data) |
|
97
|
|
|
{ |
|
98
|
4 |
|
if (!isset($data['title'])) { |
|
99
|
1 |
|
throw new Exception('Title not defined'); |
|
100
|
|
|
} |
|
101
|
3 |
|
$post = []; |
|
102
|
3 |
|
$post['type'] = (isset($data['type']) and $data['type'] == 'page') ? $data['type'] : 'article'; |
|
103
|
3 |
|
$post['title'] = $data['title']; |
|
104
|
3 |
|
$post['date'] = (isset($data['date']) and strtotime($data['date'])) ? date(Config::get('skorry.date_format'), strtotime($data['date'])) : date(Config::get('skorry.date_format')); |
|
105
|
3 |
|
$post['comments'] = isset($data['comments']) ? true : false; |
|
106
|
3 |
|
$post['external_url'] = isset($data['external_url']) ? $data['external_url'] : null; |
|
107
|
3 |
|
$post['tags'] = isset($data['tags']) ? $data['tags'] : null; |
|
108
|
3 |
|
$post['permalink'] = \Str::slug($data['title'], '-'); |
|
109
|
|
|
|
|
110
|
3 |
|
$mdFile = "{$this->parser->delimiter()}\n"; |
|
111
|
3 |
|
$mdFile .= Yaml::dump($post); |
|
112
|
3 |
|
$mdFile .= "{$this->parser->delimiter()}\n"; |
|
113
|
3 |
|
$mdFile .= isset($data['post']) ? $data['post'] : ''; |
|
114
|
|
|
|
|
115
|
3 |
|
$filname = "{$this->directory}{$post['permalink']}.md"; |
|
116
|
|
|
|
|
117
|
3 |
|
if (file_exists($filname)) { |
|
118
|
1 |
|
throw new Exception('This post already exists'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
File::put($filname, $mdFile); |
|
122
|
|
|
|
|
123
|
2 |
|
return "Your post was created on: $filname"; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
2 |
|
public function update($name, $data) |
|
127
|
|
|
{ |
|
128
|
2 |
|
$this->destroy($name); |
|
129
|
1 |
|
$this->store($data); |
|
130
|
|
|
|
|
131
|
1 |
|
return true; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
4 |
|
public function destroy($name) |
|
135
|
|
|
{ |
|
136
|
4 |
|
$file = "{$this->directory}{$name}"; |
|
137
|
4 |
|
if (!File::exists($file)) { |
|
138
|
2 |
|
throw new Exception('This post not exists,'.$file); |
|
139
|
|
|
} |
|
140
|
2 |
|
File::delete($file); |
|
141
|
|
|
|
|
142
|
2 |
|
return true; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|