Completed
Push — code ( eb95c2 )
by Arnaud
02:14
created

StaticCopy::process()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.6257
c 0
b 0
f 0
cc 6
nc 4
nop 0
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Step;
10
11
use Cecil\Util;
12
use Symfony\Component\Finder\Finder;
13
14
/**
15
 * Copy static directory content to site root.
16
 */
17
class StaticCopy extends AbstractStep
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function init($options)
23
    {
24
        // clean before
25
        Util::getFS()->remove($this->builder->getConfig()->getOutputPath());
26
27
        $this->process = true;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function process()
34
    {
35
        $count = 0;
36
37
        call_user_func_array($this->builder->getMessageCb(), ['COPY', 'Copying static files']);
38
        // copy <theme>/static/ dir if exists
39
        if ($this->builder->getConfig()->hasTheme()) {
40
            $themes = array_reverse($this->builder->getConfig()->getTheme());
41
            foreach ($themes as $theme) {
42
                $themeStaticDir = $this->builder->getConfig()->getThemeDirPath($theme, 'static');
43
                if (Util::getFS()->exists($themeStaticDir)) {
44
                    $finder = new Finder();
45
                    $finder->files()->in($themeStaticDir);
46
                    $count += $finder->count();
47
                    Util::getFS()->mirror(
48
                        $themeStaticDir,
49
                        $this->builder->getConfig()->getOutputPath(),
50
                        null,
51
                        ['override' => true]
52
                    );
53
                }
54
            }
55
        }
56
        // copy static/ dir if exists
57
        $staticDir = $this->builder->getConfig()->getStaticPath();
58
        if (Util::getFS()->exists($staticDir)) {
59
            $finder = new Finder();
60
            $finder->files()->filter(function (\SplFileInfo $file) {
61
                return !(is_array($this->builder->getConfig()->get('static.exclude'))
62
                    && in_array($file->getBasename(), $this->builder->getConfig()->get('static.exclude')));
63
            })->in($staticDir);
64
            $count += $finder->count();
65
            Util::getFS()->mirror(
66
                $staticDir,
67
                $this->builder->getConfig()->getOutputPath(),
68
                $finder,
69
                ['override' => true]
70
            );
71
        }
72
        call_user_func_array($this->builder->getMessageCb(), ['COPY_PROGRESS', 'Start copy', 0, $count]);
73
        call_user_func_array($this->builder->getMessageCb(), ['COPY_PROGRESS', 'Files copied', $count, $count]);
74
    }
75
}
76