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

TemplateHelper::loadTemplate()   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 = '#/?(?:[^/]+/)*([^/]+)\.tmpl$#';
41
42
        $this->fs = new Filesystem();
43
    }
44
45
    /**
46
     * @param string $templateFilePath
47
     * @param string $outputFilePath
48
     *
49
     * @throws TargetFileExistsException
50
     */
51
    public function dumpTemplate($templateFilePath, $outputFilePath)
52
    {
53
        $this->fs->dumpFile($outputFilePath, $this->loadTemplate($templateFilePath));
54
    }
55
56
    /**
57
     * @param string $templateFilePath
58
     * @param string $outputDir
59
     *
60
     * @return string
61
     */
62
    public function resolveOutputFilePath($templateFilePath, $outputDir)
63
    {
64
        return sprintf(
65
            '%s%s',
66
            PathHelper::appendPathSeparator($outputDir),
67
            $this->resolveOutputFilename($templateFilePath)
68
        );
69
    }
70
71
    /**
72
     * @param string $templateFilePath
73
     *
74
     * @return string
75
     */
76
    public function resolveOutputFilename($templateFilePath)
77
    {
78
        return preg_replace($this->filenameResolverRegexp, '\1', $templateFilePath);
79
    }
80
81
    /**
82
     * @param string $templateFilePath
83
     *
84
     * @return string file content
85
     */
86
    public function loadTemplate($templateFilePath)
87
    {
88
        return str_replace($this->variableNameList, $this->variableList, file_get_contents($templateFilePath));
89
    }
90
}
91