|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is, guess what, part of WebHelper. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) James <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace JamesRezo\WebHelper; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Finder\Finder; |
|
15
|
|
|
use Composer\Semver\VersionParser; |
|
16
|
|
|
use Composer\Semver\Comparator; |
|
17
|
|
|
use \Twig_Loader_Filesystem; |
|
18
|
|
|
use \Twig_Environment; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* WebHelper. |
|
22
|
|
|
*/ |
|
23
|
|
|
class WebHelper |
|
24
|
|
|
{ |
|
25
|
|
|
private $finder; |
|
26
|
|
|
|
|
27
|
|
|
private $versionParser; |
|
28
|
|
|
|
|
29
|
|
|
private $memoize = []; |
|
30
|
|
|
|
|
31
|
|
|
private $twig; |
|
32
|
|
|
|
|
33
|
|
|
private $server; |
|
34
|
|
|
|
|
35
|
|
|
private $version; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->finder = new Finder(); |
|
40
|
|
|
$this->versionParser = new VersionParser(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function initialize($resDir) |
|
44
|
|
|
{ |
|
45
|
|
|
$loader = new Twig_Loader_Filesystem($resDir); |
|
46
|
|
|
$twig = new Twig_Environment($loader, array( |
|
47
|
|
|
'cache' => __DIR__ . '/../var/cache', |
|
48
|
|
|
)); |
|
49
|
|
|
|
|
50
|
|
|
return $twig; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function memoize($resDir) |
|
54
|
|
|
{ |
|
55
|
|
|
$memoize = []; |
|
56
|
|
|
$this->finder->files()->name('*.twig')->in($resDir); |
|
57
|
|
|
|
|
58
|
|
|
foreach ($this->finder as $file) { |
|
59
|
|
|
$parsedPath = explode('/', $file->getRelativePathname()); |
|
60
|
|
|
if (count($parsedPath) == 2) { |
|
61
|
|
|
$parsedPath[2] = $parsedPath[1]; |
|
62
|
|
|
$parsedPath[1] = 0; |
|
63
|
|
|
} |
|
64
|
|
|
$parsedPath[2] = str_replace('.twig', '', $parsedPath[2]); |
|
65
|
|
|
$memoize[$parsedPath[0]] |
|
66
|
|
|
[$this->versionParser->normalize($parsedPath[1])] |
|
67
|
|
|
[$parsedPath[2]] = $file->getRelativePathname(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $memoize; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function setTwig($resDir = '') |
|
74
|
|
|
{ |
|
75
|
|
|
$this->twig = $resDir == '' ? null : $this->initialize($resDir); |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getTwig() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->twig; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function setMemoize($resDir = '') |
|
86
|
|
|
{ |
|
87
|
|
|
$this->memoize = $resDir == '' ? [] : $this->memoize($resDir); |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getMemoize() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->memoize; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function setServer($server) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->server = $server; |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getServer() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->server; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function setVersion($version) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->version = $this->versionParser->normalize($version); |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getVersion() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->version; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function find($directive) |
|
122
|
|
|
{ |
|
123
|
|
|
$return = ''; |
|
124
|
|
|
$versions = array_keys($this->getMemoize()[$this->getServer()]); |
|
125
|
|
|
sort($versions); |
|
126
|
|
|
foreach ($versions as $version) { |
|
127
|
|
|
if (Comparator::greaterThanOrEqualTo($this->getVersion(), $version) && |
|
128
|
|
|
array_key_exists($directive, $this->memoize[$this->getServer()][$version]) |
|
129
|
|
|
) { |
|
130
|
|
|
$return = $this->memoize[$this->getServer()][$version][$directive]; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function render($twigFile, array $params = array()) |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->twig->render($twigFile, $params); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|