|
1
|
|
|
<?php |
|
2
|
|
|
namespace TheCodingMachine\CMS\StaticRegistry\Loaders; |
|
3
|
|
|
|
|
4
|
|
|
use Mni\FrontYAML\Parser; |
|
5
|
|
|
use \SplFileInfo; |
|
6
|
|
|
use TheCodingMachine\CMS\Block\BlockInterface; |
|
7
|
|
|
use TheCodingMachine\CMS\Theme\TwigThemeDescriptor; |
|
8
|
|
|
|
|
9
|
|
|
class Block |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $id; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $content; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $lang; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array|string[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $tags; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var null|string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $template; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array|mixed[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $context; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string[] $tags |
|
39
|
|
|
* @param mixed[] $context |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(string $id, string $content, string $lang, array $tags, ?string $template, array $context = []) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->id = $id; |
|
44
|
|
|
$this->content = $content; |
|
45
|
|
|
$this->lang = $lang; |
|
46
|
|
|
$this->tags = $tags; |
|
47
|
|
|
$this->template = $template; |
|
48
|
|
|
$this->context = $context; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public static function fromFile(SplFileInfo $file): self |
|
52
|
|
|
{ |
|
53
|
|
|
if (!is_readable($file->getRealPath())) { |
|
54
|
|
|
throw new UnableToLoadFileException('Cannot read file '.$file); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$extension = strtolower($file->getExtension()); |
|
58
|
|
|
|
|
59
|
|
|
switch ($extension) { |
|
60
|
|
|
case 'md': |
|
61
|
|
|
$parseMarkDown = true; |
|
62
|
|
|
break; |
|
63
|
|
|
case 'html': |
|
64
|
|
|
$parseMarkDown = false; |
|
65
|
|
|
break; |
|
66
|
|
|
default: |
|
67
|
|
|
throw new InvalidExtensionException(sprintf('Invalid extension for block %s. Valid extensions are .md and .html', $file)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$parser = new Parser(); |
|
71
|
|
|
|
|
72
|
|
|
$document = $parser->parse(file_get_contents($file->getRealPath()), $parseMarkDown); |
|
73
|
|
|
|
|
74
|
|
|
$yaml = $document->getYAML(); |
|
75
|
|
|
|
|
76
|
|
|
$compulsoryFields = ['id', 'lang']; |
|
77
|
|
|
|
|
78
|
|
|
foreach ($compulsoryFields as $field) { |
|
79
|
|
|
if (!isset($yaml[$field])) { |
|
80
|
|
|
throw new UnableToLoadFileException('Missing field '.$field.' in YAML front matter of file '.$file); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return new self( |
|
85
|
|
|
$yaml['id'], |
|
86
|
|
|
$document->getContent(), |
|
87
|
|
|
$yaml['lang'], |
|
88
|
|
|
$yaml['tags'] ?? [], |
|
89
|
|
|
$yaml['template'] ?? null, |
|
90
|
|
|
$yaml['context'] ?? [] |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getId(): string |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->id; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getContent(): string |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->content; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getLang(): string |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->lang; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return array|string[] |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getTags() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->tags; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return null|string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getTemplate(): ?string |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->template; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return array|mixed[] |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getContext() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->context; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $themePath |
|
144
|
|
|
* @return BlockInterface|string |
|
145
|
|
|
*/ |
|
146
|
|
|
public function toCmsBlock(string $themePath) |
|
147
|
|
|
{ |
|
148
|
|
|
if ($this->getTemplate() !== null) { |
|
149
|
|
|
$context = $this->getContext(); |
|
150
|
|
|
$context['content'][] = $this->getContent(); |
|
151
|
|
|
return new \TheCodingMachine\CMS\Block\Block(new TwigThemeDescriptor($this->getTemplate(), [ |
|
152
|
|
|
'theme' => $themePath |
|
153
|
|
|
]), $context); |
|
154
|
|
|
} else { |
|
155
|
|
|
return $this->getContent(); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|