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