1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Cecil. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Cecil\Renderer; |
15
|
|
|
|
16
|
|
|
use Cecil\Builder; |
17
|
|
|
use Cecil\Collection\Page\Page as CollectionPage; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Site. |
21
|
|
|
*/ |
22
|
|
|
class Site implements \ArrayAccess |
23
|
|
|
{ |
24
|
|
|
/** @var Builder Builder object. */ |
25
|
|
|
protected $builder; |
26
|
|
|
|
27
|
|
|
/** @var \Cecil\Config */ |
28
|
|
|
protected $config; |
29
|
|
|
|
30
|
|
|
/** @var string Current language. */ |
31
|
|
|
protected $language; |
32
|
|
|
|
33
|
1 |
|
public function __construct(Builder $builder, string $language) |
34
|
|
|
{ |
35
|
1 |
|
$this->builder = $builder; |
36
|
1 |
|
$this->config = $this->builder->getConfig(); |
37
|
1 |
|
$this->language = $language; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Implement ArrayAccess. |
42
|
|
|
* |
43
|
|
|
* @param mixed $offset |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
1 |
|
#[\ReturnTypeWillChange] |
48
|
|
|
public function offsetExists($offset): bool |
49
|
|
|
{ |
50
|
|
|
// special cases |
51
|
|
|
switch ($offset) { |
52
|
1 |
|
case 'menus': |
53
|
1 |
|
case 'taxonomies': |
54
|
1 |
|
case 'home': |
55
|
1 |
|
case 'debug': |
56
|
1 |
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return $this->config->has($offset); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Implements \ArrayAccess. |
64
|
|
|
* |
65
|
|
|
* @param mixed $offset |
66
|
|
|
* |
67
|
|
|
* @return mixed|null |
68
|
|
|
*/ |
69
|
1 |
|
#[\ReturnTypeWillChange] |
70
|
|
|
public function offsetGet($offset) |
71
|
|
|
{ |
72
|
|
|
// If it's a built-in variable: dot not fetchs data from config raw |
73
|
|
|
switch ($offset) { |
74
|
1 |
|
case 'pages': |
75
|
1 |
|
return $this->getPages(); |
76
|
1 |
|
case 'menus': |
77
|
1 |
|
return $this->builder->getMenus($this->language); |
78
|
1 |
|
case 'taxonomies': |
79
|
1 |
|
return $this->builder->getTaxonomies($this->language); |
80
|
1 |
|
case 'data': |
81
|
1 |
|
return $this->builder->getData($this->language); |
82
|
1 |
|
case 'static': |
83
|
1 |
|
return $this->builder->getStatic(); |
84
|
1 |
|
case 'language': |
85
|
1 |
|
return new Language($this->config, $this->language); |
86
|
1 |
|
case 'home': |
87
|
1 |
|
return 'index'; |
88
|
1 |
|
case 'debug': |
89
|
|
|
return $this->builder->isDebug(); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $this->config->get($offset, $this->language); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Implements \ArrayAccess. |
97
|
|
|
* |
98
|
|
|
* @param mixed $offset |
99
|
|
|
* @param mixed $value |
100
|
|
|
* |
101
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
102
|
|
|
*/ |
103
|
|
|
#[\ReturnTypeWillChange] |
104
|
|
|
public function offsetSet($offset, $value): void |
105
|
|
|
{ |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Implements \ArrayAccess. |
110
|
|
|
* |
111
|
|
|
* @param mixed $offset |
112
|
|
|
* |
113
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
114
|
|
|
*/ |
115
|
|
|
#[\ReturnTypeWillChange] |
116
|
|
|
public function offsetUnset($offset): void |
117
|
|
|
{ |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns a page for the provided language or the current one provided. |
122
|
|
|
* |
123
|
|
|
* @throws \DomainException |
124
|
|
|
*/ |
125
|
1 |
|
public function getPage(string $id, ?string $language = null): ?CollectionPage |
126
|
|
|
{ |
127
|
1 |
|
$pageId = $id; |
128
|
1 |
|
$language = $language ?? $this->language; |
129
|
|
|
|
130
|
1 |
|
if ($language !== null && $language != $this->config->getLanguageDefault()) { |
131
|
1 |
|
$pageId = "$language/$id"; |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
if ($this->builder->getPages()->has($pageId) === false) { |
135
|
|
|
// if multilingual == false |
136
|
1 |
|
if ($this->builder->getPages()->has($id) && $this->builder->getPages()->get($id)->getVariable('multilingual') === false) { |
137
|
|
|
return $this->builder->getPages()->get($id); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
return null; |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
return $this->builder->getPages()->get($pageId); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns all pages, in the current language. |
148
|
|
|
*/ |
149
|
1 |
|
public function getPages(): \Cecil\Collection\Page\Collection |
150
|
|
|
{ |
151
|
1 |
|
return $this->builder->getPages()->filter(function (CollectionPage $page) { |
152
|
|
|
// We should fix case of virtual pages without language |
153
|
|
|
if ( |
154
|
1 |
|
$page->getVariable('language') === null |
155
|
1 |
|
&& $this->language == $this->config->getLanguageDefault() |
156
|
|
|
) { |
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
return $page->getVariable('language') == $this->language; |
161
|
1 |
|
}); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns all pages, regardless of their translation. |
166
|
|
|
*/ |
167
|
1 |
|
public function getAllPages(): \Cecil\Collection\Page\Collection |
168
|
|
|
{ |
169
|
1 |
|
return $this->builder->getPages(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Alias of getAllPages(). |
174
|
|
|
*/ |
175
|
|
|
public function getPagesIntl(): \Cecil\Collection\Page\Collection |
176
|
|
|
{ |
177
|
|
|
return $this->getAllPages(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns current time. |
182
|
|
|
*/ |
183
|
1 |
|
public function getTime(): int |
184
|
|
|
{ |
185
|
1 |
|
return time(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns the property value(s) of an output format. |
190
|
|
|
*/ |
191
|
1 |
|
public function getOutputProperty(string $name, string $property): string|array|null |
192
|
|
|
{ |
193
|
1 |
|
return $this->config->getOutputFormatProperty($name, $property); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|