|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Cecil. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Cecil\Renderer; |
|
15
|
|
|
|
|
16
|
|
|
use Cecil\Collection\Page\Page as CollectionPage; |
|
17
|
|
|
use Cecil\Collection\Page\Type as PageType; |
|
18
|
|
|
use Cecil\Exception\RuntimeException; |
|
19
|
|
|
use Cecil\Util; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Layout renderer class. |
|
23
|
|
|
* |
|
24
|
|
|
* This class is responsible for finding and returning the appropriate layout file |
|
25
|
|
|
* for a given page based on its type, section, and other variables. |
|
26
|
|
|
* It looks for layout files in various directories such as the site's layouts directory, |
|
27
|
|
|
* the theme's layouts directory, and the internal resources/layouts directory. |
|
28
|
|
|
*/ |
|
29
|
|
|
class Layout |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Twig template extension. |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
public const EXT = 'twig'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Layout files finder. |
|
39
|
|
|
* |
|
40
|
|
|
* @throws RuntimeException |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public static function finder(CollectionPage $page, string $format, \Cecil\Config $config): array |
|
43
|
|
|
{ |
|
44
|
1 |
|
$layout = 'unknown'; |
|
45
|
|
|
|
|
46
|
|
|
// which layouts, in what format, could be used for the page? |
|
47
|
1 |
|
$layouts = self::lookup($page, $format, $config); |
|
48
|
|
|
|
|
49
|
|
|
// take the first available layout |
|
50
|
1 |
|
foreach ($layouts as $layout) { |
|
51
|
1 |
|
$layout = Util::joinFile($layout); |
|
52
|
|
|
// is it in `layouts/` dir? |
|
53
|
1 |
|
if (Util\File::getFS()->exists(Util::joinFile($config->getLayoutsPath(), $layout))) { |
|
54
|
1 |
|
return [ |
|
55
|
1 |
|
'scope' => 'site', |
|
56
|
1 |
|
'file' => $layout, |
|
57
|
1 |
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
// is it in `<theme>/layouts/` dir? |
|
60
|
1 |
|
if ($config->hasTheme()) { |
|
61
|
1 |
|
$themes = $config->getTheme(); |
|
62
|
1 |
|
foreach ($themes ?? [] as $theme) { |
|
63
|
1 |
|
if (Util\File::getFS()->exists(Util::joinFile($config->getThemeDirPath($theme, 'layouts'), $layout))) { |
|
64
|
1 |
|
return [ |
|
65
|
1 |
|
'scope' => $theme, |
|
66
|
1 |
|
'file' => $layout, |
|
67
|
1 |
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
// is it in resources/layouts/ dir? |
|
72
|
1 |
|
if (Util\File::getFS()->exists(Util::joinPath($config->getLayoutsInternalPath(), $layout))) { |
|
73
|
1 |
|
return [ |
|
74
|
1 |
|
'scope' => 'cecil', |
|
75
|
1 |
|
'file' => $layout, |
|
76
|
1 |
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
throw new RuntimeException(\sprintf('Layout "%s" not found (page: %s).', $layout, $page->getId())); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Templates lookup rules. |
|
85
|
|
|
* |
|
86
|
|
|
* @see self::finder() |
|
87
|
|
|
*/ |
|
88
|
1 |
|
protected static function lookup(CollectionPage $page, string $format, \Cecil\Config $config): array |
|
89
|
|
|
{ |
|
90
|
1 |
|
$ext = self::EXT; |
|
91
|
|
|
|
|
92
|
|
|
// remove potential redundant extension |
|
93
|
1 |
|
$layout = str_replace(".$ext", '', (string) $page->getVariable('layout')); |
|
94
|
|
|
// page section or layout mapping |
|
95
|
1 |
|
$section = $config->getLayoutSection($page->getSection()); |
|
96
|
|
|
|
|
97
|
1 |
|
switch ($page->getType()) { |
|
98
|
1 |
|
case PageType::HOMEPAGE->value: |
|
99
|
1 |
|
$layouts = [ |
|
100
|
|
|
// "$layout.$format.$ext", |
|
101
|
1 |
|
"index.$format.$ext", |
|
102
|
1 |
|
"home.$format.$ext", |
|
103
|
1 |
|
"list.$format.$ext", |
|
104
|
1 |
|
]; |
|
105
|
1 |
|
if ($page->hasVariable('layout')) { |
|
106
|
|
|
$layouts = array_merge(["$layout.$format.$ext"], $layouts, ["_default/$layout.$format.$ext"]); |
|
107
|
|
|
} |
|
108
|
1 |
|
$layouts = array_merge($layouts, [ |
|
109
|
|
|
// "_default/$layout.$format.$ext", |
|
110
|
1 |
|
"_default/index.$format.$ext", |
|
111
|
1 |
|
"_default/home.$format.$ext", |
|
112
|
1 |
|
"_default/list.$format.$ext", |
|
113
|
1 |
|
"_default/page.$format.$ext", |
|
114
|
1 |
|
]); |
|
115
|
1 |
|
break; |
|
116
|
1 |
|
case PageType::SECTION->value: |
|
117
|
1 |
|
$layouts = [ |
|
118
|
|
|
// "$layout.$format.$ext", |
|
119
|
|
|
// "$section/index.$format.$ext", |
|
120
|
|
|
// "$section/list.$format.$ext", |
|
121
|
|
|
// "section/$section.$format.$ext", |
|
122
|
1 |
|
"_default/section.$format.$ext", |
|
123
|
1 |
|
"_default/list.$format.$ext", |
|
124
|
1 |
|
]; |
|
125
|
1 |
|
if ($page->getPath()) { |
|
126
|
1 |
|
$layouts = array_merge(["section/{$section}.$format.$ext"], $layouts); |
|
127
|
1 |
|
$layouts = array_merge(["{$section}/list.$format.$ext"], $layouts); |
|
128
|
1 |
|
$layouts = array_merge(["{$section}/index.$format.$ext"], $layouts); |
|
129
|
|
|
} |
|
130
|
1 |
|
if ($page->hasVariable('layout')) { |
|
131
|
|
|
$layouts = array_merge(["$layout.$format.$ext"], $layouts); |
|
132
|
|
|
} |
|
133
|
1 |
|
break; |
|
134
|
1 |
|
case PageType::VOCABULARY->value: |
|
135
|
1 |
|
$layouts = [ |
|
136
|
|
|
// "taxonomy/$plural.$format.$ext", // e.g.: taxonomy/tags.html.twig |
|
137
|
1 |
|
"vocabulary.$format.$ext", // e.g.: vocabulary.html.twig |
|
138
|
1 |
|
"_default/vocabulary.$format.$ext", // e.g.: _default/vocabulary.html.twig |
|
139
|
1 |
|
]; |
|
140
|
1 |
|
if ($page->hasVariable('plural')) { |
|
141
|
1 |
|
$layouts = array_merge(["taxonomy/{$page->getVariable('plural')}.$format.$ext"], $layouts); |
|
142
|
|
|
} |
|
143
|
1 |
|
break; |
|
144
|
1 |
|
case PageType::TERM->value: |
|
145
|
1 |
|
$layouts = [ |
|
146
|
|
|
// "taxonomy/$term.$format.$ext", // e.g.: taxonomy/velo.html.twig |
|
147
|
|
|
// "taxonomy/$singular.$format.$ext", // e.g.: taxonomy/tag.html.twig |
|
148
|
1 |
|
"term.$format.$ext", // e.g.: term.html.twig |
|
149
|
1 |
|
"_default/term.$format.$ext", // e.g.: _default/term.html.twig |
|
150
|
1 |
|
"_default/list.$format.$ext", // e.g.: _default/list.html.twig |
|
151
|
1 |
|
]; |
|
152
|
1 |
|
if ($page->hasVariable('term')) { |
|
153
|
1 |
|
$layouts = array_merge(["taxonomy/{$page->getVariable('term')}.$format.$ext"], $layouts); |
|
154
|
|
|
} |
|
155
|
1 |
|
if ($page->hasVariable('singular')) { |
|
156
|
1 |
|
$layouts = array_merge(["taxonomy/{$page->getVariable('singular')}.$format.$ext"], $layouts); |
|
157
|
|
|
} |
|
158
|
1 |
|
break; |
|
159
|
|
|
default: |
|
160
|
1 |
|
$layouts = [ |
|
161
|
|
|
// "$section/$layout.$format.$ext", |
|
162
|
|
|
// "$layout.$format.$ext", |
|
163
|
|
|
// "$section/page.$format.$ext", |
|
164
|
|
|
// "_default/$layout.$format.$ext", |
|
165
|
|
|
// "page.$format.$ext", |
|
166
|
1 |
|
"_default/page.$format.$ext", |
|
167
|
1 |
|
]; |
|
168
|
1 |
|
$layouts = array_merge(["page.$format.$ext"], $layouts); |
|
169
|
1 |
|
if ($page->hasVariable('layout')) { |
|
170
|
1 |
|
$layouts = array_merge(["_default/$layout.$format.$ext"], $layouts); |
|
171
|
|
|
} |
|
172
|
1 |
|
if ($section) { |
|
173
|
1 |
|
$layouts = array_merge(["{$section}/page.$format.$ext"], $layouts); |
|
174
|
|
|
} |
|
175
|
1 |
|
if ($page->hasVariable('layout')) { |
|
176
|
1 |
|
$layouts = array_merge(["$layout.$format.$ext"], $layouts); |
|
177
|
1 |
|
if ($section) { |
|
178
|
1 |
|
$layouts = array_merge(["{$section}/$layout.$format.$ext"], $layouts); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// add localized layouts |
|
184
|
1 |
|
if ($page->getVariable('language') !== $config->getLanguageDefault()) { |
|
185
|
1 |
|
foreach ($layouts as $key => $value) { |
|
186
|
1 |
|
$layouts = array_merge(\array_slice($layouts, 0, $key), [str_replace(".$ext", ".{$page->getVariable('language')}.$ext", $value)], \array_slice($layouts, $key)); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
return $layouts; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|