1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matks\MarkdownBlogBundle\Blog\Factory; |
4
|
|
|
|
5
|
|
|
use Matks\MarkdownBlogBundle\Blog\ExternalPost; |
6
|
|
|
use Matks\MarkdownBlogBundle\Blog\Library; |
7
|
|
|
use Matks\MarkdownBlogBundle\Blog\Post; |
8
|
|
|
use Matks\MarkdownBlogBundle\Blog\Register\LibraryRegisterInterface; |
9
|
|
|
use Matks\MarkdownBlogBundle\Blog\Register\RegisterEntry; |
10
|
|
|
|
11
|
|
|
class LibraryFactory |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $postsDirectory; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var LibraryRegisterInterface |
20
|
|
|
*/ |
21
|
|
|
private $libraryRegister; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $postsDirectory path of the posts directory |
25
|
|
|
* @param LibraryRegisterInterface $libraryRegister |
26
|
|
|
*/ |
27
|
1 |
|
public function __construct($postsDirectory, LibraryRegisterInterface $libraryRegister) |
28
|
|
|
{ |
29
|
1 |
|
if (substr($postsDirectory, -1) !== '/') { |
30
|
1 |
|
$postsDirectory = $postsDirectory . '/'; |
31
|
1 |
|
} |
32
|
|
|
|
33
|
1 |
|
$this->validateDirectory($postsDirectory); |
34
|
|
|
|
35
|
1 |
|
$this->postsDirectory = $postsDirectory; |
36
|
1 |
|
$this->libraryRegister = $libraryRegister; |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return Library |
41
|
|
|
*/ |
42
|
|
|
public function buildLibrary() |
43
|
|
|
{ |
44
|
1 |
|
$markdownFiles = $this->getMarkdownFiles(); |
45
|
1 |
|
$posts = []; |
46
|
|
|
|
47
|
1 |
|
foreach ($markdownFiles as $markdownFile) { |
48
|
|
|
|
49
|
1 |
|
$onlyFilename = basename($markdownFile, '.md'); |
50
|
1 |
|
if ($this->libraryRegister->isRegistered($onlyFilename)) { |
51
|
1 |
|
$entry = $this->libraryRegister->getEntry($onlyFilename); |
52
|
|
|
|
53
|
1 |
|
$post = $this->buildPostFromRegisterEntry($markdownFile, $entry); |
|
|
|
|
54
|
1 |
|
} else { |
55
|
1 |
|
$post = $this->buildPost($markdownFile); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
$posts[] = $post; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
$library = new Library($posts); |
62
|
|
|
|
63
|
1 |
|
return $library; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $filename |
68
|
|
|
* |
69
|
|
|
* @return Post |
70
|
|
|
*/ |
71
|
|
|
private function buildPost($filename) |
72
|
|
|
{ |
73
|
1 |
|
$filepath = $this->postsDirectory . $filename; |
74
|
1 |
|
$publishDate = $this->computeFilePublishDate($filepath); |
75
|
1 |
|
$name = basename($filename, '.md'); |
76
|
|
|
|
77
|
1 |
|
$post = new Post($filepath, $name, $publishDate); |
78
|
|
|
|
79
|
1 |
|
return $post; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $filename |
84
|
|
|
* @param RegisterEntry $entry |
85
|
|
|
* |
86
|
|
|
* @return Post |
87
|
|
|
*/ |
88
|
|
|
private function buildPostFromRegisterEntry($filename, RegisterEntry $entry) |
89
|
|
|
{ |
90
|
1 |
|
$filepath = $this->postsDirectory . $filename; |
91
|
|
|
|
92
|
1 |
|
if (null !== $entry->getPublishDate()) { |
93
|
1 |
|
$publishDate = $entry->getPublishDate(); |
94
|
1 |
|
} else { |
95
|
|
|
$publishDate = $this->computeFilePublishDate($filepath); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
if (null !== $entry->getAlias()) { |
99
|
|
|
$name = $entry->getAlias(); |
100
|
|
|
} else { |
101
|
1 |
|
$name = basename($filename, '.md'); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
if ($entry->getBlogType() === Post::TYPE_EXTERNAL) { |
105
|
|
|
$post = new ExternalPost( |
106
|
|
|
$filepath, |
107
|
|
|
$name, |
108
|
|
|
$publishDate, |
109
|
|
|
$entry->getCategory(), |
110
|
|
|
$entry->getTags(), |
111
|
|
|
$entry->getUrl() |
112
|
|
|
); |
113
|
|
|
} else { |
114
|
1 |
|
$post = new Post( |
115
|
1 |
|
$filepath, |
116
|
1 |
|
$name, |
117
|
1 |
|
$publishDate, |
118
|
1 |
|
$entry->getCategory(), |
119
|
1 |
|
$entry->getTags() |
120
|
1 |
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return $post; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $postsDirectory |
128
|
|
|
* |
129
|
|
|
* @throws \InvalidArgumentException |
130
|
|
|
*/ |
131
|
|
|
private function validateDirectory($postsDirectory) |
132
|
|
|
{ |
133
|
1 |
|
if (false === is_dir($postsDirectory)) { |
134
|
1 |
|
throw new \InvalidArgumentException("$postsDirectory is not a directory"); |
135
|
|
|
} |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return string[] |
140
|
|
|
*/ |
141
|
|
|
private function getMarkdownFiles() |
142
|
|
|
{ |
143
|
1 |
|
$scan = scandir($this->postsDirectory); |
144
|
|
|
|
145
|
1 |
|
$markdownFiles = []; |
146
|
1 |
|
foreach ($scan as $file) { |
147
|
1 |
|
if (in_array($file, ['.', '..'])) { |
148
|
1 |
|
continue; |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
$hasMdExtension = ('.md' === substr($file, -3)); |
152
|
1 |
|
if (false === $hasMdExtension) { |
153
|
1 |
|
continue; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
$markdownFiles[] = $file; |
157
|
1 |
|
} |
158
|
|
|
|
159
|
1 |
|
return $markdownFiles; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $filepath |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
private function computeFilePublishDate($filepath) |
168
|
|
|
{ |
169
|
1 |
|
$creationTimestamp = filectime($filepath); |
170
|
1 |
|
if (false === $creationTimestamp) { |
171
|
|
|
throw new \InvalidArgumentException("Could not get creation date of file $filepath"); |
172
|
|
|
} |
173
|
1 |
|
$creationDateTime = new \DateTime('@' . $creationTimestamp); |
174
|
|
|
|
175
|
1 |
|
return $creationDateTime->format('Y-m-d'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: