Passed
Pull Request — assets (#772)
by Arnaud
08:47 queued 05:50
created

PagesSave::clearCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Step;
12
13
use Cecil\Collection\Page\Page;
14
use Cecil\Exception\Exception;
15
use Cecil\Util;
16
17
/**
18
 * Pages saving.
19
 */
20
class PagesSave extends AbstractStep
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getName(): string
26
    {
27
        return 'Saving pages';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @throws Exception
34
     */
35
    public function init($options)
36
    {
37
        if ($options['dry-run']) {
38
            $this->canProcess = false;
39
40
            return;
41
        }
42
43
        Util::getFS()->mkdir($this->config->getOutputPath());
44
45
        $this->canProcess = true;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @throws Exception
52
     */
53
    public function process()
54
    {
55
        /** @var Page $page */
56
        $filteredPages = $this->builder->getPages()->filter(function (Page $page) {
57
            return !empty($page->getVariable('rendered'));
58
        });
59
        $max = count($filteredPages);
60
61
        $count = 0;
62
        foreach ($filteredPages as $page) {
63
            $count++;
64
            $message = [];
65
66
            foreach ($page->getVariable('rendered') as $format => $rendered) {
67
                if (false === $pathname = $page->getOutputFile($format, $this->config)) {
68
                    throw new Exception(sprintf(
69
                        "Can't get pathname of page '%s' (format: '%s')",
70
                        $page->getId(),
71
                        $format
72
                    ));
73
                }
74
                $pathname = $this->cleanPath(Util::joinFile($this->config->getOutputPath(), $pathname));
75
76
                try {
77
                    Util::getFS()->dumpFile($pathname, $rendered['output']);
78
                } catch (\Exception $e) {
79
                    throw new Exception($e->getMessage());
80
                }
81
82
                $message[] = substr($pathname, strlen($this->config->getDestinationDir()) + 1);
83
            }
84
85
            $message = implode(', ', $message);
86
            $this->builder->getLogger()->info($message, ['progress' => [$count, $max]]);
87
        }
88
89
        // clear cache
90
        $this->clearCache();
91
    }
92
93
    /**
94
     * Removes unnecessary directory separators.
95
     *
96
     * @param string $pathname
97
     *
98
     * @return string
99
     */
100
    private function cleanPath($pathname): string
101
    {
102
        if (DIRECTORY_SEPARATOR == '\\') {
103
            $pathname = preg_replace('#\\\\+#', '\\', $pathname);
104
        }
105
106
        return preg_replace('#/+#', '/', $pathname);
107
    }
108
109
    /**
110
     * Deletes cache directory.
111
     *
112
     * @return void
113
     */
114
    private function clearCache(): void
115
    {
116
        if ($this->config->get('cache.enabled') === false) {
117
            Util::getFS()->remove($this->config->getCachePath());
118
        }
119
    }
120
}
121