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