|
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\Util; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Pages saving. |
|
17
|
|
|
*/ |
|
18
|
|
|
class PagesSave extends AbstractStep |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
* |
|
23
|
|
|
* @throws Exception |
|
24
|
|
|
*/ |
|
25
|
|
|
public function init($options) |
|
26
|
|
|
{ |
|
27
|
|
|
if ($options['dry-run']) { |
|
28
|
|
|
$this->process = false; |
|
29
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['SAVE', 'Dry run']); |
|
30
|
|
|
|
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
Util::getFS()->mkdir($this->config->getOutputPath()); |
|
35
|
|
|
|
|
36
|
|
|
$this->process = true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
* |
|
42
|
|
|
* @throws Exception |
|
43
|
|
|
*/ |
|
44
|
|
|
public function process() |
|
45
|
|
|
{ |
|
46
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['SAVE', 'Saving pages']); |
|
47
|
|
|
|
|
48
|
|
|
/* @var $page Page */ |
|
49
|
|
|
$filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
|
50
|
|
|
return !empty($page->getVariable('rendered')); |
|
51
|
|
|
}); |
|
52
|
|
|
$max = count($filteredPages); |
|
53
|
|
|
|
|
54
|
|
|
$count = 0; |
|
55
|
|
|
foreach ($filteredPages as $page) { |
|
56
|
|
|
$count++; |
|
57
|
|
|
$message = []; |
|
58
|
|
|
|
|
59
|
|
|
foreach ($page->getVariable('rendered') as $format => $rendered) { |
|
60
|
|
|
if (false === $pathname = $page->getOutputFile($format, $this->config)) { |
|
61
|
|
|
throw new Exception(sprintf( |
|
62
|
|
|
"Can't get pathname of page '%s' (format: '%s')", |
|
63
|
|
|
$page->getId(), |
|
64
|
|
|
$format |
|
65
|
|
|
)); |
|
66
|
|
|
continue; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
$pathname = $this->cleanPath($this->config->getOutputPath().'/'.$pathname); |
|
69
|
|
|
|
|
70
|
|
|
try { |
|
71
|
|
|
Util::getFS()->dumpFile($pathname, $rendered['output']); |
|
72
|
|
|
} catch (\Exception $e) { |
|
73
|
|
|
throw new Exception($e->getMessage()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$message[] = substr($pathname, strlen($this->config->getDestinationDir()) + 1); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
call_user_func_array( |
|
80
|
|
|
$this->builder->getMessageCb(), |
|
81
|
|
|
['SAVE_PROGRESS', implode(', ', $message), $count, $max] |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Remove unnecessary slashes. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $pathname |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function cleanPath($pathname) |
|
94
|
|
|
{ |
|
95
|
|
|
return preg_replace('#/+#', '/', $pathname); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.