1 | <?php |
||
32 | class PageManager extends TrackingManager |
||
33 | { |
||
34 | /** @var StaticPageView[] A place to store a reference to static PageViews with titles. */ |
||
35 | private $staticPages; |
||
36 | private $configuration; |
||
37 | private $collectionManager; |
||
38 | private $dataManager; |
||
39 | private $eventDispatcher; |
||
40 | private $logger; |
||
41 | |||
42 | /** |
||
43 | * PageManager constructor. |
||
44 | */ |
||
45 | 15 | public function __construct(Configuration $configuration, CollectionManager $collectionManager, DataManager $dataManager, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) |
|
46 | { |
||
47 | 15 | $this->trackedItems = [ |
|
48 | 15 | BasePageView::STATIC_TYPE => [], |
|
49 | 15 | BasePageView::DYNAMIC_TYPE => [], |
|
50 | 15 | BasePageView::REPEATER_TYPE => [], |
|
51 | ]; |
||
52 | 15 | $this->staticPages = []; |
|
53 | 15 | $this->configuration = $configuration; |
|
54 | 15 | $this->collectionManager = $collectionManager; |
|
55 | 15 | $this->dataManager = $dataManager; |
|
56 | 15 | $this->eventDispatcher = $eventDispatcher; |
|
57 | 15 | $this->logger = $logger; |
|
58 | 15 | } |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 3 | public function compileManager() |
|
67 | |||
68 | /** |
||
69 | * Go through all of the PageView directories and create a respective PageView for each and classify them by type. |
||
70 | * |
||
71 | * @param string[] $pageViewFolders |
||
72 | * |
||
73 | * @since 0.1.0 |
||
74 | */ |
||
75 | 15 | public function parsePageViews(array $pageViewFolders) |
|
92 | |||
93 | /** |
||
94 | * Get all of the PageViews in an associative array with PageView types as the keys. |
||
95 | * |
||
96 | * @since 0.1.1 |
||
97 | * |
||
98 | * @return BasePageView[][] |
||
99 | */ |
||
100 | public function &getPageViews() |
||
104 | |||
105 | /** |
||
106 | * Get all of the PageViews in flat array. |
||
107 | * |
||
108 | * @since 0.1.1 |
||
109 | * |
||
110 | * @return BasePageView[] |
||
111 | */ |
||
112 | 14 | public function &getPageViewsFlattened() |
|
116 | |||
117 | /** |
||
118 | * Get the static PageViews tracked by this manager indexed by their title. |
||
119 | * |
||
120 | * @since 0.1.0 |
||
121 | * |
||
122 | * @return StaticPageView[] |
||
123 | */ |
||
124 | 1 | public function getStaticPageViews() |
|
128 | |||
129 | /** |
||
130 | * Get the jailed version of the static PageViews indexed by their title. |
||
131 | * |
||
132 | * @since 0.1.0 |
||
133 | * |
||
134 | * @return JailedDocument[] |
||
135 | */ |
||
136 | 13 | public function getJailedStaticPageViews() |
|
147 | |||
148 | /** |
||
149 | * Add a new ContentItem to the respective parent PageView of the ContentItem. |
||
150 | * |
||
151 | * @param ContentItem $contentItem |
||
152 | * |
||
153 | * @since 0.1.0 |
||
154 | */ |
||
155 | public function trackNewContentItem(&$contentItem) |
||
156 | { |
||
157 | $collection = $contentItem->getNamespace(); |
||
158 | $this->trackedItems[BasePageView::DYNAMIC_TYPE][$collection]->addCollectableItem($contentItem); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | 14 | protected function &handleTrackableItem(File $filePath, array $options = array()) |
|
165 | { |
||
166 | 14 | $pageView = BasePageView::create($filePath, [ |
|
167 | 14 | 'site' => $this->configuration->getConfiguration(), |
|
168 | ]); |
||
169 | 14 | $namespace = $pageView->getType(); |
|
170 | |||
171 | switch ($namespace) |
||
172 | { |
||
173 | 14 | case BasePageView::STATIC_TYPE: |
|
174 | 11 | $this->handleTrackableStaticPageView($pageView); |
|
|
|||
175 | 11 | break; |
|
176 | |||
177 | 3 | case BasePageView::DYNAMIC_TYPE: |
|
178 | $this->handleTrackableDynamicPageView($pageView); |
||
179 | break; |
||
180 | |||
181 | 3 | case BasePageView::REPEATER_TYPE: |
|
182 | 3 | $this->handleTrackableRepeaterPageView($pageView); |
|
183 | 3 | break; |
|
184 | |||
185 | default: |
||
186 | break; |
||
187 | } |
||
188 | |||
189 | 14 | $this->addObjectToTracker($pageView, $namespace); |
|
190 | |||
191 | 14 | return $pageView; |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * Handle special behavior and treatment for static PageViews while we're iterating through them. |
||
196 | * |
||
197 | * @param StaticPageView $pageView |
||
198 | * |
||
199 | * @since 0.1.0 |
||
200 | */ |
||
201 | 11 | private function handleTrackableStaticPageView(&$pageView) |
|
214 | |||
215 | /** |
||
216 | * Handle special behavior and treatment for dynamic PageViews while we're iterating through them. |
||
217 | * |
||
218 | * @param DynamicPageView $pageView |
||
219 | * |
||
220 | * @since 0.1.0 |
||
221 | * |
||
222 | * @throws \Exception |
||
223 | */ |
||
224 | private function handleTrackableDynamicPageView(&$pageView) |
||
225 | { |
||
226 | $frontMatter = $pageView->getRawFrontMatter(); |
||
227 | $dataSource = null; |
||
228 | $namespace = null; |
||
229 | |||
230 | if (isset($frontMatter['collection'])) |
||
231 | { |
||
232 | $dataSource = &$this->collectionManager->getCollections(); |
||
233 | $namespace = 'collection'; |
||
234 | } |
||
235 | elseif (isset($frontMatter['dataset'])) |
||
236 | { |
||
237 | $dataSource = &$this->dataManager->getDataItems(); |
||
238 | $namespace = 'dataset'; |
||
239 | } |
||
240 | |||
241 | if ($dataSource === null) |
||
242 | { |
||
243 | throw new \Exception('Invalid Dynamic PageView defined'); |
||
244 | } |
||
245 | |||
246 | $collection = $frontMatter[$namespace]; |
||
247 | |||
248 | if (!isset($dataSource[$collection])) |
||
249 | { |
||
250 | throw new CollectionNotFoundException("The '$collection' $namespace is not defined"); |
||
251 | } |
||
252 | |||
253 | /** @var ContentItem|DataItem $item */ |
||
254 | foreach ($dataSource[$collection] as &$item) |
||
255 | { |
||
256 | $item->evaluateFrontMatter($frontMatter, [ |
||
257 | 'site' => $this->configuration->getConfiguration(), |
||
258 | ]); |
||
259 | $item->setParentPageView($pageView); |
||
260 | $item->buildPermalink(true); |
||
261 | |||
262 | $pageView->addCollectableItem($item); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Handle special behavior and treatment for repeater PageViews while we're iterating through them. |
||
268 | * |
||
269 | * @param RepeaterPageView $pageView |
||
270 | * |
||
271 | * @since 0.2.0 |
||
272 | */ |
||
273 | 3 | private function handleTrackableRepeaterPageView(&$pageView) |
|
280 | } |
||
281 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.