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\PageView; |
14
|
|
|
use allejo\stakx\System\FileExplorer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This class is responsible for handling all of the PageViews within a website. |
18
|
|
|
* |
19
|
|
|
* PageManager will parse all available dynamic and static PageViews. After, dynamic PageViews will be prepared by |
20
|
|
|
* setting the appropriate values for each ContentItem such as permalinks. Lastly, this class will compile all of the |
21
|
|
|
* PageViews and write them to the target directory. |
22
|
|
|
* |
23
|
|
|
* @package allejo\stakx\Manager |
24
|
|
|
*/ |
25
|
|
|
class PageManager extends TrackingManager |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The relative (to the stakx project) file path to the redirect template |
29
|
|
|
* |
30
|
|
|
* @var string|bool |
31
|
|
|
*/ |
32
|
|
|
private $redirectTemplate; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var PageView[] |
36
|
|
|
*/ |
37
|
|
|
private $twigExtendsDeps; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ContentItem[][] |
41
|
|
|
*/ |
42
|
|
|
private $collections; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var PageView[] |
46
|
|
|
*/ |
47
|
|
|
private $flatPages; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* PageManager constructor |
51
|
|
|
*/ |
52
|
|
|
public function __construct() |
53
|
|
|
{ |
54
|
|
|
parent::__construct(); |
55
|
|
|
|
56
|
|
|
$this->redirectTemplate = false; |
57
|
|
|
$this->twigExtendsDeps = array(); |
58
|
|
|
$this->collections = array(); |
59
|
|
|
$this->flatPages = array(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Give this manager the collections we'll be using for dynamic PageViews |
64
|
|
|
* |
65
|
|
|
* @param ContentItem[][] $collections |
66
|
|
|
*/ |
67
|
|
|
public function setCollections (&$collections) |
68
|
|
|
{ |
69
|
|
|
$this->collections = &$collections; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getStaticPages () |
73
|
|
|
{ |
74
|
|
|
return $this->flatPages; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function getJailedStaticPages () |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$jailedObjects = array(); |
80
|
|
|
|
81
|
|
|
foreach ($this->flatPages as $key => $value) |
82
|
|
|
{ |
83
|
|
|
// If it's an array, it means the parent is hidden from the site menu therefore its children should be too |
84
|
|
|
if (is_array($value)) |
85
|
|
|
{ |
86
|
|
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$jailedObjects[$key] = $value->createJail(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $jailedObjects; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Go through all of the PageView directories and create a respective PageView for each and classify them as a |
97
|
|
|
* dynamic or static PageView. |
98
|
|
|
* |
99
|
|
|
* @param $pageViewFolders |
100
|
|
|
*/ |
101
|
|
|
public function parsePageViews ($pageViewFolders) |
102
|
|
|
{ |
103
|
|
|
if (empty($pageViewFolders)) { return; } |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* The name of the folder where PageViews are located |
107
|
|
|
* |
108
|
|
|
* @var $pageViewFolder string |
109
|
|
|
*/ |
110
|
|
|
foreach ($pageViewFolders as $pageViewFolderName) |
111
|
|
|
{ |
112
|
|
|
$pageViewFolder = $this->fs->absolutePath($pageViewFolderName); |
113
|
|
|
|
114
|
|
|
if (!$this->fs->exists($pageViewFolder)) |
115
|
|
|
{ |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->scanTrackableItems($pageViewFolder, array( |
120
|
|
|
'fileExplorer' => FileExplorer::INCLUDE_ONLY_FILES |
121
|
|
|
), array('/.html$/', '/.twig$/')); |
122
|
|
|
$this->saveFolderDefinition($pageViewFolderName); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Add a new ContentItem to the respective parent PageView of the ContentItem |
128
|
|
|
* |
129
|
|
|
* @param ContentItem $contentItem |
130
|
|
|
*/ |
131
|
|
|
public function updatePageView ($contentItem) |
132
|
|
|
{ |
133
|
|
|
/** @var DynamicPageView $pageView */ |
134
|
|
|
foreach ($this->trackedItems['dynamic'] as &$pageView) |
135
|
|
|
{ |
136
|
|
|
$fm = $pageView->getFrontMatter(false); |
137
|
|
|
|
138
|
|
|
if ($fm['collection'] == $contentItem->getCollection()) |
139
|
|
|
{ |
140
|
|
|
$pageView->addContentItem($contentItem); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function isTracked($filePath) |
149
|
|
|
{ |
150
|
|
|
return (parent::isTracked($filePath) || isset($this->twigExtendsDeps[$filePath])); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return PageView[] |
155
|
|
|
*/ |
156
|
|
|
public function getPageViews () |
157
|
|
|
{ |
158
|
|
|
return $this->trackedItemsFlattened; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
protected function handleTrackableItem($filePath, $options = array()) |
165
|
|
|
{ |
166
|
|
|
$pageView = PageView::create($filePath); |
167
|
|
|
$namespace = $pageView->getType(); |
168
|
|
|
|
169
|
|
|
switch ($namespace) |
170
|
|
|
{ |
171
|
|
|
case PageView::DYNAMIC_TYPE: |
172
|
|
|
$this->handleTrackableDynamicPageView($pageView); |
|
|
|
|
173
|
|
|
break; |
174
|
|
|
|
175
|
|
|
case PageView::STATIC_TYPE: |
176
|
|
|
$this->handleTrackableStaticPageView($pageView); |
177
|
|
|
break; |
178
|
|
|
|
179
|
|
|
default: |
180
|
|
|
break; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$this->addObjectToTracker($pageView, $pageView->getRelativeFilePath(), $namespace); |
184
|
|
|
$this->saveTrackerOptions($pageView->getRelativeFilePath(), array( |
185
|
|
|
'viewType' => $namespace |
186
|
|
|
)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param DynamicPageView $pageView |
191
|
|
|
*/ |
192
|
|
|
private function handleTrackableDynamicPageView ($pageView) |
193
|
|
|
{ |
194
|
|
|
$frontMatter = $pageView->getFrontMatter(false); |
195
|
|
|
$collection = $frontMatter['collection']; |
196
|
|
|
|
197
|
|
|
if (!isset($this->collections[$collection])) |
198
|
|
|
{ |
199
|
|
|
throw new CollectionNotFoundException("The '$collection' collection is not defined"); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
foreach ($this->collections[$collection] as &$item) |
203
|
|
|
{ |
204
|
|
|
$item->evaluateFrontMatter($frontMatter); |
205
|
|
|
$pageView->addContentItem($item); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param PageView $pageView |
211
|
|
|
*/ |
212
|
|
|
private function handleTrackableStaticPageView ($pageView) |
213
|
|
|
{ |
214
|
|
|
if (empty($pageView['title'])) { return; } |
215
|
|
|
|
216
|
|
|
$this->flatPages[$pageView['title']] = $pageView; |
217
|
|
|
} |
218
|
|
|
} |
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.