|
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\Collection as PageCollection; |
|
12
|
|
|
use Cecil\Collection\Page\Page; |
|
13
|
|
|
use Cecil\Collection\Taxonomy\Collection as TaxonomyCollection; |
|
14
|
|
|
use Cecil\Collection\Taxonomy\Term as Term; |
|
15
|
|
|
use Cecil\Collection\Taxonomy\Vocabulary as Vocabulary; |
|
16
|
|
|
use Cecil\Exception\Exception; |
|
17
|
|
|
use Cecil\Page\NodeType; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Taxonomy. |
|
21
|
|
|
*/ |
|
22
|
|
|
class Taxonomy extends AbstractGenerator implements GeneratorInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/* @var TaxonomyCollection */ |
|
25
|
|
|
protected $taxonomies; |
|
26
|
|
|
/* @var PageCollection */ |
|
27
|
|
|
protected $pageCollection; |
|
28
|
|
|
/* @var PageCollection */ |
|
29
|
|
|
protected $generatedPages; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
public function generate(PageCollection $pageCollection, \Closure $messageCallback) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->pageCollection = $pageCollection; |
|
37
|
|
|
$this->generatedPages = new PageCollection(); |
|
38
|
|
|
|
|
39
|
|
|
if ($this->config->get('site.taxonomies')) { |
|
40
|
|
|
// is taxonomies disabled |
|
41
|
|
|
if ($this->config->get('site.taxonomies.disabled')) { |
|
42
|
|
|
return $this->generatedPages; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// prepares taxonomies collection |
|
46
|
|
|
$this->taxonomies = new TaxonomyCollection('taxonomies'); |
|
47
|
|
|
// adds each vocabulary collection to the taxonomies collection |
|
48
|
|
|
foreach ($this->config->get('site.taxonomies') as $vocabulary) { |
|
49
|
|
|
if ($vocabulary != 'disable') { |
|
50
|
|
|
$this->taxonomies->add(new Vocabulary($vocabulary)); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// collects taxonomies from pages |
|
55
|
|
|
$this->collectTaxonomiesFromPages(); |
|
56
|
|
|
|
|
57
|
|
|
// creates node pages |
|
58
|
|
|
$this->createNodePages(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->generatedPages; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Collects taxonomies from pages. |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function collectTaxonomiesFromPages() |
|
68
|
|
|
{ |
|
69
|
|
|
/* @var $page Page */ |
|
70
|
|
|
foreach ($this->pageCollection as $page) { |
|
71
|
|
|
foreach (array_keys($this->config->get('site.taxonomies')) as $plural) { |
|
72
|
|
|
if (isset($page[$plural])) { |
|
73
|
|
|
// converts a list to an array if necessary |
|
74
|
|
|
if (!is_array($page[$plural])) { |
|
75
|
|
|
$page->setVariable($plural, [$page[$plural]]); |
|
76
|
|
|
} |
|
77
|
|
|
foreach ($page[$plural] as $term) { |
|
78
|
|
|
// adds each terms to the vocabulary collection |
|
79
|
|
|
$this->taxonomies->get($plural) |
|
80
|
|
|
->add(new Term($term)); |
|
81
|
|
|
// adds each pages to the term collection |
|
82
|
|
|
$this->taxonomies |
|
83
|
|
|
->get($plural) |
|
84
|
|
|
->get($term) |
|
85
|
|
|
->add($page); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Creates node pages. |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function createNodePages() |
|
96
|
|
|
{ |
|
97
|
|
|
/* @var $terms Vocabulary */ |
|
98
|
|
|
foreach ($this->taxonomies as $plural => $terms) { |
|
99
|
|
|
if (count($terms) > 0) { |
|
100
|
|
|
/* |
|
101
|
|
|
* Creates $plural/$term pages (list of pages) |
|
102
|
|
|
* ex: /tags/tag-1/ |
|
103
|
|
|
*/ |
|
104
|
|
|
/* @var $pages PageCollection */ |
|
105
|
|
|
foreach ($terms as $term => $pages) { |
|
106
|
|
|
if (!$this->pageCollection->has($term)) { |
|
107
|
|
|
$pages = $pages->sortByDate()->toArray(); |
|
108
|
|
|
$page = (new Page()) |
|
|
|
|
|
|
109
|
|
|
->setId(Page::urlize(sprintf('%s/%s/', $plural, $term))) |
|
110
|
|
|
->setPathname(Page::urlize(sprintf('%s/%s', $plural, $term))) |
|
111
|
|
|
->setTitle(ucfirst($term)) |
|
112
|
|
|
->setNodeType(NodeType::TAXONOMY) |
|
113
|
|
|
->setVariable('pages', $pages) |
|
114
|
|
|
->setVariable('date', $date = reset($pages)->getDate()) |
|
115
|
|
|
->setVariable('singular', $this->config->get('site.taxonomies')[$plural]) |
|
116
|
|
|
->setVariable('pagination', ['pages' => $pages]); |
|
117
|
|
|
$this->generatedPages->add($page); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
/* |
|
121
|
|
|
* Creates $plural pages (list of terms) |
|
122
|
|
|
* ex: /tags/ |
|
123
|
|
|
*/ |
|
124
|
|
|
$page = (new Page()) |
|
|
|
|
|
|
125
|
|
|
->setId(Page::urlize($plural)) |
|
126
|
|
|
->setPathname(strtolower($plural)) |
|
127
|
|
|
->setTitle($plural) |
|
128
|
|
|
->setNodeType(NodeType::TERMS) |
|
129
|
|
|
->setVariable('plural', $plural) |
|
130
|
|
|
->setVariable('singular', $this->config->get('site.taxonomies')[$plural]) |
|
131
|
|
|
->setVariable('terms', $terms) |
|
132
|
|
|
->setVariable('date', $date); |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
// add page only if a template exist |
|
135
|
|
|
try { |
|
136
|
|
|
$this->generatedPages->add($page); |
|
137
|
|
|
} catch (Exception $e) { |
|
138
|
|
|
echo $e->getMessage()."\n"; |
|
139
|
|
|
// do not add page |
|
140
|
|
|
unset($page); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: