Passed
Push — master ( e65f7a...b84038 )
by
unknown
06:29
created

Copy::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
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\StaticFiles;
15
16
use Cecil\Exception\RuntimeException;
17
use Cecil\Step\AbstractStep;
18
use Cecil\Util;
19
use Symfony\Component\Finder\Finder;
20
21
/**
22
 * Copying static files to site root.
23
 */
24
class Copy extends AbstractStep
25
{
26
    protected $count = 0;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function getName(): string
32
    {
33 1
        return 'Copying static';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function init(array $options): void
40
    {
41 1
        if ($options['dry-run']) {
42
            return;
43
        }
44
45
        // reset output directory
46 1
        Util\File::getFS()->remove($this->config->getOutputPath());
47 1
        Util\File::getFS()->mkdir($this->config->getOutputPath());
48
49 1
        $this->canProcess = true;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function process(): void
56
    {
57 1
        $target = $this->config->get('static.target');
58 1
        $exclude = $this->config->get('static.exclude');
59
60
        // copying assets in debug mode (for source maps)
61 1
        if ($this->builder->isDebug() && (bool) $this->config->get('assets.compile.sourcemap')) {
62
            // copying content of '<theme>/assets/' dir if exists
63
            if ($this->config->hasTheme()) {
64
                $themes = array_reverse($this->config->getTheme());
65
                foreach ($themes as $theme) {
66
                    $this->copy($this->config->getThemeDirPath($theme, 'assets'));
67
                }
68
            }
69
            // copying content of 'assets/' dir if exists
70
            $this->copy($this->config->getAssetsPath());
71
            // cancel exclusion for static files (see below)
72
            $exclude = [];
73
        }
74
75
        // copying content of '<theme>/static/' dir if exists
76 1
        if ($this->config->hasTheme()) {
77 1
            $themes = array_reverse($this->config->getTheme());
78 1
            foreach ($themes as $theme) {
79 1
                $this->copy($this->config->getThemeDirPath($theme, 'static'), $target, $exclude);
80
            }
81
        }
82
83
        // copying content of 'static/' dir if exists
84 1
        $this->copy($this->config->getStaticPath(), $target, $exclude);
85
86
        // copying mounts
87 1
        if ($this->config->get('static.mounts')) {
88 1
            foreach ($this->config->get('static.mounts') as $source => $destination) {
89 1
                $this->copy(Util::joinFile($this->config->getStaticPath(), (string) $source), (string) $destination);
90
            }
91
        }
92
93 1
        if ($this->count === 0) {
94
            $this->builder->getLogger()->info('Nothing to copy');
95
96
            return;
97
        }
98 1
        $this->builder->getLogger()->info('Files copied', ['progress' => [$this->count, $this->count]]);
99
    }
100
101
    /**
102
     * Copying a file or files in a directory from $from (if exists) to $to (relative to output path).
103
     * Exclude files or directories with $exclude array.
104
     */
105 1
    protected function copy(string $from, ?string $to = null, ?array $exclude = null): void
106
    {
107
        try {
108 1
            if (Util\File::getFS()->exists($from)) {
109
                // copy a file
110 1
                if (is_file($from)) {
111 1
                    Util\File::getFS()->copy($from, Util::joinFile($this->config->getOutputPath(), $to), true);
0 ignored issues
show
Bug introduced by
It seems like $to can also be of type null; however, parameter $path of Cecil\Util::joinFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
                    Util\File::getFS()->copy($from, Util::joinFile($this->config->getOutputPath(), /** @scrutinizer ignore-type */ $to), true);
Loading history...
112
113 1
                    return;
114
                }
115
                // copy a directory
116 1
                $finder = Finder::create()
117 1
                    ->files()
118 1
                    ->in($from)
119 1
                    ->ignoreDotFiles(false);
120
                // exclude files or directories
121 1
                if (\is_array($exclude)) {
122 1
                    $finder->notPath($exclude);
123 1
                    $finder->notName($exclude);
124
                }
125 1
                $this->count += $finder->count();
126 1
                Util\File::getFS()->mirror(
127 1
                    $from,
128 1
                    Util::joinFile($this->config->getOutputPath(), $to ?? ''),
129 1
                    $finder,
130 1
                    ['override' => true]
131 1
                );
132
            }
133
        } catch (\Exception $e) {
134
            throw new RuntimeException(\sprintf('Error during static files copy: %s', $e->getMessage()));
135
        }
136
    }
137
}
138