Test Failed
Pull Request — master (#9)
by Yo
02:48 queued 55s
created

TemplateHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
namespace Yoanm\DefaultPhpRepository\Helper;
3
4
use Symfony\Component\Filesystem\Filesystem;
5
use Yoanm\DefaultPhpRepository\Factory\VarFactory;
6
use Yoanm\DefaultPhpRepository\Registry\TemplateRegistry;
7
use Yoanm\DefaultPhpRepository\Resolver\NamespaceResolver;
8
9
/**
10
 * Class TemplateFileProcessor
11
 */
12
class TemplateHelper
13
{
14
    /** @var \Twig_Environment */
15
    private $twig;
16
    /** @var Filesystem */
17
    private $fileSystem;
18
    /** @var string */
19
    private static $templateBasePath = null;
20
21
    /**
22
     * @param \Twig_Environment $twig
23
     * @param array             $varList
24
     */
25
    public function __construct(\Twig_Environment $twig, array $varList)
26
    {
27
        $this->twig = $twig;
28
        $this->fileSystem = new Filesystem();
29
30
        $this->initTwig($varList);
31
    }
32
33
    /**
34
     * @param string $template
35
     * @param string $outputFilePath
36
     */
37
    public function dumpTemplate($template, $outputFilePath)
38
    {
39
        $this->fileSystem->dumpFile(
40
            $outputFilePath,
41
            $this->twig->render($template)
42
        );
43
    }
44
45
    public static function getTemplateBasePath()
46
    {
47
        if (null === self::$templateBasePath) {
48
            self::$templateBasePath = realpath(sprintf('%s/../../../../templates', __DIR__));
49
        }
50
        return self::$templateBasePath;
51
    }
52
53
    /**
54
     * @param array $varList
55
     *
56
     * @throws \Twig_Error_Loader
57
     */
58
    private function initTwig(array $varList)
59
    {
60
        $loader = new \Twig_Loader_Filesystem();
61
        $this->twig->setLoader($loader);
62
63
        // Set template namespaces
64
        $loader->addPath(sprintf('%s/%s', self::getTemplateBasePath(), 'base'));
65
        $loader->addPath(
66
            sprintf('%s/%s', self::getTemplateBasePath(), '/override/library/php'),
67
            NamespaceResolver::LIBRARY_NAMESPACE
68
        );
69
        $loader->addPath(
70
            sprintf('%s/%s', self::getTemplateBasePath(), '/override/library/symfony'),
71
            NamespaceResolver::SYMFONY_LIBRARY_NAMESPACE
72
        );
73
74
        // define variable as global
75
        $twigVarList = [];
76
        // merge keys as array
77
        foreach ($varList as $varName => $varValue) {
78
            $twigVarList = array_merge_recursive($twigVarList, $this->resolveVar($varName, $varValue));
79
        }
80
        foreach ($twigVarList as $key => $val) {
81
            $this->twig->addGlobal($key, $val);
82
        }
83
    }
84
85
    /**
86
     * @param string $varName
87
     * @param string $varValue
88
     *
89
     * @return array
90
     */
91
    protected function resolveVar($varName, $varValue)
92
    {
93
        $componentList = explode('.', $varName);
94
        if (count($componentList) > 1) {
95
            $varName = array_shift($componentList);
96
            $varValue = $this->resolveVar(implode('.', $componentList), $varValue);
97
        }
98
99
        return [$varName => $varValue];
100
    }
101
}
102