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