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\Step; |
10
|
|
|
|
11
|
|
|
use Cecil\Collection\Page\Page; |
12
|
|
|
use Cecil\Exception\Exception; |
13
|
|
|
use Cecil\Renderer\Layout; |
14
|
|
|
use Cecil\Renderer\Twig as Twig; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Pages rendering. |
18
|
|
|
*/ |
19
|
|
|
class PagesRender extends AbstractStep |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
* |
24
|
|
|
* @throws Exception |
25
|
|
|
*/ |
26
|
|
|
public function init($options) |
27
|
|
|
{ |
28
|
|
|
if (!is_dir($this->config->getLayoutsPath()) && !$this->config->hasTheme()) { |
29
|
|
|
throw new Exception(sprintf( |
30
|
|
|
"'%s' is not a valid layouts directory", |
31
|
|
|
$this->config->getLayoutsPath() |
32
|
|
|
)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->process = true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
* |
41
|
|
|
* @throws Exception |
42
|
|
|
*/ |
43
|
|
|
public function process() |
44
|
|
|
{ |
45
|
|
|
// prepares renderer |
46
|
|
|
$this->builder->setRenderer(new Twig($this->getAllLayoutsPaths(), $this->config)); |
47
|
|
|
|
48
|
|
|
// add globals variables |
49
|
|
|
$this->addGlobals(); |
50
|
|
|
|
51
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['RENDER', 'Rendering pages']); |
52
|
|
|
|
53
|
|
|
// collect published pages |
54
|
|
|
/* @var $page Page */ |
55
|
|
|
$filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
56
|
|
|
return !empty($page->getVariable('published')); |
57
|
|
|
}); |
58
|
|
|
$max = count($filteredPages); |
59
|
|
|
|
60
|
|
|
// render each page |
61
|
|
|
$count = 0; |
62
|
|
|
foreach ($filteredPages as $page) { |
63
|
|
|
$count++; |
64
|
|
|
|
65
|
|
|
// WIP |
66
|
|
|
if ($page->getVariable('output')) { |
|
|
|
|
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
foreach ($page->getVariable('output') as $format) { |
71
|
|
|
$rendered = $this->builder->getRenderer()->render( |
72
|
|
|
$layout = (new Layout())->finder($page, $this->config), |
|
|
|
|
73
|
|
|
['page' => $page] |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
$page->setVariable('rendered', $rendered); |
|
|
|
|
79
|
|
|
$this->builder->getPages()->replace($page->getId(), $page); |
80
|
|
|
|
81
|
|
|
$message = sprintf('%s (%s)', ($page->getId() ?: 'index'), $layout); |
|
|
|
|
82
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['RENDER_PROGRESS', $message, $count, $max]); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return an array of layouts directories. |
88
|
|
|
* |
89
|
|
|
* @return array Layouts directory |
90
|
|
|
*/ |
91
|
|
|
protected function getAllLayoutsPaths() |
92
|
|
|
{ |
93
|
|
|
$paths = []; |
94
|
|
|
|
95
|
|
|
// layouts/ |
96
|
|
|
if (is_dir($this->config->getLayoutsPath())) { |
97
|
|
|
$paths[] = $this->config->getLayoutsPath(); |
98
|
|
|
} |
99
|
|
|
// <theme>/layouts/ |
100
|
|
|
if ($this->config->hasTheme()) { |
101
|
|
|
$themes = $this->config->getTheme(); |
102
|
|
|
foreach ($themes as $theme) { |
103
|
|
|
$paths[] = $this->config->getThemeDirPath($theme); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
// res/layouts/ |
107
|
|
|
if (is_dir($this->config->getInternalLayoutsPath())) { |
108
|
|
|
$paths[] = $this->config->getInternalLayoutsPath(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $paths; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Add globals variables. |
116
|
|
|
*/ |
117
|
|
|
protected function addGlobals() |
118
|
|
|
{ |
119
|
|
|
// adds global variables |
120
|
|
|
$this->builder->getRenderer()->addGlobal('site', array_merge( |
121
|
|
|
$this->config->get('site'), |
122
|
|
|
['menus' => $this->builder->getMenus()], |
123
|
|
|
['pages' => $this->builder->getPages()->filter(function (Page $page) { |
124
|
|
|
return $page->getVariable('published'); |
125
|
|
|
})], |
126
|
|
|
['time' => time()] |
127
|
|
|
)); |
128
|
|
|
$this->builder->getRenderer()->addGlobal('cecil', [ |
129
|
|
|
'url' => sprintf('https://cecil.app/#%s', $this->builder->getVersion()), |
130
|
|
|
'version' => $this->builder->getVersion(), |
131
|
|
|
'poweredby' => sprintf('Cecil v%s', $this->builder->getVersion()), |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.