Complex classes like PageManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PageManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class PageManager extends TrackingManager |
||
28 | { |
||
29 | /** |
||
30 | * The relative (to the stakx project) file path to the redirect template |
||
31 | * |
||
32 | * @var string|bool |
||
33 | */ |
||
34 | private $redirectTemplate; |
||
35 | |||
36 | /** |
||
37 | * @var PageView[] |
||
38 | */ |
||
39 | private $twigExtendsDeps; |
||
40 | |||
41 | /** |
||
42 | * @var ContentItem[][] |
||
43 | */ |
||
44 | private $collections; |
||
45 | |||
46 | /** |
||
47 | * @var Folder |
||
48 | */ |
||
49 | private $targetDir; |
||
50 | |||
51 | /** |
||
52 | * @var PageView[] |
||
53 | */ |
||
54 | private $flatPages; |
||
55 | |||
56 | /** |
||
57 | * @var PageView[] |
||
58 | */ |
||
59 | private $siteMenu; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | private $twigOpts; |
||
65 | |||
66 | /** |
||
67 | * @var \Twig_Environment |
||
68 | */ |
||
69 | private $twig; |
||
70 | |||
71 | /** |
||
72 | * PageManager constructor |
||
73 | */ |
||
74 | 3 | public function __construct() |
|
75 | { |
||
76 | 3 | parent::__construct(); |
|
77 | |||
78 | 3 | $this->redirectTemplate = false; |
|
79 | 3 | $this->twigExtendsDeps = array(); |
|
80 | 3 | $this->collections = array(); |
|
81 | 3 | $this->flatPages = array(); |
|
82 | 3 | $this->siteMenu = array(); |
|
83 | 3 | } |
|
84 | |||
85 | /** |
||
86 | * Give this manager the collections we'll be using for dynamic PageViews |
||
87 | * |
||
88 | * @param ContentItem[][] $collections |
||
89 | */ |
||
90 | 3 | public function setCollections (&$collections) |
|
94 | |||
95 | /** |
||
96 | * Set the template used for redirects |
||
97 | * |
||
98 | * @param false|string $filePath The path to the redirect template |
||
99 | */ |
||
100 | public function setRedirectTemplate ($filePath) |
||
101 | { |
||
102 | $this->redirectTemplate = $filePath; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * The location where the compiled website will be written to |
||
107 | * |
||
108 | * @param Folder $folder The relative target directory as specified from the configuration file |
||
109 | */ |
||
110 | 3 | public function setTargetFolder (&$folder) |
|
114 | |||
115 | 3 | public function configureTwig ($configuration, $options) |
|
122 | |||
123 | 1 | public function getFlatPages () |
|
127 | |||
128 | /** |
||
129 | * An array representing the website's menu structure with children and grandchildren made from static PageViews |
||
130 | * |
||
131 | * @return JailObject[] |
||
132 | */ |
||
133 | 1 | public function getSiteMenu () |
|
134 | { |
||
135 | 1 | $jailedMenu = array(); |
|
136 | |||
137 | 1 | foreach ($this->siteMenu as $key => $value) |
|
138 | { |
||
139 | // If it's an array, it means the parent is hidden from the site menu therefore its children should be too |
||
140 | 1 | if (is_array($this->siteMenu[$key])) |
|
141 | { |
||
142 | continue; |
||
143 | } |
||
144 | |||
145 | 1 | $jailedMenu[$key] = $value->createJail(); |
|
146 | } |
||
147 | |||
148 | 1 | return $jailedMenu; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Go through all of the PageView directories and create a respective PageView for each and classify them as a |
||
153 | * dynamic or static PageView. |
||
154 | * |
||
155 | * @param $pageViewFolders |
||
156 | */ |
||
157 | 3 | public function parsePageViews ($pageViewFolders) |
|
183 | |||
184 | /** |
||
185 | * Compile dynamic and static PageViews |
||
186 | */ |
||
187 | 3 | public function compileAll () |
|
188 | { |
||
189 | 3 | foreach (array_keys($this->trackedItemsFlattened) as $filePath) |
|
190 | { |
||
191 | 3 | $this->compileFromFilePath($filePath); |
|
192 | } |
||
193 | 3 | } |
|
194 | |||
195 | public function compileSome ($filter = array()) |
||
206 | |||
207 | /** |
||
208 | * @param ContentItem $contentItem |
||
209 | */ |
||
210 | public function compileContentItem (&$contentItem) |
||
231 | |||
232 | /** |
||
233 | * Add a new ContentItem to the respective parent PageView of the ContentItem |
||
234 | * |
||
235 | * @param ContentItem $contentItem |
||
236 | */ |
||
237 | public function updatePageView ($contentItem) |
||
250 | |||
251 | /** |
||
252 | * Update an existing Twig variable that's injected globally |
||
253 | * |
||
254 | * @param string $variable |
||
255 | * @param string $value |
||
256 | */ |
||
257 | public function updateTwigVariable ($variable, $value) |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | 3 | public function isTracked($filePath) |
|
266 | { |
||
267 | 3 | return (parent::isTracked($filePath) || isset($this->twigExtendsDeps[$filePath])); |
|
268 | } |
||
269 | |||
270 | /** |
||
271 | * {@inheritdoc} |
||
272 | */ |
||
273 | public function refreshItem($filePath) |
||
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | */ |
||
293 | 3 | protected function handleTrackableItem($filePath, $options = array()) |
|
294 | { |
||
295 | 3 | $pageView = PageView::create($filePath); |
|
296 | 3 | $namespace = $pageView->getType(); |
|
297 | |||
298 | 3 | if ($namespace == PageView::DYNAMIC_TYPE) |
|
299 | { |
||
300 | 3 | $frontMatter = $pageView->getFrontMatter(false); |
|
301 | 3 | $collection = $frontMatter['collection']; |
|
302 | |||
303 | 3 | if (!isset($this->collections[$collection])) |
|
304 | { |
||
305 | throw new \RuntimeException("The '$collection' collection is not defined"); |
||
306 | } |
||
307 | |||
308 | 3 | foreach ($this->collections[$collection] as &$item) |
|
309 | { |
||
310 | 3 | $item->evaluateFrontMatter($frontMatter); |
|
311 | 3 | $pageView->addContentItem($item); |
|
312 | } |
||
313 | } |
||
314 | |||
315 | 3 | $this->addObjectToTracker($pageView, $pageView->getRelativeFilePath(), $namespace); |
|
316 | 3 | $this->saveTrackerOptions($pageView->getRelativeFilePath(), array( |
|
317 | 3 | 'viewType' => $namespace |
|
318 | )); |
||
319 | |||
320 | 3 | if ($namespace == PageView::STATIC_TYPE && !empty($pageView['title'])) |
|
321 | { |
||
322 | 3 | $this->addToSiteMenu($pageView); |
|
323 | 3 | $this->flatPages[$pageView['title']] = $pageView->createJail(); |
|
324 | } |
||
325 | 3 | } |
|
326 | |||
327 | /** |
||
328 | * Create a Twig environment |
||
329 | */ |
||
330 | 3 | private function createTwigManager () |
|
331 | { |
||
332 | 3 | $twig = new TwigManager(); |
|
333 | 3 | $twig->configureTwig( |
|
334 | 3 | $this->twigOpts['configuration'], |
|
335 | 3 | $this->twigOpts['options'] |
|
336 | ); |
||
337 | |||
338 | 3 | $this->twig = TwigManager::getInstance(); |
|
339 | 3 | } |
|
340 | |||
341 | /** |
||
342 | * Compile a given PageView |
||
343 | * |
||
344 | * @param string $filePath The file path to the PageView to compile |
||
345 | * @param bool $refresh When set to true, the PageView will reread its contents |
||
346 | * |
||
347 | * @throws \Exception |
||
348 | */ |
||
349 | 3 | private function compileFromFilePath ($filePath, $refresh = false) |
|
350 | { |
||
351 | 3 | if (!$this->isTracked($filePath)) |
|
352 | { |
||
353 | throw new TrackedItemNotFoundException('PageView not found'); |
||
354 | } |
||
355 | |||
356 | /** @var DynamicPageView|PageView|RepeaterPageView $pageView */ |
||
357 | 3 | $pageView = &$this->trackedItemsFlattened[$filePath]; |
|
358 | |||
359 | try |
||
360 | { |
||
361 | 3 | $pageView->refreshFileContent(); |
|
362 | 3 | $this->compilePageView($pageView); |
|
363 | } |
||
364 | catch (\Exception $e) |
||
365 | { |
||
366 | throw FileAwareException::castException($e, $filePath); |
||
367 | } |
||
368 | 3 | } |
|
369 | |||
370 | /** |
||
371 | * @param DynamicPageView|RepeaterPageView|PageView $pageView |
||
372 | */ |
||
373 | 3 | private function compilePageView ($pageView) |
|
374 | { |
||
375 | 3 | switch ($pageView->getType()) |
|
376 | { |
||
377 | 3 | case PageView::REPEATER_TYPE: |
|
378 | 3 | $this->compileRepeaterPageView($pageView); |
|
379 | 3 | $this->compileExpandedRedirects($pageView); |
|
380 | 3 | break; |
|
381 | |||
382 | 3 | case PageView::DYNAMIC_TYPE: |
|
383 | 3 | $this->compileDynamicPageView($pageView); |
|
384 | 3 | $this->compileNormalRedirects($pageView); |
|
385 | 3 | break; |
|
386 | |||
387 | 3 | case PageView::STATIC_TYPE: |
|
388 | 3 | $this->compileStaticPageView($pageView); |
|
389 | 3 | $this->compileNormalRedirects($pageView); |
|
390 | 3 | break; |
|
391 | } |
||
392 | 3 | } |
|
393 | |||
394 | /** |
||
395 | * @param RepeaterPageView $pageView |
||
396 | */ |
||
397 | 3 | private function compileRepeaterPageView (&$pageView) |
|
398 | { |
||
399 | 3 | $template = $this->createTemplate($pageView); |
|
400 | 3 | $pageView->rewindPermalink(); |
|
401 | |||
402 | 3 | foreach ($pageView->getRepeaterPermalinks() as $permalink) |
|
403 | { |
||
404 | 3 | $pageView->bumpPermalink(); |
|
405 | 3 | $pageView->setFrontMatter(array( |
|
406 | 3 | 'permalink' => $permalink->getEvaluated(), |
|
407 | 3 | 'iterators' => $permalink->getIterators() |
|
408 | )); |
||
409 | |||
410 | 3 | $output = $template->render(array( |
|
411 | 3 | 'this' => $pageView->createJail() |
|
412 | )); |
||
413 | |||
414 | 3 | $this->output->notice("Writing repeater file: {file}", array('file' => $pageView->getTargetFile())); |
|
415 | 3 | $this->targetDir->writeFile($pageView->getTargetFile(), $output); |
|
416 | } |
||
417 | 3 | } |
|
418 | |||
419 | /** |
||
420 | * @param PageView $pageView |
||
421 | */ |
||
422 | 3 | private function compileDynamicPageView (&$pageView) |
|
423 | { |
||
424 | 3 | $template = $this->createTemplate($pageView); |
|
425 | |||
426 | 3 | $pageViewFrontMatter = $pageView->getFrontMatter(false); |
|
427 | 3 | $collection = $pageViewFrontMatter['collection']; |
|
428 | |||
429 | 3 | if (!isset($this->collections[$collection])) |
|
430 | { |
||
431 | throw new \RuntimeException("The '$collection' collection is not defined"); |
||
432 | } |
||
433 | |||
434 | /** @var ContentItem $contentItem */ |
||
435 | 3 | foreach ($this->collections[$collection] as &$contentItem) |
|
436 | { |
||
437 | 3 | $output = $template->render(array( |
|
438 | 3 | 'this' => $contentItem->createJail() |
|
439 | )); |
||
440 | |||
441 | 3 | $this->output->notice("Writing file: {file}", array('file' => $contentItem->getTargetFile())); |
|
442 | 3 | $this->targetDir->writeFile($contentItem->getTargetFile(), $output); |
|
443 | } |
||
444 | 3 | } |
|
445 | |||
446 | /** |
||
447 | * @param PageView $pageView |
||
448 | */ |
||
449 | 3 | private function compileStaticPageView (&$pageView) |
|
450 | { |
||
451 | 3 | $this->twig->addGlobal('__currentTemplate', $pageView->getFilePath()); |
|
452 | |||
453 | 3 | $template = $this->createTemplate($pageView); |
|
454 | 3 | $output = $template->render(array( |
|
455 | 3 | 'this' => $pageView->createJail() |
|
456 | )); |
||
457 | |||
458 | 3 | $this->output->notice("Writing file: {file}", array('file' => $pageView->getTargetFile())); |
|
459 | 3 | $this->targetDir->writeFile($pageView->getTargetFile(), $output); |
|
460 | 3 | } |
|
461 | |||
462 | /** |
||
463 | * @param DynamicPageView|PageView $pageView |
||
464 | */ |
||
465 | 3 | private function compileNormalRedirects (&$pageView) |
|
466 | { |
||
467 | 3 | foreach ($pageView->getRedirects() as $redirect) |
|
468 | { |
||
469 | $redirectPageView = PageView::createRedirect( |
||
470 | $redirect, |
||
471 | $pageView->getPermalink(), |
||
472 | $this->redirectTemplate |
||
473 | ); |
||
474 | |||
475 | $this->compilePageView($redirectPageView); |
||
476 | } |
||
477 | 3 | } |
|
478 | |||
479 | /** |
||
480 | * @param RepeaterPageView $pageView |
||
481 | */ |
||
482 | 3 | private function compileExpandedRedirects (&$pageView) |
|
483 | { |
||
484 | 3 | $permalinks = $pageView->getRepeaterPermalinks(); |
|
485 | |||
486 | /** @var ExpandedValue[] $repeaterRedirect */ |
||
487 | 3 | foreach ($pageView->getRepeaterRedirects() as $repeaterRedirect) |
|
488 | { |
||
489 | /** |
||
490 | * @var int $index |
||
491 | * @var ExpandedValue $redirect |
||
492 | */ |
||
493 | foreach ($repeaterRedirect as $index => $redirect) |
||
494 | { |
||
495 | $redirectPageView = PageView::createRedirect( |
||
496 | $redirect->getEvaluated(), |
||
497 | $permalinks[$index]->getEvaluated(), |
||
498 | $this->redirectTemplate |
||
499 | ); |
||
500 | |||
501 | $this->compilePageView($redirectPageView); |
||
502 | } |
||
503 | } |
||
504 | 3 | } |
|
505 | |||
506 | /** |
||
507 | * Add a static PageView to the menu array. Dynamic PageViews are not added to the menu |
||
508 | * |
||
509 | * @param PageView $pageView |
||
510 | */ |
||
511 | 3 | private function addToSiteMenu (&$pageView) |
|
512 | { |
||
513 | 3 | $frontMatter = $pageView->getFrontMatter(); |
|
514 | |||
515 | 3 | if (isset($frontMatter['menu']) && !$frontMatter['menu']) |
|
516 | { |
||
517 | return; |
||
518 | } |
||
519 | |||
520 | 3 | $url = trim($pageView->getPermalink(), '/'); |
|
521 | |||
522 | 3 | if (empty($url)) |
|
523 | { |
||
524 | return; |
||
525 | } |
||
526 | |||
527 | 3 | $root = &$this->siteMenu; |
|
528 | 3 | $dirs = explode('/', $url); |
|
529 | |||
530 | 3 | while (count($dirs) > 0) |
|
531 | { |
||
532 | 3 | $name = array_shift($dirs); |
|
533 | 3 | $name = (!empty($name)) ? $name : '.'; |
|
534 | |||
535 | 3 | if (!is_null($name) && count($dirs) == 0) |
|
536 | { |
||
537 | 3 | if (isset($root[$name]) && is_array($root[$name])) |
|
538 | { |
||
539 | 3 | $children = &$pageView->getChildren(); |
|
540 | 3 | $children = $root[$name]['children']; |
|
541 | } |
||
542 | |||
543 | 3 | $root[$name] = &$pageView; |
|
544 | } |
||
545 | else |
||
546 | { |
||
547 | 3 | if (!isset($root[$name]['children'])) |
|
548 | { |
||
549 | 3 | $root[$name]['children'] = array(); |
|
550 | } |
||
551 | |||
552 | 3 | $root = &$root[$name]['children']; |
|
553 | } |
||
554 | } |
||
555 | 3 | } |
|
556 | |||
557 | /** |
||
558 | * @param PageView $pageView |
||
559 | * |
||
560 | * @return Twig_Template |
||
561 | * @throws Twig_Error_Syntax |
||
562 | */ |
||
563 | 3 | private function createTemplate (&$pageView) |
|
581 | |||
582 | /** |
||
583 | * Find the parent Twig templates of the given template and keep a list of it |
||
584 | * |
||
585 | * @param Twig_Template $template The template created from the PageView's content |
||
586 | * @param PageView $pageView The PageView that has this content. Used to keep a reference of PageViews |
||
587 | */ |
||
588 | 3 | private function trackParentTwigTemplate ($template, &$pageView) |
|
589 | { |
||
590 | 3 | if (!$this->tracking) { return; } |
|
591 | |||
592 | /** @var Twig_Template $parent */ |
||
593 | $parent = $template->getParent(array()); |
||
594 | |||
603 | } |