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\WebServer\WebServerFactory; |
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 WebServerInterface a Web Server instance */ |
34
|
|
|
private $server; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Base constructor. |
38
|
|
|
*/ |
39
|
3 |
|
public function __construct() |
40
|
|
|
{ |
41
|
3 |
|
$this->versionParser = new VersionParser(); |
42
|
3 |
|
$this->comparator = new Comparator(); |
43
|
3 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Sets the Repository Instance. |
47
|
|
|
* |
48
|
|
|
* @param string $resDir a Path of a Directives Repository |
49
|
|
|
*/ |
50
|
3 |
|
public function setRepository($resDir = '') |
51
|
|
|
{ |
52
|
3 |
|
$this->repository = new WebHelperRepository($resDir); |
53
|
|
|
|
54
|
3 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Gets the Repository Instance. |
59
|
|
|
* |
60
|
|
|
* @return WebHelperRepository the Repository Instance |
61
|
|
|
*/ |
62
|
2 |
|
public function getRepository() |
63
|
|
|
{ |
64
|
2 |
|
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
|
3 |
|
public function setServer($server, $version) |
74
|
|
|
{ |
75
|
3 |
|
$factory = new WebServerFactory(); |
76
|
3 |
|
$this->server = $factory->create($server, $this->versionParser->normalize($version)); |
|
|
|
|
77
|
|
|
|
78
|
3 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Gets the web server instance. |
83
|
|
|
* |
84
|
|
|
* @return WebServerInterface the web server instance |
85
|
|
|
*/ |
86
|
2 |
|
public function getServer() |
87
|
|
|
{ |
88
|
2 |
|
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
|
2 |
|
public function find($directive) |
98
|
|
|
{ |
99
|
2 |
|
$memoize = $this->getRepository()->getMemoize(); |
100
|
2 |
|
$return = ''; |
101
|
2 |
|
$versions = array_keys($memoize[$this->getServer()->getName()]); |
102
|
2 |
|
sort($versions); |
103
|
|
|
|
104
|
2 |
|
foreach ($versions as $version) { |
105
|
2 |
|
if ($this->comparator->greaterThanOrEqualTo($this->getServer()->getVersion(), $version) && |
106
|
2 |
|
array_key_exists($directive, $memoize[$this->getServer()->getName()][$version]) |
107
|
2 |
|
) { |
108
|
1 |
|
$return = $memoize[$this->getServer()->getName()][$version][$directive]; |
109
|
1 |
|
} |
110
|
2 |
|
} |
111
|
|
|
|
112
|
2 |
|
return $return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Outputs a webserver directive. |
117
|
|
|
* |
118
|
|
|
* @param string $twigFile a relative path of a template |
119
|
|
|
* @param array $params parameters |
120
|
|
|
* @return string the directive output |
121
|
|
|
*/ |
122
|
1 |
|
public function render($twigFile, array $params = array()) |
123
|
|
|
{ |
124
|
1 |
|
return $this->repository->getTwig()->render($twigFile, $params); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..