Completed
Push — include-lib ( fd24a6...4173d3 )
by Arnaud
13:24
created

CopyStatic::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 CopyStatic extends AbstractStep
18
{
19
    /**
20
     * {@inheritdoc}
21
     *
22
     * @throws Exception
23
     */
24
    public function init($options)
25
    {
26
        // clean before
27
        Util::getFS()->remove($this->builder->getConfig()->getOutputPath());
28
29
        $this->process = true;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     *
35
     * @throws \Cecil\Exception\Exception
36
     */
37
    public function process()
38
    {
39
        $count = 0;
40
41
        call_user_func_array($this->builder->getMessageCb(), ['COPY', 'Copying static files']);
42
        // copy <theme>/static/ dir if exists
43
        if ($this->builder->getConfig()->hasTheme()) {
44
            $themes = array_reverse($this->builder->getConfig()->getTheme());
45
            foreach ($themes as $theme) {
46
                $themeStaticDir = $this->builder->getConfig()->getThemeDirPath($theme, 'static');
47
                if (Util::getFS()->exists($themeStaticDir)) {
48
                    $finder = new Finder();
49
                    $finder->files()->in($themeStaticDir);
50
                    $count += $finder->count();
51
                    Util::getFS()->mirror(
52
                        $themeStaticDir,
53
                        $this->builder->getConfig()->getOutputPath(),
54
                        null,
55
                        ['override' => true]
56
                    );
57
                }
58
            }
59
        }
60
        // copy static/ dir if exists
61
        $staticDir = $this->builder->getConfig()->getStaticPath();
62
        if (Util::getFS()->exists($staticDir)) {
63
            $finder = new Finder();
64
            $finder->files()->filter(function (\SplFileInfo $file) {
65
                return !(is_array($this->builder->getConfig()->get('static.exclude'))
66
                    && in_array($file->getBasename(), $this->builder->getConfig()->get('static.exclude')));
67
            })->in($staticDir);
68
            $count += $finder->count();
69
            Util::getFS()->mirror(
70
                $staticDir,
71
                $this->builder->getConfig()->getOutputPath(),
72
                $finder,
73
                ['override' => true]
74
            );
75
        }
76
        call_user_func_array($this->builder->getMessageCb(), ['COPY_PROGRESS', 'Start copy', 0, $count]);
77
        call_user_func_array($this->builder->getMessageCb(), ['COPY_PROGRESS', 'Files copied', $count, $count]);
78
    }
79
}
80