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
|
|
|
|
58
|
|
|
$pathname = $this->cleanPath($this->config->getOutputPath().'/'.$this->getPathname($page)); |
59
|
|
|
|
60
|
|
|
Util::getFS()->dumpFile($pathname, $page->getVariable('rendered')); |
61
|
|
|
|
62
|
|
|
$message = substr($pathname, strlen($this->config->getDestinationDir()) + 1); |
63
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['SAVE_PROGRESS', $message, $count, $max]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return output pathname. |
69
|
|
|
* |
70
|
|
|
* @param Page $page |
71
|
|
|
* |
72
|
|
|
* @return string |
|
|
|
|
73
|
|
|
*/ |
74
|
|
|
protected function getPathname(Page $page) |
75
|
|
|
{ |
76
|
|
|
// force pathname of "index" pages (ie: homepage, sections, etc.) |
77
|
|
|
if ($page->getName() == 'index') { |
78
|
|
|
return $page->getPath().'/'.$this->config->get('output.filename'); |
79
|
|
|
} else { |
80
|
|
|
// custom extension, ex: 'manifest.json' |
81
|
|
|
if (!empty(pathinfo($page->getPermalink(), PATHINFO_EXTENSION))) { |
82
|
|
|
return $page->getPermalink(); |
83
|
|
|
} |
84
|
|
|
// underscore prefix, ex: '_redirects' |
85
|
|
|
if (strpos($page->getPermalink(), '_') === 0) { |
86
|
|
|
return $page->getPermalink(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $page->getPermalink().'/'.$this->config->get('output.filename'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Remove unnecessary slashes. |
95
|
|
|
* |
96
|
|
|
* @param string $pathname |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
protected function cleanPath($pathname) |
101
|
|
|
{ |
102
|
|
|
return preg_replace('#/+#', '/', $pathname); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.