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
|
|
|
public static function finder(CollectionPage $page, string $format, \Cecil\Config $config): array |
43
|
|
|
{ |
44
|
|
|
$layout = 'unknown'; |
45
|
|
|
|
46
|
|
|
// which layouts, in what format, could be used for the page? |
47
|
|
|
$layouts = self::lookup($page, $format, $config); |
48
|
|
|
|
49
|
|
|
// take the first available layout |
50
|
|
|
foreach ($layouts as $layout) { |
51
|
|
|
$layout = Util::joinFile($layout); |
52
|
|
|
// is it in `layouts/` dir? |
53
|
|
|
if (Util\File::getFS()->exists(Util::joinFile($config->getLayoutsPath(), $layout))) { |
54
|
|
|
return [ |
55
|
|
|
'scope' => 'site', |
56
|
|
|
'file' => $layout, |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
// is it in `<theme>/layouts/` dir? |
60
|
|
|
if ($config->hasTheme()) { |
61
|
|
|
$themes = $config->getTheme(); |
62
|
|
|
foreach ($themes ?? [] as $theme) { |
63
|
|
|
if (Util\File::getFS()->exists(Util::joinFile($config->getThemeDirPath($theme, 'layouts'), $layout))) { |
64
|
|
|
return [ |
65
|
|
|
'scope' => $theme, |
66
|
|
|
'file' => $layout, |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
// is it in resources/layouts/ dir? |
72
|
|
|
if (Util\File::getFS()->exists(Util::joinPath($config->getLayoutsInternalPath(), $layout))) { |
73
|
|
|
return [ |
74
|
|
|
'scope' => 'cecil', |
75
|
|
|
'file' => $layout, |
76
|
|
|
]; |
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
|
|
|
protected static function lookup(CollectionPage $page, string $format, \Cecil\Config $config): array |
89
|
|
|
{ |
90
|
|
|
$ext = self::EXT; |
91
|
|
|
|
92
|
|
|
// remove potential redundant extension |
93
|
|
|
$layout = str_replace(".$ext", '', (string) $page->getVariable('layout')); |
94
|
|
|
// page section or layout mapping |
95
|
|
|
$section = $config->getLayoutSection($page->getSection()); |
96
|
|
|
|
97
|
|
|
switch ($page->getType()) { |
98
|
|
|
case PageType::HOMEPAGE->value: |
99
|
|
|
$layouts = [ |
100
|
|
|
// "$layout.$format.$ext", |
101
|
|
|
"index.$format.$ext", |
102
|
|
|
"home.$format.$ext", |
103
|
|
|
"list.$format.$ext", |
104
|
|
|
]; |
105
|
|
|
if ($page->hasVariable('layout')) { |
106
|
|
|
$layouts = array_merge(["$layout.$format.$ext"], $layouts, ["_default/$layout.$format.$ext"]); |
107
|
|
|
} |
108
|
|
|
$layouts = array_merge($layouts, [ |
109
|
|
|
// "_default/$layout.$format.$ext", |
110
|
|
|
"_default/index.$format.$ext", |
111
|
|
|
"_default/home.$format.$ext", |
112
|
|
|
"_default/list.$format.$ext", |
113
|
|
|
"_default/page.$format.$ext", |
114
|
|
|
]); |
115
|
|
|
break; |
116
|
|
|
case PageType::SECTION->value: |
117
|
|
|
$layouts = [ |
118
|
|
|
// "$layout.$format.$ext", |
119
|
|
|
// "$section/index.$format.$ext", |
120
|
|
|
// "$section/list.$format.$ext", |
121
|
|
|
// "section/$section.$format.$ext", |
122
|
|
|
"_default/section.$format.$ext", |
123
|
|
|
"_default/list.$format.$ext", |
124
|
|
|
]; |
125
|
|
|
if ($page->getPath()) { |
126
|
|
|
$layouts = array_merge(["section/{$section}.$format.$ext"], $layouts); |
127
|
|
|
$layouts = array_merge(["{$section}/list.$format.$ext"], $layouts); |
128
|
|
|
$layouts = array_merge(["{$section}/index.$format.$ext"], $layouts); |
129
|
|
|
} |
130
|
|
|
if ($page->hasVariable('layout')) { |
131
|
|
|
$layouts = array_merge(["$layout.$format.$ext"], $layouts); |
132
|
|
|
} |
133
|
|
|
break; |
134
|
|
|
case PageType::VOCABULARY->value: |
135
|
|
|
$layouts = [ |
136
|
|
|
// "taxonomy/$plural.$format.$ext", // e.g.: taxonomy/tags.html.twig |
137
|
|
|
"vocabulary.$format.$ext", // e.g.: vocabulary.html.twig |
138
|
|
|
"_default/vocabulary.$format.$ext", // e.g.: _default/vocabulary.html.twig |
139
|
|
|
]; |
140
|
|
|
if ($page->hasVariable('plural')) { |
141
|
|
|
$layouts = array_merge(["taxonomy/{$page->getVariable('plural')}.$format.$ext"], $layouts); |
142
|
|
|
} |
143
|
|
|
break; |
144
|
|
|
case PageType::TERM->value: |
145
|
|
|
$layouts = [ |
146
|
|
|
// "taxonomy/$term.$format.$ext", // e.g.: taxonomy/velo.html.twig |
147
|
|
|
// "taxonomy/$singular.$format.$ext", // e.g.: taxonomy/tag.html.twig |
148
|
|
|
"term.$format.$ext", // e.g.: term.html.twig |
149
|
|
|
"_default/term.$format.$ext", // e.g.: _default/term.html.twig |
150
|
|
|
"_default/list.$format.$ext", // e.g.: _default/list.html.twig |
151
|
|
|
]; |
152
|
|
|
if ($page->hasVariable('term')) { |
153
|
|
|
$layouts = array_merge(["taxonomy/{$page->getVariable('term')}.$format.$ext"], $layouts); |
154
|
|
|
} |
155
|
|
|
if ($page->hasVariable('singular')) { |
156
|
|
|
$layouts = array_merge(["taxonomy/{$page->getVariable('singular')}.$format.$ext"], $layouts); |
157
|
|
|
} |
158
|
|
|
break; |
159
|
|
|
default: |
160
|
|
|
$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
|
|
|
"_default/page.$format.$ext", |
167
|
|
|
]; |
168
|
|
|
$layouts = array_merge(["page.$format.$ext"], $layouts); |
169
|
|
|
if ($page->hasVariable('layout')) { |
170
|
|
|
$layouts = array_merge(["_default/$layout.$format.$ext"], $layouts); |
171
|
|
|
} |
172
|
|
|
if ($section) { |
173
|
|
|
$layouts = array_merge(["{$section}/page.$format.$ext"], $layouts); |
174
|
|
|
} |
175
|
|
|
if ($page->hasVariable('layout')) { |
176
|
|
|
$layouts = array_merge(["$layout.$format.$ext"], $layouts); |
177
|
|
|
if ($section) { |
178
|
|
|
$layouts = array_merge(["{$section}/$layout.$format.$ext"], $layouts); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// add localized layouts |
184
|
|
|
if ($page->getVariable('language') !== $config->getLanguageDefault()) { |
185
|
|
|
foreach ($layouts as $key => $value) { |
186
|
|
|
$layouts = array_merge(\array_slice($layouts, 0, $key), [str_replace(".$ext", ".{$page->getVariable('language')}.$ext", $value)], \array_slice($layouts, $key)); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $layouts; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|