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