Failed Conditions
Pull Request — master (#1)
by Yo
01:56
created

TemplateHelper::resolveOutputFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Yoanm\DefaultPhpRepository\Helper;
3
4
use Symfony\Component\Filesystem\Filesystem;
5
use Yoanm\DefaultPhpRepository\Exception\TargetFileExistsException;
6
7
/**
8
 * Class TemplateFileProcessor
9
 */
10
class TemplateHelper
11
{
12
    /** @var array */
13
    private $variableList = [];
14
    /** @var string[] */
15
    private $variableNameList = [];
16
    /** @var string */
17
    private $filenameResolverRegexp;
18
    /** @var Filesystem */
19
    private $fs;
20
21
    /**
22
     * @param array $variableList
23
     * @param array $extraTemplatePath
24
     */
25
    public function __construct(array $variableList, array $extraTemplatePath = [])
26
    {
27
        $this->variableList = $variableList;
28
        foreach ($this->variableList as $variableId => $variableValue) {
29
            $variableId = sprintf('%%%s%%', $variableId);
30
            $this->variableNameList[$variableId] = $variableId;
31
        }
32
33
        foreach ($extraTemplatePath as $extraKey => $extraValue) {
34
            $variableId = sprintf('%%%s%%', $extraKey);
35
            $this->variableNameList[$variableId] = $variableId;
36
            $this->variableList[$variableId] = $this->loadTemplate($extraValue);
37
        }
38
39
        // compile this regexp at startup (no need to to it each time)
40
        $this->filenameResolverRegexp = sprintf(
41
            '#%s?(?:[^%s]+%s)*([^%s]+)\.tmpl$#',
42
            PathHelper::separator(),
43
            PathHelper::separator(),
44
            PathHelper::separator(),
45
            PathHelper::separator()
46
        );
47
48
        $this->fs = new Filesystem();
49
    }
50
51
    /**
52
     * @param string $templateFilePath
53
     * @param string $outputFilePath
54
     *
55
     * @throws TargetFileExistsException
56
     */
57
    public function dumpTemplate($templateFilePath, $outputFilePath)
58
    {
59
        $this->fs->dumpFile($outputFilePath, $this->loadTemplate($templateFilePath));
60
    }
61
62
    /**
63
     * @param string $templateFilePath
64
     * @param string $outputDir
65
     *
66
     * @return string
67
     */
68
    public function resolveOutputFilePath($templateFilePath, $outputDir)
69
    {
70
        return sprintf(
71
            '%s%s',
72
            PathHelper::appendPathSeparator($outputDir),
73
            $this->resolveOutputFilename($templateFilePath)
74
        );
75
    }
76
77
    /**
78
     * @param string $templateFilePath
79
     *
80
     * @return string
81
     */
82
    public function resolveOutputFilename($templateFilePath)
83
    {
84
        return preg_replace($this->filenameResolverRegexp, '\1', $templateFilePath);
85
    }
86
87
    /**
88
     * @param string $templateFilePath
89
     *
90
     * @return string file content
91
     */
92
    public function loadTemplate($templateFilePath)
93
    {
94
        return str_replace($this->variableNameList, $this->variableList, file_get_contents($templateFilePath));
95
    }
96
}
97