|
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 Composer\Semver\VersionParser; |
|
15
|
|
|
use Composer\Semver\Comparator; |
|
16
|
|
|
use JamesRezo\WebHelper\Factory; |
|
17
|
|
|
use JamesRezo\WebHelper\WebHelperRepository; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* WebHelper. |
|
21
|
|
|
*/ |
|
22
|
|
|
class WebHelper |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var WebHelperRepository a Repository instance */ |
|
25
|
|
|
private $repository; |
|
26
|
|
|
|
|
27
|
|
|
/** @var VersionParser a VersionParser instance */ |
|
28
|
|
|
private $versionParser; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Comparator a Comparator instance */ |
|
31
|
|
|
private $comparator; |
|
32
|
|
|
|
|
33
|
|
|
/** @var WebServer\WebServerInterface a Web Server instance */ |
|
34
|
|
|
private $server; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Base constructor. |
|
38
|
|
|
*/ |
|
39
|
5 |
|
public function __construct() |
|
40
|
|
|
{ |
|
41
|
5 |
|
$this->versionParser = new VersionParser(); |
|
42
|
5 |
|
$this->comparator = new Comparator(); |
|
43
|
5 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Sets the Repository Instance. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $resDir a Path of a Directives Repository |
|
49
|
|
|
*/ |
|
50
|
5 |
|
public function setRepository($resDir = '') |
|
51
|
|
|
{ |
|
52
|
5 |
|
$this->repository = new WebHelperRepository($resDir); |
|
53
|
|
|
|
|
54
|
5 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Gets the Repository Instance. |
|
59
|
|
|
* |
|
60
|
|
|
* @return WebHelperRepository the Repository Instance |
|
61
|
|
|
*/ |
|
62
|
5 |
|
public function getRepository() |
|
63
|
|
|
{ |
|
64
|
5 |
|
return $this->repository; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Sets the web server instance. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $server a web server name |
|
71
|
|
|
* @param string $version a semver-like version |
|
72
|
|
|
*/ |
|
73
|
5 |
|
public function setServer($server, $version) |
|
74
|
|
|
{ |
|
75
|
5 |
|
$factory = new Factory(); |
|
76
|
5 |
|
$this->server = $factory->createWebServer($server, $this->versionParser->normalize($version)); |
|
77
|
|
|
|
|
78
|
5 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Gets the web server instance. |
|
83
|
|
|
* |
|
84
|
|
|
* @return WebServerInterface the web server instance |
|
85
|
|
|
*/ |
|
86
|
3 |
|
public function getServer() |
|
87
|
|
|
{ |
|
88
|
3 |
|
return $this->server; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Finds the best template for a web server directive according to its version. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $directive a directive |
|
95
|
|
|
* @return string the relative path to the template |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public function find($directive) |
|
98
|
|
|
{ |
|
99
|
3 |
|
$memoize = $this->getRepository()->getMemoize(); |
|
100
|
3 |
|
$serverName = $this->getServer()->getName(); |
|
101
|
3 |
|
$return = ''; |
|
102
|
|
|
|
|
103
|
3 |
|
$versions = array_keys($memoize[$serverName]); |
|
104
|
3 |
|
sort($versions); |
|
105
|
|
|
|
|
106
|
3 |
|
foreach ($versions as $version) { |
|
107
|
3 |
|
if ($this->comparator->greaterThanOrEqualTo($this->getServer()->getVersion(), $version) && |
|
108
|
3 |
|
array_key_exists($directive, $memoize[$serverName][$version]) |
|
109
|
3 |
|
) { |
|
110
|
2 |
|
$return = $memoize[$serverName][$version][$directive]; |
|
111
|
2 |
|
} |
|
112
|
3 |
|
} |
|
113
|
|
|
|
|
114
|
3 |
|
return $return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Outputs a webserver directive. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $twigFile a relative path of a template |
|
121
|
|
|
* @param array $params parameters |
|
122
|
|
|
* @return string the directive output |
|
123
|
|
|
*/ |
|
124
|
3 |
|
public function render($twigFile, array $params = array()) |
|
125
|
|
|
{ |
|
126
|
|
|
try { |
|
127
|
3 |
|
return $this->getRepository()->getTwig()->render($twigFile, $params); |
|
128
|
1 |
|
} catch (\Twig_Error_Loader $e) { |
|
129
|
1 |
|
return ''; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|