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