|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Cecil\Renderer; |
|
10
|
|
|
|
|
11
|
|
|
use Cecil\Collection\Page\Page; |
|
12
|
|
|
use Cecil\Config; |
|
13
|
|
|
use Cecil\Exception\Exception; |
|
14
|
|
|
use Cecil\Page\NodeType; |
|
15
|
|
|
use Cecil\Util; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Layout. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Layout |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Layout file finder. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Page $page |
|
26
|
|
|
* @param Config $config |
|
27
|
|
|
* |
|
28
|
|
|
* @throws Exception |
|
29
|
|
|
* |
|
30
|
|
|
* @return string |
|
31
|
|
|
*/ |
|
32
|
|
|
public function finder(Page $page, Config $config) |
|
33
|
|
|
{ |
|
34
|
|
|
$layout = 'unknown'; |
|
35
|
|
|
|
|
36
|
|
|
// what layouts could be use for the page? |
|
37
|
|
|
$layouts = self::fallback($page); |
|
38
|
|
|
|
|
39
|
|
|
// take the first available layout |
|
40
|
|
|
foreach ($layouts as $layout) { |
|
41
|
|
|
// is it in layouts/ dir? |
|
42
|
|
|
if (Util::getFS()->exists($config->getLayoutsPath().'/'.$layout)) { |
|
43
|
|
|
return $layout; |
|
44
|
|
|
} |
|
45
|
|
|
// is it in <theme>/layouts/ dir? |
|
46
|
|
|
if ($config->hasTheme()) { |
|
47
|
|
|
$themes = $config->getTheme(); |
|
48
|
|
|
foreach ($themes as $theme) { |
|
49
|
|
|
if (Util::getFS()->exists($config->getThemeDirPath($theme, 'layouts').'/'.$layout)) { |
|
50
|
|
|
return $layout; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
// is it in res/layouts/ dir? |
|
55
|
|
|
if (Util::getFS()->exists($config->getInternalLayoutsPath().'/'.$layout)) { |
|
56
|
|
|
return $layout; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
throw new Exception(sprintf("Layout '%s' not found for page '%s'!", $layout, $page->getId())); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Layout fall-back. |
|
65
|
|
|
* |
|
66
|
|
|
* @param $page |
|
67
|
|
|
* |
|
68
|
|
|
* @return string[] |
|
69
|
|
|
* |
|
70
|
|
|
* @see finder() |
|
71
|
|
|
*/ |
|
72
|
|
|
protected static function fallback(Page $page) |
|
73
|
|
|
{ |
|
74
|
|
|
// remove redundant '.twig' extension |
|
75
|
|
|
$layout = str_replace('.twig', '', $page->getLayout()); |
|
76
|
|
|
|
|
77
|
|
|
switch ($page->getNodeType()) { |
|
78
|
|
|
case NodeType::HOMEPAGE: |
|
79
|
|
|
$layouts = [ |
|
80
|
|
|
'index.html.twig', |
|
81
|
|
|
'_default/list.html.twig', |
|
82
|
|
|
'_default/page.html.twig', |
|
83
|
|
|
]; |
|
84
|
|
|
break; |
|
85
|
|
|
case NodeType::SECTION: |
|
86
|
|
|
$layouts = [ |
|
87
|
|
|
// 'section/$section.html.twig', |
|
88
|
|
|
'_default/section.html.twig', |
|
89
|
|
|
'_default/list.html.twig', |
|
90
|
|
|
]; |
|
91
|
|
|
if ($page->getPathname()) { |
|
92
|
|
|
$section = explode('/', $page->getPathname())[0]; |
|
93
|
|
|
$layouts = array_merge( |
|
94
|
|
|
[sprintf('section/%s.html.twig', $section)], |
|
95
|
|
|
$layouts |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
break; |
|
99
|
|
View Code Duplication |
case NodeType::TAXONOMY: |
|
|
|
|
|
|
100
|
|
|
$layouts = [ |
|
101
|
|
|
// 'taxonomy/$singular.html.twig', |
|
102
|
|
|
'_default/taxonomy.html.twig', |
|
103
|
|
|
'_default/list.html.twig', |
|
104
|
|
|
]; |
|
105
|
|
|
if ($page->getVariable('singular')) { |
|
106
|
|
|
$layouts = array_merge( |
|
107
|
|
|
[sprintf('taxonomy/%s.html.twig', $page->getVariable('singular'))], |
|
108
|
|
|
$layouts |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
break; |
|
112
|
|
View Code Duplication |
case NodeType::TERMS: |
|
|
|
|
|
|
113
|
|
|
$layouts = [ |
|
114
|
|
|
// 'taxonomy/$singular.terms.html.twig', |
|
115
|
|
|
'_default/terms.html.twig', |
|
116
|
|
|
]; |
|
117
|
|
|
if ($page->getVariable('singular')) { |
|
118
|
|
|
$layouts = array_merge( |
|
119
|
|
|
[sprintf('taxonomy/%s.terms.html.twig', $page->getVariable('singular'))], |
|
120
|
|
|
$layouts |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
break; |
|
124
|
|
|
default: |
|
125
|
|
|
$layouts = [ |
|
126
|
|
|
// '$section/$layout.twig', |
|
127
|
|
|
// '$layout.twig', |
|
128
|
|
|
// '$section/page.html.twig', |
|
129
|
|
|
// 'page.html.twig', |
|
130
|
|
|
'_default/page.html.twig', |
|
131
|
|
|
]; |
|
132
|
|
|
$layouts = array_merge( |
|
133
|
|
|
['page.html.twig'], |
|
134
|
|
|
$layouts |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
if ($page->getSection()) { |
|
138
|
|
|
$layouts = array_merge( |
|
139
|
|
|
[sprintf('%s/page.html.twig', $page->getSection())], |
|
140
|
|
|
$layouts |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
if ($page->getLayout()) { |
|
144
|
|
|
$layouts = array_merge( |
|
145
|
|
|
[sprintf('%s.twig', $layout)], |
|
146
|
|
|
$layouts |
|
147
|
|
|
); |
|
148
|
|
|
if ($page->getSection()) { |
|
149
|
|
|
$layouts = array_merge( |
|
150
|
|
|
[sprintf('%s/%s.twig', $page->getSection(), $layout)], |
|
151
|
|
|
$layouts |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $layouts; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.