• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 5045835811

pending completion
5045835811

Pull #1697

github

GitHub
Merge c3e3c7443 into a16355c73
Pull Request #1697: perf: native_function_invocation

321 of 321 new or added lines in 62 files covered. (100.0%)

2784 of 4121 relevant lines covered (67.56%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.31
/src/Step/Pages/Save.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
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
    public function getName(): string
31
    {
32
        return 'Saving pages';
1✔
33
    }
34

35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function init(array $options): void
39
    {
40
        // should clear cache?
41
        $this->clearCache();
1✔
42

43
        if ($options['dry-run']) {
1✔
44
            return;
×
45
        }
46

47
        Util\File::getFS()->mkdir($this->config->getOutputPath());
1✔
48

49
        $this->canProcess = true;
1✔
50
    }
51

52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @throws RuntimeException
56
     */
57
    public function process(): void
58
    {
59
        $filteredPages = $this->builder->getPages()->filter(function (Page $page) {
1✔
60
            return !empty($page->getRendered());
1✔
61
        });
1✔
62
        $total = \count($filteredPages);
1✔
63

64
        $count = 0;
1✔
65
        foreach ($filteredPages as $page) {
1✔
66
            $count++;
1✔
67
            $files = [];
1✔
68

69
            foreach ($page->getRendered() as $format => $rendered) {
1✔
70
                if (false === $pathname = (new PageRenderer($this->config))->getOutputFile($page, $format)) {
1✔
71
                    throw new RuntimeException(sprintf("Can't get pathname of page '%s' (format: '%s')", $page->getId(), $format));
×
72
                }
73
                $pathname = $this->cleanPath(Util::joinFile($this->config->getOutputPath(), $pathname));
1✔
74

75
                try {
76
                    Util\File::getFS()->dumpFile($pathname, $rendered['output']);
1✔
77
                } catch (\Symfony\Component\Filesystem\Exception\IOException $e) {
×
78
                    throw new RuntimeException($e->getMessage());
×
79
                }
80

81
                $files[] = $this->builder->isDebug() ? $pathname : substr($pathname, \strlen($this->config->getDestinationDir()) + 1);
1✔
82
            }
83

84
            $message = sprintf('File(s) "%s" saved', implode(', ', $files));
1✔
85
            $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
1✔
86
        }
87
    }
88

89
    /**
90
     * Removes unnecessary directory separators.
91
     */
92
    private function cleanPath(string $pathname): string
93
    {
94
        if (DIRECTORY_SEPARATOR == '\\') {
1✔
95
            $pathname = preg_replace('#\\\\+#', '\\', $pathname);
×
96
        }
97

98
        return preg_replace('#/+#', '/', $pathname);
1✔
99
    }
100

101
    /**
102
     * Deletes cache directory.
103
     */
104
    private function clearCache(): void
105
    {
106
        if ($this->config->get('cache.enabled') === false) {
1✔
107
            Util\File::getFS()->remove($this->config->getCachePath());
×
108
        }
109
    }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc