Passed
Pull Request — master (#2103)
by Arnaud
05:45
created

Copy   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 76.67%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 54
dl 0
loc 123
ccs 46
cts 60
cp 0.7667
rs 10
c 2
b 1
f 0
wmc 19

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A init() 0 11 2
C process() 0 51 12
A copy() 0 35 4
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\Step\AbstractStep;
17
use Cecil\Util;
18
use Symfony\Component\Finder\Finder;
19
20
/**
21
 * Copying static files to site root.
22
 */
23
class Copy extends AbstractStep
24
{
25
    protected $count = 0;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public function getName(): string
31
    {
32 1
        return 'Copying static';
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
        // reset output directory
45 1
        Util\File::getFS()->remove($this->config->getOutputPath());
46 1
        Util\File::getFS()->mkdir($this->config->getOutputPath());
47
48 1
        $this->canProcess = true;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function process(): void
55
    {
56 1
        $target = $this->config->get('static.target');
57 1
        $exclude = $this->config->get('static.exclude');
58
59
        // abord exclude if in debug mode + sourcemap
60 1
        if ($this->builder->isDebug() && (bool) $this->config->get('assets.compile.sourcemap')) {
61
            $exclude = [];
62
        }
63
64
        // copying content of '<theme>/static/' dir if exists
65 1
        if ($this->config->hasTheme()) {
66 1
            $themes = array_reverse($this->config->getTheme());
67 1
            foreach ($themes as $theme) {
68 1
                $this->copy($this->config->getThemeDirPath($theme, 'static'), $target, $exclude);
69
            }
70
        }
71
72
        // copying mounts
73 1
        if ($this->config->get('static.mounts')) {
74 1
            foreach ($this->config->get('static.mounts') as $source => $destination) {
75 1
                $this->copy(Util::joinFile($this->config->getStaticPath(), (string) $source), (string) $destination);
76
            }
77
        }
78
79
        // copying content of 'static/' dir if exists
80 1
        $this->copy($this->config->getStaticPath(), $target, $exclude);
81
82
        // copying assets in debug mode (for source maps)
83 1
        if ($this->builder->isDebug() && (bool) $this->config->get('assets.compile.sourcemap')) {
84
            // copying content of '<theme>/assets/' dir if exists
85
            if ($this->config->hasTheme()) {
86
                $themes = array_reverse($this->config->getTheme());
87
                foreach ($themes as $theme) {
88
                    $this->copy(
89
                        $this->config->getThemeDirPath($theme, 'assets')
90
                    );
91
                }
92
            }
93
            // copying content of 'assets/' dir if exists
94
            $this->copy(
95
                $this->config->getAssetsPath()
96
            );
97
        }
98
99 1
        if ($this->count === 0) {
100
            $this->builder->getLogger()->info('Nothing to copy');
101
102
            return;
103
        }
104 1
        $this->builder->getLogger()->info('Files copied', ['progress' => [$this->count, $this->count]]);
105
    }
106
107
    /**
108
     * Copying a file or files in a directory from $from to $to (relative to output path).
109
     * Exclude files or directories with $exclude array.
110
     */
111 1
    protected function copy(string $from, ?string $to = null, ?array $exclude = null): bool
112
    {
113 1
        if (Util\File::getFS()->exists($from)) {
114
            // copy a file
115 1
            if (is_file($from)) {
116 1
                Util\File::getFS()->copy(
117 1
                    $from,
118 1
                    Util::joinFile($this->config->getOutputPath(), $to ?? ''),
119 1
                    true
120 1
                );
121
122 1
                return true;
123
            }
124
            // copy a directory
125 1
            $finder = Finder::create()
126 1
                ->files()
127 1
                ->in($from)
128 1
                ->ignoreDotFiles(false);
129
            // exclude files or directories
130 1
            if (\is_array($exclude)) {
131 1
                $finder->notPath($exclude);
132 1
                $finder->notName($exclude);
133
            }
134 1
            $this->count += $finder->count();
135 1
            Util\File::getFS()->mirror(
136 1
                $from,
137 1
                Util::joinFile($this->config->getOutputPath(), $to ?? ''),
138 1
                $finder,
139 1
                ['override' => true]
140 1
            );
141
142 1
            return true;
143
        }
144
145
        return false;
146
    }
147
}
148