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
|
|
|
|
73
|
|
|
// define variable as global |
74
|
|
|
$twigVarList = []; |
75
|
|
|
// merge keys as array |
76
|
|
|
foreach ($varList as $varName => $varValue) { |
77
|
|
|
$twigVarList = array_merge_recursive($twigVarList, $this->resolveVar($varName, $varValue)); |
78
|
|
|
} |
79
|
|
|
foreach ($twigVarList as $key => $val) { |
80
|
|
|
$this->twig->addGlobal($key, $val); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $varName |
86
|
|
|
* @param string $varValue |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function resolveVar($varName, $varValue) |
91
|
|
|
{ |
92
|
|
|
$componentList = explode('.', $varName); |
93
|
|
|
if (count($componentList) > 1) { |
94
|
|
|
$varName = array_shift($componentList); |
95
|
|
|
$varValue = $this->resolveVar(implode('.', $componentList), $varValue); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return [$varName => $varValue]; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|