|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Cecil. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Cecil\Step\Pages; |
|
15
|
|
|
|
|
16
|
|
|
use Cecil\Collection\Page\Page; |
|
17
|
|
|
use Cecil\Exception\RuntimeException; |
|
18
|
|
|
use Cecil\Renderer\Page as PageRenderer; |
|
19
|
|
|
use Cecil\Step\AbstractStep; |
|
20
|
|
|
use Cecil\Util; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Pages saving. |
|
24
|
|
|
*/ |
|
25
|
|
|
class Save extends AbstractStep |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function getName(): string |
|
31
|
|
|
{ |
|
32
|
1 |
|
return 'Saving pages'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function init(array $options): void |
|
39
|
|
|
{ |
|
40
|
1 |
|
if ($options['dry-run']) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
Util\File::getFS()->mkdir($this->config->getOutputPath()); |
|
45
|
|
|
|
|
46
|
1 |
|
$this->canProcess = true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
* |
|
52
|
|
|
* @throws RuntimeException |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function process(): void |
|
55
|
|
|
{ |
|
56
|
1 |
|
$filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
|
57
|
1 |
|
return !empty($page->getRendered()); |
|
58
|
1 |
|
}); |
|
59
|
1 |
|
$total = \count($filteredPages); |
|
60
|
|
|
|
|
61
|
1 |
|
$count = 0; |
|
62
|
1 |
|
foreach ($filteredPages as $page) { |
|
63
|
1 |
|
$count++; |
|
64
|
1 |
|
$files = []; |
|
65
|
|
|
|
|
66
|
1 |
|
foreach ($page->getRendered() as $format => $rendered) { |
|
67
|
1 |
|
if (false === $pathname = (new PageRenderer($this->config))->getOutputFilePath($page, $format)) { |
|
68
|
|
|
throw new RuntimeException(\sprintf("Can't get pathname of page '%s' (format: '%s').", $page->getId(), $format)); |
|
69
|
|
|
} |
|
70
|
1 |
|
$pathname = $this->cleanPath(Util::joinFile($this->config->getOutputPath(), $pathname)); |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
1 |
|
Util\File::getFS()->dumpFile($pathname, $rendered['output']); |
|
74
|
|
|
} catch (\Symfony\Component\Filesystem\Exception\IOException $e) { |
|
75
|
|
|
throw new RuntimeException($e->getMessage()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
$files[] = $this->builder->isDebug() ? $pathname : substr($pathname, \strlen($this->config->getDestinationDir()) + 1); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$message = \sprintf('File(s) "%s" saved', implode(', ', $files)); |
|
82
|
1 |
|
$this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Removes unnecessary directory separators. |
|
88
|
|
|
*/ |
|
89
|
1 |
|
private function cleanPath(string $pathname): string |
|
90
|
|
|
{ |
|
91
|
1 |
|
if (DIRECTORY_SEPARATOR == '\\') { |
|
92
|
|
|
$pathname = preg_replace('#\\\\+#', '\\', $pathname); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
return preg_replace('#/+#', '/', $pathname); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|