|
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\Collection\Page\Type; |
|
14
|
|
|
use Cecil\Collection\Taxonomy\Collection as Collection; |
|
15
|
|
|
use Cecil\Collection\Taxonomy\Term as Term; |
|
16
|
|
|
use Cecil\Collection\Taxonomy\Vocabulary as Vocabulary; |
|
17
|
|
|
use Cecil\Exception\Exception; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Taxonomy. |
|
21
|
|
|
*/ |
|
22
|
|
|
class Taxonomy extends AbstractGenerator implements GeneratorInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/* @var Collection */ |
|
25
|
|
|
protected $taxonomyCollection; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public function generate(): void |
|
31
|
|
|
{ |
|
32
|
|
|
if ($this->config->get('site.taxonomies')) { |
|
33
|
|
|
$this->createCollection(); |
|
34
|
|
|
$this->collectTermsFromPages(); |
|
35
|
|
|
$this->createTaxonomiesPages(); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a collection from the vocabularies configuration. |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function createCollection() |
|
43
|
|
|
{ |
|
44
|
|
|
// create an empty "taxonomies" collection |
|
45
|
|
|
$this->taxonomyCollection = new Collection('taxonomies'); |
|
46
|
|
|
|
|
47
|
|
|
// adds vocabularies collections |
|
48
|
|
|
foreach (array_keys($this->config->get('site.taxonomies')) as $vocabulary) { |
|
49
|
|
|
/* |
|
50
|
|
|
* ie: |
|
51
|
|
|
* taxonomies: |
|
52
|
|
|
* tags: disabled |
|
53
|
|
|
*/ |
|
54
|
|
|
if ($this->config->get("site.taxonomies.$vocabulary") == 'disabled') { |
|
55
|
|
|
continue; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->taxonomyCollection->add(new Vocabulary($vocabulary)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Collects taxonomies's terms from pages frontmatter. |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function collectTermsFromPages() |
|
66
|
|
|
{ |
|
67
|
|
|
/* @var $page Page */ |
|
68
|
|
|
foreach ($this->pagesCollection as $page) { |
|
69
|
|
|
//foreach (array_keys($this->config->get('site.taxonomies')) as $plural) { |
|
70
|
|
|
foreach ($this->taxonomyCollection as $vocabularyCollection) { |
|
71
|
|
|
$plural = $vocabularyCollection->getId(); |
|
72
|
|
|
/* |
|
73
|
|
|
* ie: |
|
74
|
|
|
* tags: Tag 1, Tag 2 |
|
75
|
|
|
*/ |
|
76
|
|
|
if ($page->hasVariable($plural)) { |
|
77
|
|
|
// converts a string list to an array |
|
78
|
|
|
if (!is_array($page->getVariable($plural))) { |
|
79
|
|
|
$page->setVariable($plural, [$page->getVariable($plural)]); |
|
80
|
|
|
} |
|
81
|
|
|
// adds each term to the vocabulary collection... |
|
82
|
|
|
foreach ($page->getVariable($plural) as $term) { |
|
83
|
|
|
$term = mb_strtolower($term); |
|
84
|
|
|
$this->taxonomyCollection |
|
|
|
|
|
|
85
|
|
|
->get($plural) |
|
86
|
|
|
->add(new Term($term)); |
|
87
|
|
|
// ... and adds page to the term collection |
|
88
|
|
|
$this->taxonomyCollection |
|
|
|
|
|
|
89
|
|
|
->get($plural) |
|
90
|
|
|
->get($term) |
|
91
|
|
|
->add($page); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Creates taxonomies pages. |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function createTaxonomiesPages() |
|
102
|
|
|
{ |
|
103
|
|
|
/* @var $vocabulary Vocabulary */ |
|
104
|
|
|
foreach ($this->taxonomyCollection as $position => $vocabulary) { |
|
105
|
|
|
$plural = $vocabulary->getId(); |
|
106
|
|
|
if (count($vocabulary) > 0) { |
|
107
|
|
|
/* |
|
108
|
|
|
* Creates $plural/$term pages (list of pages) |
|
109
|
|
|
* ie: /tags/tag-1/ |
|
110
|
|
|
*/ |
|
111
|
|
|
/* @var $pages PagesCollection */ |
|
112
|
|
|
foreach ($vocabulary as $position => $term) { |
|
113
|
|
|
$pages = $term; |
|
114
|
|
|
$term = $term->getId(); |
|
115
|
|
|
$pages = $pages->sortByDate(); |
|
116
|
|
|
$pageId = $path = Page::slugify(sprintf('%s/%s', $plural, $term)); |
|
117
|
|
|
$page = (new Page($pageId))->setVariable('title', ucfirst($term)); |
|
118
|
|
|
if ($this->pagesCollection->has($pageId)) { |
|
119
|
|
|
$page = clone $this->pagesCollection->get($pageId); |
|
120
|
|
|
} |
|
121
|
|
|
$date = $pages->first()->getVariable('date'); |
|
122
|
|
|
$page->setPath($path) |
|
123
|
|
|
->setType(Type::TAXONOMY) |
|
124
|
|
|
->setVariable('pages', $pages) |
|
125
|
|
|
->setVariable('date', $date) |
|
126
|
|
|
->setVariable('singular', $this->config->get('site.taxonomies')[$plural]) |
|
127
|
|
|
->setVariable('pagination', ['pages' => $pages]); |
|
128
|
|
|
$this->generatedPages->add($page); |
|
129
|
|
|
} |
|
130
|
|
|
/* |
|
131
|
|
|
* Creates $plural pages (list of terms) |
|
132
|
|
|
* ex: /tags/ |
|
133
|
|
|
*/ |
|
134
|
|
|
$page = (new Page(Page::slugify($plural))) |
|
135
|
|
|
->setPath(strtolower($plural)) |
|
136
|
|
|
->setVariable('title', $plural) |
|
137
|
|
|
->setType(Type::TERMS) |
|
138
|
|
|
->setVariable('plural', $plural) |
|
139
|
|
|
->setVariable('singular', $this->config->get('site.taxonomies')[$plural]) |
|
140
|
|
|
->setVariable('terms', $vocabulary) |
|
141
|
|
|
->setVariable('date', $date); |
|
|
|
|
|
|
142
|
|
|
// add page only if a template exist |
|
143
|
|
|
try { |
|
144
|
|
|
$this->generatedPages->add($page); |
|
145
|
|
|
} catch (Exception $e) { |
|
146
|
|
|
printf("%s\n", $e->getMessage()); |
|
147
|
|
|
// do not add page |
|
148
|
|
|
unset($page); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
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 implementation 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 interface: