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\Collection\Page\Page as CollectionPage; |
17
|
|
|
use Cecil\Collection\Page\Type as PageType; |
18
|
|
|
use Cecil\Config; |
19
|
|
|
use Cecil\Exception\RuntimeException; |
20
|
|
|
use Cecil\Util; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Layout. |
24
|
|
|
*/ |
25
|
|
|
class Layout |
26
|
|
|
{ |
27
|
|
|
public const EXT = 'twig'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Layout files finder. |
31
|
|
|
* |
32
|
|
|
* @throws RuntimeException |
33
|
|
|
*/ |
34
|
1 |
|
public static function finder(CollectionPage $page, string $format, Config $config): array |
35
|
|
|
{ |
36
|
1 |
|
$layout = 'unknown'; |
37
|
|
|
|
38
|
|
|
// what layouts, in what format, could be use for the page? |
39
|
1 |
|
$layouts = self::fallback($page, $format, $config); |
40
|
|
|
|
41
|
|
|
// take the first available layout |
42
|
1 |
|
foreach ($layouts as $layout) { |
43
|
1 |
|
$layout = Util::joinFile($layout); |
44
|
|
|
// is it in `layouts/` dir? |
45
|
1 |
|
if (Util\File::getFS()->exists(Util::joinFile($config->getLayoutsPath(), $layout))) { |
46
|
1 |
|
return [ |
47
|
1 |
|
'scope' => 'site', |
48
|
1 |
|
'file' => $layout, |
49
|
1 |
|
]; |
50
|
|
|
} |
51
|
|
|
// is it in `<theme>/layouts/` dir? |
52
|
1 |
|
if ($config->hasTheme()) { |
53
|
1 |
|
$themes = $config->getTheme(); |
54
|
1 |
|
foreach ($themes as $theme) { |
55
|
1 |
|
if (Util\File::getFS()->exists(Util::joinFile($config->getThemeDirPath($theme, 'layouts'), $layout))) { |
56
|
1 |
|
return [ |
57
|
1 |
|
'scope' => $theme, |
58
|
1 |
|
'file' => $layout, |
59
|
1 |
|
]; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
// is it in `resources/layouts/` dir? |
64
|
1 |
|
if (Util\File::getFS()->exists(Util::joinPath($config->getInternalLayoutsPath(), $layout))) { |
65
|
1 |
|
return [ |
66
|
1 |
|
'scope' => 'cecil', |
67
|
1 |
|
'file' => $layout, |
68
|
1 |
|
]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
throw new RuntimeException(sprintf('Layout "%s" not found (page: %s).', $layout, $page->getId())); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Layout fall-back. |
77
|
|
|
* |
78
|
|
|
* @see finder() |
79
|
|
|
*/ |
80
|
1 |
|
protected static function fallback(CollectionPage $page, string $format, Config $config): array |
81
|
|
|
{ |
82
|
1 |
|
$ext = self::EXT; |
83
|
|
|
|
84
|
|
|
// remove potential redundant extension |
85
|
1 |
|
$layout = str_replace(".$ext", '', (string) $page->getVariable('layout')); |
86
|
|
|
|
87
|
1 |
|
switch ($page->getType()) { |
88
|
|
|
case PageType::HOMEPAGE: |
89
|
1 |
|
$layouts = [ |
90
|
|
|
// "$layout.$format.$ext", |
91
|
1 |
|
"index.$format.$ext", |
92
|
1 |
|
"home.$format.$ext", |
93
|
1 |
|
"list.$format.$ext", |
94
|
1 |
|
]; |
95
|
1 |
|
if ($page->hasVariable('layout')) { |
96
|
|
|
$layouts = array_merge(["$layout.$format.$ext"], $layouts, ["_default/$layout.$format.$ext"]); |
97
|
|
|
} |
98
|
1 |
|
$layouts = array_merge($layouts, [ |
99
|
|
|
// "_default/$layout.$format.$ext", |
100
|
1 |
|
"_default/index.$format.$ext", |
101
|
1 |
|
"_default/home.$format.$ext", |
102
|
1 |
|
"_default/list.$format.$ext", |
103
|
1 |
|
"_default/page.$format.$ext", |
104
|
1 |
|
]); |
105
|
1 |
|
break; |
106
|
|
|
case PageType::SECTION: |
107
|
1 |
|
$layouts = [ |
108
|
|
|
// "$layout.$format.$ext", |
109
|
|
|
// "$section/index.$format.$ext", |
110
|
|
|
// "$section/list.$format.$ext", |
111
|
|
|
// "section/$section.$format.$ext", |
112
|
1 |
|
"_default/section.$format.$ext", |
113
|
1 |
|
"_default/list.$format.$ext", |
114
|
1 |
|
]; |
115
|
1 |
|
if ($page->getPath()) { |
116
|
1 |
|
$layouts = array_merge(["section/{$page->getSection()}.$format.$ext"], $layouts); |
117
|
1 |
|
$layouts = array_merge(["{$page->getSection()}/list.$format.$ext"], $layouts); |
118
|
1 |
|
$layouts = array_merge(["{$page->getSection()}/index.$format.$ext"], $layouts); |
119
|
|
|
} |
120
|
1 |
|
if ($page->hasVariable('layout')) { |
121
|
|
|
$layouts = array_merge(["$layout.$format.$ext"], $layouts); |
122
|
|
|
} |
123
|
1 |
|
break; |
124
|
|
|
case PageType::VOCABULARY: |
125
|
1 |
|
$layouts = [ |
126
|
|
|
// "taxonomy/$plural.$format.$ext", // e.g.: taxonomy/tags.html.twig |
127
|
1 |
|
"_default/vocabulary.$format.$ext", // e.g.: _default/vocabulary.html.twig |
128
|
1 |
|
]; |
129
|
1 |
|
if ($page->hasVariable('plural')) { |
130
|
1 |
|
$layouts = array_merge(["taxonomy/{$page->getVariable('plural')}.$format.$ext"], $layouts); |
131
|
|
|
} |
132
|
1 |
|
break; |
133
|
|
|
case PageType::TERM: |
134
|
1 |
|
$layouts = [ |
135
|
|
|
// "taxonomy/$term.$format.$ext", // e.g.: taxonomy/velo.html.twig |
136
|
|
|
// "taxonomy/$singular.$format.$ext", // e.g.: taxonomy/tag.html.twig |
137
|
1 |
|
"_default/term.$format.$ext", // e.g.: _default/term.html.twig |
138
|
1 |
|
"_default/list.$format.$ext", // e.g.: _default/list.html.twig |
139
|
1 |
|
]; |
140
|
1 |
|
if ($page->hasVariable('term')) { |
141
|
1 |
|
$layouts = array_merge(["taxonomy/{$page->getVariable('term')}.$format.$ext"], $layouts); |
142
|
|
|
} |
143
|
1 |
|
if ($page->hasVariable('singular')) { |
144
|
1 |
|
$layouts = array_merge(["taxonomy/{$page->getVariable('singular')}.$format.$ext"], $layouts); |
145
|
|
|
} |
146
|
1 |
|
break; |
147
|
|
|
default: |
148
|
1 |
|
$layouts = [ |
149
|
|
|
// "$section/$layout.$format.$ext", |
150
|
|
|
// "$layout.$format.$ext", |
151
|
|
|
// "$section/page.$format.$ext", |
152
|
|
|
// "page.$format.$ext", |
153
|
|
|
// "_default/$layout.$format.$ext", |
154
|
1 |
|
"_default/page.$format.$ext", |
155
|
1 |
|
]; |
156
|
1 |
|
if ($page->hasVariable('layout')) { |
157
|
1 |
|
$layouts = array_merge(["_default/$layout.$format.$ext"], $layouts); |
158
|
|
|
} |
159
|
1 |
|
$layouts = array_merge(["page.$format.$ext"], $layouts); |
160
|
1 |
|
if ($page->getSection()) { |
161
|
1 |
|
$layouts = array_merge(["{$page->getSection()}/page.$format.$ext"], $layouts); |
162
|
|
|
} |
163
|
1 |
|
if ($page->hasVariable('layout')) { |
164
|
1 |
|
$layouts = array_merge(["$layout.$format.$ext"], $layouts); |
165
|
1 |
|
if ($page->getSection()) { |
166
|
1 |
|
$layouts = array_merge(["{$page->getSection()}/$layout.$format.$ext"], $layouts); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// add localized layouts |
172
|
1 |
|
if ($page->getVariable('language') !== $config->getLanguageDefault()) { |
173
|
1 |
|
foreach ($layouts as $key => $value) { |
174
|
1 |
|
$layouts = array_merge(\array_slice($layouts, 0, $key), [str_replace(".$ext", ".{$page->getVariable('language')}.$ext", $value)], \array_slice($layouts, $key)); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
1 |
|
return $layouts; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|