1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Repository; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Finder\Finder; |
6
|
|
|
use AppBundle\Entity\Post; |
7
|
|
|
use Mni\FrontYAML\Parser; |
8
|
|
|
|
9
|
|
|
class PostRepository |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $path; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Parser |
18
|
|
|
*/ |
19
|
|
|
private $parser; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* PostRepository constructor. |
23
|
|
|
* |
24
|
|
|
* @param string $kernelProjectDir |
25
|
|
|
* @param Parser $parser |
26
|
|
|
*/ |
27
|
|
|
public function __construct($kernelProjectDir, Parser $parser) |
28
|
|
|
{ |
29
|
|
|
$this->path = $kernelProjectDir; |
30
|
|
|
$this->parser = $parser; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Gets all Blog posts. |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function findAll() |
39
|
|
|
{ |
40
|
|
|
$finder = new Finder(); |
41
|
|
|
$finder->files()->in($this->path.'/app/Resources/content/blog'); |
42
|
|
|
$finder->files()->name('*.md'); |
43
|
|
|
$finder->sort(function($a, $b) { return strcmp($b->getRealpath(), $a->getRealpath()); }); |
44
|
|
|
|
45
|
|
|
$posts = []; |
46
|
|
|
foreach ($finder as $file) { |
47
|
|
|
$post = $this->getPostByFile($file->getRealPath()); |
48
|
|
|
$posts[] = $post; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $posts; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Find one Post by parameters. |
56
|
|
|
* |
57
|
|
|
* @param array $params Search parameters. |
58
|
|
|
* @return null|Post |
59
|
|
|
*/ |
60
|
|
|
public function findOneBy(array $params) { |
61
|
|
|
$finder = new Finder(); |
62
|
|
|
$fileName = $params['year'].'-'.$params['month'].'-'.$params['day'].($params['num'] === 0 ? '' : '-'.$params['num']).'.md'; |
63
|
|
|
$finder->files()->in($this->path.'/app/Resources/content/blog')->name($fileName); |
64
|
|
|
$iterator = $finder->getIterator(); |
65
|
|
|
$iterator->rewind(); |
66
|
|
|
$file = $iterator->current(); |
67
|
|
|
|
68
|
|
|
$post = $this->getPostByFile($file->getRealPath()); |
69
|
|
|
|
70
|
|
|
if ($finder->count() == 0 || $params['slug'] != $post->getSlug()) { |
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $post; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Find limited latest posts. |
79
|
|
|
* |
80
|
|
|
* @param int $limit |
81
|
|
|
* @return null|array |
82
|
|
|
*/ |
83
|
|
|
public function findLatest($limit = 10) |
84
|
|
|
{ |
85
|
|
|
$finder = new Finder(); |
86
|
|
|
$finder->files()->in($this->path.'/app/Resources/content/blog'); |
87
|
|
|
$finder->files()->name('*.md'); |
88
|
|
|
$finder->sort(function($a, $b) { return strcmp($b->getRealpath(), $a->getRealpath()); }); |
89
|
|
|
|
90
|
|
|
$posts = []; |
91
|
|
|
foreach (new \LimitIterator($finder->getIterator(), 0, $limit) as $file) { |
92
|
|
|
$post = $this->getPostByFile($file->getRealPath()); |
93
|
|
|
$posts[] = $post; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $posts; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $file |
101
|
|
|
* @return Post|null |
102
|
|
|
*/ |
103
|
|
|
public function getPostByFile($file) |
104
|
|
|
{ |
105
|
|
|
if (!file_exists($file)) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$document = $this->parser->parse(file_get_contents($file)); |
110
|
|
|
|
111
|
|
|
$post = new Post(); |
112
|
|
|
$post->setTitle($document->getYaml()['title']); |
113
|
|
|
$post->setReadTime($document->getYaml()['read_time']); |
114
|
|
|
$post->setIntro($document->getYaml()['intro']); |
115
|
|
|
$post->setCreated(substr(basename($file), 0, 10)); |
116
|
|
|
$post->setUpdated($document->getYAML()['updated']); |
117
|
|
|
$post->setNum(substr(basename($file), 11, -3)); |
118
|
|
|
$post->setSlug($document->getYaml()['slug']); |
119
|
|
|
$post->setContent($document->getContent()); |
120
|
|
|
$post->setFile(basename($file)); |
121
|
|
|
|
122
|
|
|
return $post; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|