|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Docs\Convert; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
6
|
|
|
|
|
7
|
|
|
class Document |
|
8
|
|
|
{ |
|
9
|
|
|
private const METATAG_REGEX = '/^\[(.*?)\]:\s*<>\((.*?)\)\s*?$/m'; |
|
10
|
|
|
|
|
11
|
|
|
private const IGNORE_TAGS = [ |
|
12
|
|
|
'titleDe', |
|
13
|
|
|
]; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var Document |
|
17
|
|
|
*/ |
|
18
|
|
|
private $parent = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Document[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private $children = []; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
private $isCatgory; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var SplFileInfo |
|
30
|
|
|
*/ |
|
31
|
|
|
private $file; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $baseUrl; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var int |
|
38
|
|
|
*/ |
|
39
|
|
|
private $categoryId; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct(SplFileInfo $file, bool $isCatgory, string $baseUrl) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->file = $file; |
|
44
|
|
|
$this->isCatgory = $isCatgory; |
|
45
|
|
|
$this->baseUrl = $baseUrl; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getFile(): SplFileInfo |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->file; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getUrlPart(): string |
|
54
|
|
|
{ |
|
55
|
|
|
if ($this->isCatgory) { |
|
56
|
|
|
$part = basename($this->getFile()->getRelativePath()); |
|
57
|
|
|
} else { |
|
58
|
|
|
$part = $this->getFile()->getBasename('.md'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$parts = explode('-', $part); |
|
62
|
|
|
|
|
63
|
|
|
array_shift($parts); |
|
64
|
|
|
|
|
65
|
|
|
return implode('-', $parts); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getBaseUrl(): string |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->baseUrl; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function isCategory(): bool |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->isCatgory; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function setParent(Document $document): void |
|
79
|
|
|
{ |
|
80
|
|
|
$this->parent = $document; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getParent(): ?Document |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->parent; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function addChild(Document $child): void |
|
89
|
|
|
{ |
|
90
|
|
|
$this->children[] = $child; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function getChild(string $childPath): Document |
|
94
|
|
|
{ |
|
95
|
|
|
foreach ($this->children as $child) { |
|
96
|
|
|
echo ($child->getFile()->getRelativePathname()) . PHP_EOL; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function loadRawMetadata(): array |
|
101
|
|
|
{ |
|
102
|
|
|
$fileContents = $this->file->getContents(); |
|
103
|
|
|
$metadata = []; |
|
104
|
|
|
|
|
105
|
|
|
$matches = []; |
|
106
|
|
|
if (!preg_match_all(self::METATAG_REGEX, $fileContents, $matches, PREG_SET_ORDER)) { |
|
107
|
|
|
throw new \InvalidArgumentException(sprintf('Missing metadata in %s', $this->file)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
foreach ($matches as $match) { |
|
111
|
|
|
$metadata[$match[1]] = $match[2]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$metadata = array_filter($metadata, function (string $key): bool { |
|
115
|
|
|
return !in_array($key, self::IGNORE_TAGS, true); |
|
116
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
117
|
|
|
|
|
118
|
|
|
if (!$metadata) { |
|
119
|
|
|
throw new \InvalidArgumentException(sprintf('Missing metadata in %s', $this->file)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $metadata; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getMetadata(): DocumentMetadata |
|
126
|
|
|
{ |
|
127
|
|
|
return new DocumentMetadata($this); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getHtml(): DocumentHtml |
|
131
|
|
|
{ |
|
132
|
|
|
return new DocumentHtml($this); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function cliOut() |
|
136
|
|
|
{ |
|
137
|
|
|
$result = [ |
|
138
|
|
|
'path' => $this->file->getRealPath(), |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
if (!$this->children) { |
|
142
|
|
|
return $result; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$result['children'] = []; |
|
146
|
|
|
|
|
147
|
|
|
foreach ($this->children as $child) { |
|
148
|
|
|
$result['children'][] = $child->cliOut(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $result; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function getPriority(): int |
|
155
|
|
|
{ |
|
156
|
|
|
$path = dirname($this->file->getRealPath()); |
|
157
|
|
|
$self = $this->getFile()->getBasename(); |
|
158
|
|
|
|
|
159
|
|
|
if ($this->isCategory()) { |
|
160
|
|
|
$path = dirname($path); |
|
161
|
|
|
$self = pathinfo(dirname($this->getFile()->getRealPath()), PATHINFO_BASENAME); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$files = []; |
|
165
|
|
|
foreach (scandir($path, SCANDIR_SORT_ASCENDING) as $file) { |
|
166
|
|
|
$files[] = $file; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$index = array_search($self, $files, true); |
|
170
|
|
|
|
|
171
|
|
|
return count($files) - $index; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function createParentChain(): array |
|
175
|
|
|
{ |
|
176
|
|
|
$chain = []; |
|
177
|
|
|
$current = $this; |
|
178
|
|
|
do { |
|
179
|
|
|
$chain[] = $current; |
|
180
|
|
|
$current = $current->getParent(); |
|
181
|
|
|
} while ($current !== null); |
|
182
|
|
|
|
|
183
|
|
|
return array_reverse($chain); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function setCategoryId(int $id) |
|
187
|
|
|
{ |
|
188
|
|
|
$this->categoryId = $id; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function getCategoryId(): ?int |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->categoryId; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|