1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil\Generator; |
10
|
|
|
|
11
|
|
|
use Cecil\Collection\Page\Collection as PagesCollection; |
12
|
|
|
use Cecil\Collection\Page\Page; |
13
|
|
|
use Cecil\Page\Type; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Section. |
17
|
|
|
*/ |
18
|
|
|
class Section extends AbstractGenerator implements GeneratorInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function generate(PagesCollection $pagesCollection, \Closure $messageCallback) |
24
|
|
|
{ |
25
|
|
|
$generatedPages = new PagesCollection('sections'); |
|
|
|
|
26
|
|
|
$sectionsList = []; |
27
|
|
|
|
28
|
|
|
// identify sections |
29
|
|
|
/* @var $page Page */ |
30
|
|
|
foreach ($pagesCollection as $page) { |
31
|
|
|
if ($page->getSection()) { |
32
|
|
|
// ie: ['blog'][0]['blog/post-1'] |
33
|
|
|
$sectionsList[$page->getSection()][] = $page->getId(); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// sections collections |
38
|
|
|
$sections = []; |
39
|
|
|
foreach ($sectionsList as $sectionName => $pagesList) { |
40
|
|
|
if (!array_key_exists($sectionName, $sections)) { |
41
|
|
|
$sections[$sectionName] = new PagesCollection($sectionName); |
42
|
|
|
foreach ($pagesList as $pageId) { |
43
|
|
|
$sections[$sectionName]->add($pagesCollection->get($pageId)); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// DEBUG |
49
|
|
|
//print_r($sections2); |
50
|
|
|
//print_r($section3); |
51
|
|
|
foreach ($section3 as $section => $collection) { |
|
|
|
|
52
|
|
|
//echo $section."\n"; |
53
|
|
|
$collection->sortByDate(); |
54
|
|
|
//echo $collection->getId()."\n"; |
55
|
|
|
//echo $collection; |
56
|
|
|
print_r($collection); |
57
|
|
|
} |
58
|
|
|
die(); |
59
|
|
|
|
60
|
|
|
// adds section pages to collection |
61
|
|
|
if (count($sections) > 0) { |
|
|
|
|
62
|
|
|
$menuWeight = 100; |
63
|
|
|
foreach ($sections as $section => $pages) { |
64
|
|
|
$pageId = Page::slugify($section); |
65
|
|
|
if (!$pagesCollection->has($pageId)) { |
66
|
|
|
usort($pages, 'Cecil\Util::sortByDate'); |
67
|
|
|
$page = (new Page()) |
|
|
|
|
68
|
|
|
->setId($pageId) |
69
|
|
|
->setPathname($pageId) |
70
|
|
|
->setVariable('title', ucfirst($section)) |
71
|
|
|
->setType(Type::SECTION) |
72
|
|
|
->setVariable('pages', $pages) |
73
|
|
|
->setVariable('date', reset($pages)->getVariable('date')) |
74
|
|
|
->setVariable('menu', [ |
75
|
|
|
'main' => ['weight' => $menuWeight], |
76
|
|
|
]); |
77
|
|
|
$generatedPages->add($page); |
78
|
|
|
} |
79
|
|
|
$menuWeight += 10; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $generatedPages; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.