Completed
Push — master ( e15c58...b150a8 )
by Changwan
07:08
created

SkeletonBuilder::buildFile()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 16
nc 8
nop 2
dl 0
loc 23
ccs 21
cts 21
cp 1
crap 8
rs 6.1403
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Installation;
3
4
use DirectoryIterator;
5
use SplFileInfo;
6
use Wandu\Installation\Contracts\ReplacerInterface;
7
use Wandu\Installation\Replacers\CallableReplacer;
8
use Wandu\Installation\Replacers\StringReplacer;
9
10
class SkeletonBuilder
11
{
12
    /** @var string */
13
    protected $targetPath;
14
    
15
    /** @var string */
16
    protected $skeletonPath;
17
    
18
    /**
19
     * @param string $targetPath
20
     * @param string $skeletonPath
21
     */
22 1
    public function __construct($targetPath, $skeletonPath)
23
    {
24 1
        $this->targetPath = rtrim($targetPath, '/');
25 1
        $this->skeletonPath = realpath($skeletonPath);
26 1
    }
27
28
    /**
29
     * @param array $replacers
30
     */
31 1
    public function build(array $replacers = [])
32
    {
33 1
        $this->buildFile($this->skeletonPath, $this->normalizeReplacers($replacers));
34 1
    }
35
36
    /**
37
     * @param string $file
38
     * @param \Wandu\Installation\Contracts\ReplacerInterface[] $replaces
39
     */
40 1
    protected function buildFile($file, array $replaces = [])
41
    {
42 1
        $dest = str_replace($this->skeletonPath, $this->targetPath, $file);
43 1
        if (is_file($file)) {
44 1
            $contents = file_get_contents($file);
45 1
            foreach ($replaces as $matcher => $replacer) {
46 1
                $contents = $replacer->replace(
47 1
                    $contents,
48 1
                    $matcher,
49 1
                    file_exists($dest) ? new SplFileInfo($dest) : null
50 1
                );
51 1
            }
52 1
            file_put_contents($dest, $contents);
53 1
            return;
54
        }
55 1
        if (!is_dir($dest)) {
56 1
            mkdir($dest);
57 1
        }
58 1
        foreach (new DirectoryIterator($file) as $item) {
59 1
            if ($item->getFilename() === '.' || $item->getFilename() === '..') continue;
60 1
            $this->buildFile($item->getRealPath(), $replaces);
61 1
        }
62 1
    }
63
64
    /**
65
     * @param array $replacers
66
     * @return \Wandu\Installation\Contracts\ReplacerInterface[]
67
     */
68 1
    protected function normalizeReplacers(array $replacers = [])
69
    {
70 1
        foreach ($replacers as $matcher => $replacer) {
71 1
            if ($replacer instanceof ReplacerInterface) {
72 1
                continue;
73
            }
74 1
            if (is_callable($replacer)) {
75 1
                $replacers[$matcher] = new CallableReplacer($replacer);
76 1
            } else {
77 1
                $replacers[$matcher] = new StringReplacer($replacer);
78
            }
79 1
        }
80 1
        return $replacers;
81
    }
82
}
83