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
|
|
|
#[\ReturnTypeWillChange] |
48
|
1 |
|
public function offsetExists($offset): bool |
49
|
|
|
{ |
50
|
|
|
// special cases |
51
|
|
|
switch ($offset) { |
52
|
1 |
|
case 'config': |
53
|
1 |
|
case 'home': |
54
|
1 |
|
case 'menus': |
55
|
1 |
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
return $this->config->has($offset); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Implements \ArrayAccess. |
63
|
|
|
* |
64
|
|
|
* @param mixed $offset |
65
|
|
|
* |
66
|
|
|
* @return mixed|null |
67
|
|
|
*/ |
68
|
|
|
#[\ReturnTypeWillChange] |
69
|
1 |
|
public function offsetGet($offset) |
70
|
|
|
{ |
71
|
|
|
// If it's a built-in variable: dot not fetchs data from config raw |
72
|
|
|
switch ($offset) { |
73
|
1 |
|
case 'home': |
74
|
1 |
|
return $this->language != $this->config->getLanguageDefault() ? \sprintf('index.%s', $this->language) : 'index'; |
75
|
1 |
|
case 'language': |
76
|
1 |
|
return new Language($this->config, $this->language); |
77
|
1 |
|
case 'taxonomies': |
78
|
1 |
|
return $this->builder->getTaxonomies(); |
79
|
1 |
|
case 'menus': |
80
|
1 |
|
return $this->builder->getMenus($this->language); |
81
|
1 |
|
case 'pages': |
82
|
1 |
|
return $this->getPages(); |
83
|
1 |
|
case 'data': |
84
|
1 |
|
return $this->builder->getData(); |
85
|
1 |
|
case 'static': |
86
|
1 |
|
return $this->builder->getStatic(); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
return $this->config->get($offset, $this->language); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Implements \ArrayAccess. |
94
|
|
|
* |
95
|
|
|
* @param mixed $offset |
96
|
|
|
* @param mixed $value |
97
|
|
|
* |
98
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
99
|
|
|
*/ |
100
|
|
|
#[\ReturnTypeWillChange] |
101
|
|
|
public function offsetSet($offset, $value): void |
102
|
|
|
{ |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Implements \ArrayAccess. |
107
|
|
|
* |
108
|
|
|
* @param mixed $offset |
109
|
|
|
* |
110
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
111
|
|
|
*/ |
112
|
|
|
#[\ReturnTypeWillChange] |
113
|
|
|
public function offsetUnset($offset): void |
114
|
|
|
{ |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns a page in the current language or the one provided. |
119
|
|
|
* |
120
|
|
|
* @throws \DomainException |
121
|
|
|
*/ |
122
|
1 |
|
public function getPage(string $id, string $language = null): ?CollectionPage |
123
|
|
|
{ |
124
|
1 |
|
$pageId = $id; |
125
|
1 |
|
if ($language === null && $this->language != $this->config->getLanguageDefault()) { |
126
|
1 |
|
$pageId = \sprintf('%s.%s', $id, $this->language); |
127
|
|
|
} |
128
|
1 |
|
if ($language !== null && $language != $this->config->getLanguageDefault()) { |
129
|
1 |
|
$pageId = \sprintf('%s.%s', $id, $language); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
return $this->builder->getPages()->get($pageId); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns all pages, in the current language. |
137
|
|
|
*/ |
138
|
1 |
|
public function getPages(): \Cecil\Collection\Page\Collection |
139
|
|
|
{ |
140
|
1 |
|
return $this->builder->getPages()->filter(function (CollectionPage $page) { |
141
|
|
|
// We should fix case of virtual pages without language |
142
|
1 |
|
if ($page->getVariable('language') === null && $this->language == $this->config->getLanguageDefault()) { |
143
|
|
|
return true; |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
return $page->getVariable('language') == $this->language; |
147
|
1 |
|
}); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns all pages, regardless of their translation. |
152
|
|
|
*/ |
153
|
1 |
|
public function getAllPages(): \Cecil\Collection\Page\Collection |
154
|
|
|
{ |
155
|
1 |
|
return $this->builder->getPages(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Alias of getAllPages(). |
160
|
|
|
*/ |
161
|
|
|
public function getPagesIntl(): \Cecil\Collection\Page\Collection |
162
|
|
|
{ |
163
|
|
|
return $this->getAllPages(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Return current time. |
168
|
|
|
*/ |
169
|
1 |
|
public function getTime(): int |
170
|
|
|
{ |
171
|
1 |
|
return time(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|