1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Lichtenwallner (https://lichtenwallner.at) |
4
|
|
|
* |
5
|
|
|
* @see https://github.com/jolicht/markdown-cms for the canonical source repository |
6
|
|
|
* @license https://github.com/jolicht/markdown-cms/blob/master/LICENSE MIT |
7
|
|
|
* @copyright Copyright (c) Johannes Lichtenwallner |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types = 1); |
10
|
|
|
namespace Jolicht\MarkdownCms\Action; |
11
|
|
|
|
12
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface; |
13
|
|
|
use Jolicht\MarkdownCms\ContentType\ContentCreator; |
14
|
|
|
use Jolicht\MarkdownCms\Markdown\MarkdownDocumentParser; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
18
|
|
|
|
19
|
|
|
class BlogOverviewAction |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Template renderer |
23
|
|
|
* |
24
|
|
|
* @var TemplateRendererInterface |
25
|
|
|
*/ |
26
|
|
|
private $templateRenderer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Content creator |
30
|
|
|
* |
31
|
|
|
* @var ContentCreator |
32
|
|
|
*/ |
33
|
|
|
private $contentCreator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Markdown component parser |
37
|
|
|
* |
38
|
|
|
* @var MarkdownDocumentParser |
39
|
|
|
*/ |
40
|
|
|
private $markdownDocumentParser; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Markdown cms config |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $markdownCmsConfig; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor |
51
|
|
|
* |
52
|
|
|
* @param TemplateRendererInterface $templateRenderer |
53
|
|
|
* @param ContentCreator $contentCreator |
54
|
|
|
* @param MarkdownDocumentParser $markdownDocumentParser |
55
|
|
|
* @param array $markdownCmsConfig |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function __construct(TemplateRendererInterface $templateRenderer, ContentCreator $contentCreator, |
|
|
|
|
58
|
|
|
MarkdownDocumentParser $markdownDocumentParser, array $markdownCmsConfig) |
59
|
|
|
{ |
60
|
|
|
$this->templateRenderer = $templateRenderer; |
61
|
|
|
$this->contentCreator = $contentCreator; |
62
|
|
|
$this->markdownDocumentParser = $markdownDocumentParser; |
63
|
|
|
$this->markdownCmsConfig = $markdownCmsConfig; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) |
67
|
|
|
{ |
68
|
|
|
$options = $this->markdownCmsConfig['options']; |
69
|
|
|
$config = $this->markdownCmsConfig['content']; |
70
|
|
|
|
71
|
|
|
$entries = []; |
72
|
|
|
foreach ($config['content_types']['blogEntry']['all'] as $id) { |
73
|
|
|
|
74
|
|
|
$entries[] = ($this->contentCreator)( |
75
|
|
|
($this->markdownDocumentParser)( |
76
|
|
|
file_get_contents($options['content_dir'] . '/' . $config['src'][$id]) |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$serverParams = $request->getServerParams(); |
83
|
|
|
|
84
|
|
|
$data = [ |
85
|
|
|
'entries' => $entries, |
86
|
|
|
'chanonical_url' => $serverParams['REQUEST_SCHEME'] . '://' . $serverParams['SERVER_NAME'] . |
87
|
|
|
'/blog.html' |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
return new HtmlResponse($this->templateRenderer->render('app::blog-overwiew.twig', $data)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get template renderer |
95
|
|
|
* |
96
|
|
|
* @return TemplateRendererInterface |
97
|
|
|
*/ |
98
|
|
|
public function getTemplateRenderer() : TemplateRendererInterface |
99
|
|
|
{ |
100
|
|
|
return $this->templateRenderer; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get content creator |
105
|
|
|
* |
106
|
|
|
* @return ContentCreator |
107
|
|
|
*/ |
108
|
|
|
public function getContentCreator() : ContentCreator |
109
|
|
|
{ |
110
|
|
|
return $this->contentCreator; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get markdown document parser |
115
|
|
|
* |
116
|
|
|
* @return MarkdownDocumentParser |
117
|
|
|
*/ |
118
|
|
|
public function getMarkdownDocumentParser() : MarkdownDocumentParser |
119
|
|
|
{ |
120
|
|
|
return $this->markdownDocumentParser; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get markdown cms config |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getMarkdownCmsConfig() : array |
129
|
|
|
{ |
130
|
|
|
return $this->markdownCmsConfig; |
131
|
|
|
} |
132
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.