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
|
|
|
$sections = []; |
28
|
|
|
|
29
|
|
|
// identify sections |
30
|
|
|
/* @var $page Page */ |
31
|
|
|
foreach ($pagesCollection as $page) { |
32
|
|
|
if ($page->getSection()) { |
33
|
|
|
// ie: |
34
|
|
|
// [blog][0] = blog/post-1 |
35
|
|
|
// [blog][1] = blog/post-2 |
36
|
|
|
$sectionsList[$page->getSection()][] = $page->getId(); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// sections collections |
41
|
|
|
// ie: |
42
|
|
|
// [blog] = Collection(blog) |
43
|
|
|
foreach ($sectionsList as $sectionName => $pagesList) { |
44
|
|
|
$sections[$sectionName] = new PagesCollection($sectionName); |
45
|
|
|
foreach ($pagesList as $pageId) { |
46
|
|
|
$sections[$sectionName]->add($pagesCollection->get($pageId)); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// adds section to pages collection |
51
|
|
|
if (count($sections) > 0) { |
52
|
|
|
$menuWeight = 100; |
53
|
|
|
foreach ($sections as $section => $pages) { |
54
|
|
|
$pageId = $pathname = Page::slugify($section); |
55
|
|
|
if (!$pagesCollection->has($pageId)) { |
56
|
|
|
//usort($pages, 'Cecil\Util::sortByDate'); |
57
|
|
|
$pages = $pages->sortByDate(); |
58
|
|
|
$page = (new Page()) |
|
|
|
|
59
|
|
|
->setId($pageId) |
60
|
|
|
->setPathname($pathname) |
61
|
|
|
->setType(Type::SECTION) |
62
|
|
|
->setVariable('title', ucfirst($section)) |
63
|
|
|
->setVariable('pages', $pages) |
64
|
|
|
->setVariable('date', $pages->getIterator()->current()->getVariable('date')) |
65
|
|
|
->setVariable('menu', [ |
66
|
|
|
'main' => ['weight' => $menuWeight], |
67
|
|
|
]); |
68
|
|
|
$generatedPages->add($page); |
69
|
|
|
} |
70
|
|
|
$menuWeight += 10; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $generatedPages; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.