Test Failed
Pull Request — master (#9)
by Yo
02:00
created

TemplateHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 94
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

5 Methods

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