|
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
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function generate(): void |
|
28
|
|
|
{ |
|
29
|
|
|
if ($this->config->get('site.taxonomies')) { |
|
30
|
|
|
/* @var $vocabulary Vocabulary */ |
|
31
|
|
|
foreach ($this->builder->getTaxonomies() as $position => $vocabulary) { |
|
32
|
|
|
$plural = $vocabulary->getId(); |
|
33
|
|
|
$singular = $this->config->get("site.taxonomies.$plural"); |
|
34
|
|
|
if (count($vocabulary) > 0) { |
|
35
|
|
|
/* |
|
36
|
|
|
* Creates $plural/$term pages (list of pages) |
|
37
|
|
|
* ie: /tags/tag-1/ |
|
38
|
|
|
*/ |
|
39
|
|
|
/* @var $pages PagesCollection */ |
|
40
|
|
|
foreach ($vocabulary as $position => $term) { |
|
41
|
|
|
$pageId = $path = Page::slugify(sprintf('%s/%s', $plural, $term->getId())); |
|
42
|
|
|
$pages = $term->sortByDate(); |
|
43
|
|
|
$date = $pages->first()->getVariable('date'); |
|
44
|
|
|
$page = (new Page($pageId)) |
|
45
|
|
|
->setVariable('title', $term->getName()); |
|
46
|
|
|
if ($this->pagesCollection->has($pageId)) { |
|
47
|
|
|
$page = clone $this->pagesCollection->get($pageId); |
|
48
|
|
|
} |
|
49
|
|
|
$page |
|
50
|
|
|
->setType(Type::TERM) |
|
51
|
|
|
->setPath($path) |
|
52
|
|
|
->setVariable('date', $date) |
|
53
|
|
|
->setVariable('term', $term->getId()) |
|
54
|
|
|
->setVariable('plural', $plural) |
|
55
|
|
|
->setVariable('singular', $singular) |
|
56
|
|
|
->setVariable('pages', $pages) |
|
57
|
|
|
->setVariable('pagination', ['pages' => $pages]); |
|
58
|
|
|
$this->generatedPages->add($page); |
|
59
|
|
|
} |
|
60
|
|
|
/* |
|
61
|
|
|
* Creates $plural pages (list of terms) |
|
62
|
|
|
* ex: /tags/ |
|
63
|
|
|
*/ |
|
64
|
|
|
$pageId = $path = Page::slugify($plural); |
|
65
|
|
|
$page = (new Page($pageId)) |
|
66
|
|
|
->setType(Type::VOCABULARY) |
|
67
|
|
|
->setPath($path) |
|
68
|
|
|
->setVariable('title', ucfirst($plural)) |
|
69
|
|
|
->setVariable('date', $date) |
|
|
|
|
|
|
70
|
|
|
->setVariable('plural', $plural) |
|
71
|
|
|
->setVariable('singular', $singular) |
|
72
|
|
|
->setVariable('terms', $vocabulary); |
|
73
|
|
|
// add page only if a template exist |
|
74
|
|
|
try { |
|
75
|
|
|
$this->generatedPages->add($page); |
|
76
|
|
|
} catch (Exception $e) { |
|
77
|
|
|
printf("%s\n", $e->getMessage()); |
|
78
|
|
|
unset($page); // do not add page |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: