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\Exception\CollectionNotFoundException; |
11
|
|
|
use allejo\stakx\Object\ContentItem; |
12
|
|
|
use allejo\stakx\Object\DynamicPageView; |
13
|
|
|
use allejo\stakx\Object\JailObject; |
14
|
|
|
use allejo\stakx\Object\PageView; |
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->collections = array(); |
49
|
7 |
|
$this->staticPages = array(); |
50
|
7 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Give this manager the collections we'll be using for dynamic PageViews |
54
|
|
|
* |
55
|
|
|
* @param ContentItem[][] $collections |
56
|
|
|
* @since 0.1.0 |
57
|
|
|
*/ |
58
|
4 |
|
public function setCollections (&$collections) |
59
|
|
|
{ |
60
|
4 |
|
$this->collections = &$collections; |
61
|
4 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get all of the PageViews tracked by this manager |
65
|
|
|
* |
66
|
|
|
* @since 0.1.0 |
67
|
|
|
* @return PageView[][] |
68
|
|
|
*/ |
69
|
5 |
|
public function getAllPageViews () |
70
|
|
|
{ |
71
|
5 |
|
return $this->trackedItemsFlattened; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the static PageViews tracked by this manager indexed by their title |
76
|
|
|
* |
77
|
|
|
* @since 0.1.0 |
78
|
|
|
* @return PageView[] |
79
|
|
|
*/ |
80
|
1 |
|
public function getStaticPageViews () |
81
|
|
|
{ |
82
|
1 |
|
return $this->staticPages; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the jailed version of the static PageViews indexed by their title |
87
|
|
|
* |
88
|
|
|
* @since 0.1.0 |
89
|
|
|
* @return JailObject[] |
90
|
|
|
*/ |
91
|
1 |
|
public function getJailedStaticPageViews () |
92
|
|
|
{ |
93
|
1 |
|
$jailedObjects = array(); |
94
|
|
|
|
95
|
1 |
|
foreach ($this->staticPages as $key => $value) |
96
|
|
|
{ |
97
|
1 |
|
$jailedObjects[$key] = $value->createJail(); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return $jailedObjects; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Go through all of the PageView directories and create a respective PageView for each and classify them as a |
105
|
|
|
* dynamic or static PageView. |
106
|
|
|
* |
107
|
|
|
* @param string[] $pageViewFolders |
108
|
|
|
* @since 0.1.0 |
109
|
|
|
*/ |
110
|
7 |
|
public function parsePageViews ($pageViewFolders) |
111
|
|
|
{ |
112
|
7 |
|
if (empty($pageViewFolders)) { return; } |
113
|
|
|
|
114
|
7 |
|
foreach ($pageViewFolders as $pageViewFolderName) |
115
|
|
|
{ |
116
|
|
|
/** @var string $pageViewFolderPath */ |
117
|
7 |
|
$pageViewFolderPath = $this->fs->absolutePath($pageViewFolderName); |
118
|
|
|
|
119
|
7 |
|
if (!$this->fs->exists($pageViewFolderPath)) |
120
|
|
|
{ |
121
|
1 |
|
$this->output->warning("The '$pageViewFolderName' folder could not be found"); |
122
|
1 |
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
6 |
|
$this->scanTrackableItems($pageViewFolderPath, array( |
126
|
|
|
'fileExplorer' => FileExplorer::INCLUDE_ONLY_FILES |
127
|
6 |
|
), array('/.html$/', '/.twig$/')); |
128
|
|
|
} |
129
|
6 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Add a new ContentItem to the respective parent PageView of the ContentItem |
133
|
|
|
* |
134
|
|
|
* @param ContentItem $contentItem |
135
|
|
|
* @since 0.1.0 |
136
|
|
|
*/ |
137
|
1 |
|
public function trackNewContentItem (&$contentItem) |
138
|
|
|
{ |
139
|
1 |
|
$collection = $contentItem->getCollection(); |
140
|
1 |
|
$this->trackedItems[PageView::DYNAMIC_TYPE][$collection]->addContentItem($contentItem); |
141
|
1 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
6 |
|
protected function handleTrackableItem ($filePath, $options = array()) |
147
|
|
|
{ |
148
|
6 |
|
$pageView = PageView::create($filePath); |
149
|
6 |
|
$namespace = $pageView->getType(); |
150
|
6 |
|
$storageKey = $pageView->getRelativeFilePath(); |
151
|
|
|
|
152
|
|
|
switch ($namespace) |
153
|
|
|
{ |
154
|
6 |
|
case PageView::STATIC_TYPE: |
155
|
2 |
|
$this->handleTrackableStaticPageView($pageView); |
156
|
2 |
|
break; |
157
|
|
|
|
158
|
4 |
|
case PageView::DYNAMIC_TYPE: |
159
|
4 |
|
$this->handleTrackableDynamicPageView($pageView); |
|
|
|
|
160
|
3 |
|
$storageKey = $pageView->getCollection(); |
161
|
3 |
|
break; |
162
|
|
|
|
163
|
|
|
default: |
164
|
|
|
break; |
165
|
|
|
} |
166
|
|
|
|
167
|
5 |
|
$this->addObjectToTracker($pageView, $storageKey, $namespace); |
168
|
5 |
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Handle special behavior and treatment for static PageViews while we're iterating through them |
172
|
|
|
* |
173
|
|
|
* @param PageView $pageView |
174
|
|
|
* @since 0.1.0 |
175
|
|
|
*/ |
176
|
2 |
|
private function handleTrackableStaticPageView (&$pageView) |
177
|
|
|
{ |
178
|
2 |
|
if (empty($pageView['title'])) { return; } |
179
|
|
|
|
180
|
2 |
|
$this->staticPages[$pageView['title']] = &$pageView; |
181
|
2 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Handle special behavior and treatment for dynamic PageViews while we're iterating through them |
185
|
|
|
* |
186
|
|
|
* @param DynamicPageView $pageView |
187
|
|
|
* @since 0.1.0 |
188
|
|
|
*/ |
189
|
4 |
|
private function handleTrackableDynamicPageView (&$pageView) |
190
|
|
|
{ |
191
|
4 |
|
$frontMatter = $pageView->getFrontMatter(false); |
192
|
4 |
|
$collection = $frontMatter['collection']; |
193
|
|
|
|
194
|
4 |
|
if (!isset($this->collections[$collection])) |
195
|
|
|
{ |
196
|
1 |
|
throw new CollectionNotFoundException("The '$collection' collection is not defined"); |
197
|
|
|
} |
198
|
|
|
|
199
|
3 |
|
foreach ($this->collections[$collection] as &$item) |
200
|
|
|
{ |
201
|
3 |
|
$item->evaluateFrontMatter($frontMatter); |
202
|
3 |
|
$pageView->addContentItem($item); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.