1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Manager; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Document\ContentItem; |
11
|
|
|
use allejo\stakx\Document\DynamicPageView; |
12
|
|
|
use allejo\stakx\Document\JailedDocument; |
13
|
|
|
use allejo\stakx\Document\PageView; |
14
|
|
|
use allejo\stakx\Exception\CollectionNotFoundException; |
15
|
|
|
use allejo\stakx\System\FileExplorer; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This class is responsible for handling all of the PageViews within a website. |
19
|
|
|
* |
20
|
|
|
* PageManager will parse all available dynamic and static PageViews. After, dynamic PageViews will be prepared by |
21
|
|
|
* setting the appropriate values for each ContentItem such as permalinks. |
22
|
|
|
* |
23
|
|
|
* @internal |
24
|
|
|
*/ |
25
|
|
|
class PageManager extends TrackingManager |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* A reference to the collections available to this website. |
29
|
|
|
* |
30
|
|
|
* @var ContentItem[][] |
31
|
|
|
*/ |
32
|
|
|
private $collections; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* A place to store a reference to static PageViews with titles. |
36
|
|
|
* |
37
|
|
|
* @var PageView[] |
38
|
|
|
*/ |
39
|
|
|
private $staticPages; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* PageManager constructor. |
43
|
|
|
*/ |
44
|
7 |
|
public function __construct() |
45
|
|
|
{ |
46
|
7 |
|
parent::__construct(); |
47
|
|
|
|
48
|
7 |
|
$this->trackedItems = array( |
49
|
7 |
|
PageView::STATIC_TYPE => array(), |
50
|
7 |
|
PageView::DYNAMIC_TYPE => array(), |
51
|
7 |
|
PageView::REPEATER_TYPE => array(), |
52
|
|
|
); |
53
|
7 |
|
$this->collections = array(); |
54
|
7 |
|
$this->staticPages = array(); |
55
|
7 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Give this manager the collections we'll be using for dynamic PageViews. |
59
|
|
|
* |
60
|
|
|
* @param ContentItem[][] $collections |
61
|
|
|
* |
62
|
|
|
* @since 0.1.0 |
63
|
|
|
*/ |
64
|
4 |
|
public function setCollections(&$collections) |
65
|
|
|
{ |
66
|
4 |
|
$this->collections = &$collections; |
67
|
4 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get all of the PageViews tracked by this manager. |
71
|
|
|
* |
72
|
|
|
* @todo Remove this function |
73
|
|
|
* |
74
|
|
|
* @deprecated Been replaced by getPageViewsFlattened() |
75
|
|
|
* @since 0.1.0 |
76
|
|
|
* |
77
|
|
|
* @return PageView[][] |
78
|
|
|
*/ |
79
|
|
|
public function getAllPageViews() |
80
|
|
|
{ |
81
|
|
|
return $this->trackedItemsFlattened; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get all of the PageViews in an associative array with PageView types as the keys. |
86
|
|
|
* |
87
|
|
|
* @since 0.1.1 |
88
|
|
|
* |
89
|
|
|
* @return PageView[][] |
90
|
|
|
*/ |
91
|
|
|
public function &getPageViews() |
92
|
|
|
{ |
93
|
|
|
return $this->trackedItems; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get all of the PageViews in flat array. |
98
|
|
|
* |
99
|
|
|
* @since 0.1.1 |
100
|
|
|
* |
101
|
|
|
* @return PageView[] |
102
|
|
|
*/ |
103
|
|
|
public function &getPageViewsFlattened() |
104
|
|
|
{ |
105
|
|
|
return $this->trackedItemsFlattened; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the static PageViews tracked by this manager indexed by their title. |
110
|
|
|
* |
111
|
|
|
* @since 0.1.0 |
112
|
|
|
* |
113
|
|
|
* @return PageView[] |
114
|
|
|
*/ |
115
|
7 |
|
public function getStaticPageViews() |
116
|
|
|
{ |
117
|
7 |
|
return $this->staticPages; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the jailed version of the static PageViews indexed by their title. |
122
|
|
|
* |
123
|
|
|
* @since 0.1.0 |
124
|
|
|
* |
125
|
|
|
* @return JailedDocument[] |
126
|
|
|
*/ |
127
|
1 |
|
public function getJailedStaticPageViews() |
128
|
|
|
{ |
129
|
1 |
|
$jailedObjects = array(); |
130
|
|
|
|
131
|
1 |
|
foreach ($this->staticPages as $key => $value) |
132
|
|
|
{ |
133
|
1 |
|
$jailedObjects[$key] = $value->createJail(); |
134
|
1 |
|
} |
135
|
|
|
|
136
|
1 |
|
return $jailedObjects; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Go through all of the PageView directories and create a respective PageView for each and classify them as a |
141
|
|
|
* dynamic or static PageView. |
142
|
|
|
* |
143
|
|
|
* @param string[] $pageViewFolders |
144
|
|
|
* |
145
|
|
|
* @since 0.1.0 |
146
|
|
|
*/ |
147
|
7 |
|
public function parsePageViews($pageViewFolders) |
148
|
|
|
{ |
149
|
7 |
|
if (empty($pageViewFolders)) |
150
|
7 |
|
{ |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
7 |
|
foreach ($pageViewFolders as $pageViewFolderName) |
155
|
|
|
{ |
156
|
|
|
/** @var string $pageViewFolderPath */ |
157
|
7 |
|
$pageViewFolderPath = $this->fs->absolutePath($pageViewFolderName); |
158
|
|
|
|
159
|
7 |
|
if (!$this->fs->exists($pageViewFolderPath)) |
160
|
7 |
|
{ |
161
|
1 |
|
$this->output->warning("The '$pageViewFolderName' folder could not be found"); |
|
|
|
|
162
|
1 |
|
continue; |
163
|
|
|
} |
164
|
|
|
|
165
|
6 |
|
$this->scanTrackableItems($pageViewFolderPath, array( |
166
|
6 |
|
'fileExplorer' => FileExplorer::INCLUDE_ONLY_FILES, |
167
|
6 |
|
), array('/.html$/', '/.twig$/')); |
168
|
6 |
|
} |
169
|
6 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Add a new ContentItem to the respective parent PageView of the ContentItem. |
173
|
|
|
* |
174
|
|
|
* @param ContentItem $contentItem |
175
|
|
|
* |
176
|
|
|
* @since 0.1.0 |
177
|
|
|
*/ |
178
|
1 |
|
public function trackNewContentItem(&$contentItem) |
179
|
|
|
{ |
180
|
1 |
|
$collection = $contentItem->getCollection(); |
181
|
1 |
|
$this->trackedItems[PageView::DYNAMIC_TYPE][$collection]->addContentItem($contentItem); |
182
|
1 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* {@inheritdoc} |
186
|
|
|
*/ |
187
|
6 |
|
protected function &handleTrackableItem($filePath, $options = array()) |
188
|
|
|
{ |
189
|
6 |
|
$pageView = PageView::create($filePath); |
190
|
6 |
|
$namespace = $pageView->getType(); |
191
|
6 |
|
$storageKey = $pageView->getRelativeFilePath(); |
192
|
|
|
|
193
|
|
|
switch ($namespace) |
194
|
|
|
{ |
195
|
6 |
|
case PageView::STATIC_TYPE: |
196
|
2 |
|
$this->handleTrackableStaticPageView($pageView); |
197
|
2 |
|
break; |
198
|
|
|
|
199
|
4 |
|
case PageView::DYNAMIC_TYPE: |
200
|
4 |
|
$this->handleTrackableDynamicPageView($pageView); |
|
|
|
|
201
|
3 |
|
$storageKey = $pageView->getCollection(); |
202
|
3 |
|
break; |
203
|
|
|
|
204
|
|
|
default: |
205
|
|
|
break; |
206
|
|
|
} |
207
|
|
|
|
208
|
5 |
|
$this->addObjectToTracker($pageView, $storageKey, $namespace); |
209
|
|
|
|
210
|
5 |
|
return $pageView; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Handle special behavior and treatment for static PageViews while we're iterating through them. |
215
|
|
|
* |
216
|
|
|
* @param PageView $pageView |
217
|
|
|
* |
218
|
|
|
* @since 0.1.0 |
219
|
|
|
*/ |
220
|
2 |
|
private function handleTrackableStaticPageView(&$pageView) |
221
|
|
|
{ |
222
|
2 |
|
if (empty($pageView['title'])) |
223
|
2 |
|
{ |
224
|
2 |
|
return; |
225
|
|
|
} |
226
|
|
|
|
227
|
2 |
|
$this->staticPages[$pageView['title']] = &$pageView; |
228
|
2 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Handle special behavior and treatment for dynamic PageViews while we're iterating through them. |
232
|
|
|
* |
233
|
|
|
* @param DynamicPageView $pageView |
234
|
|
|
* |
235
|
|
|
* @since 0.1.0 |
236
|
|
|
*/ |
237
|
4 |
|
private function handleTrackableDynamicPageView(&$pageView) |
238
|
|
|
{ |
239
|
4 |
|
$frontMatter = $pageView->getFrontMatter(false); |
240
|
4 |
|
$collection = $frontMatter['collection']; |
241
|
|
|
|
242
|
4 |
|
if (!isset($this->collections[$collection])) |
243
|
4 |
|
{ |
244
|
1 |
|
throw new CollectionNotFoundException("The '$collection' collection is not defined"); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
3 |
|
foreach ($this->collections[$collection] as &$item) |
248
|
|
|
{ |
249
|
3 |
|
$item->evaluateFrontMatter($frontMatter); |
250
|
3 |
|
$pageView->addContentItem($item); |
251
|
3 |
|
} |
252
|
3 |
|
} |
253
|
|
|
} |
254
|
|
|
|
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.