|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSHttpCache package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
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 FOS\HttpCache\Test\Proxy; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritdoc} |
|
16
|
|
|
*/ |
|
17
|
|
|
class VarnishProxy extends AbstractProxy |
|
18
|
|
|
{ |
|
19
|
|
|
protected $binary = 'varnishd'; |
|
20
|
|
|
|
|
21
|
|
|
protected $port = 6181; |
|
22
|
|
|
|
|
23
|
|
|
protected $managementPort = 6182; |
|
24
|
|
|
|
|
25
|
|
|
protected $pid = '/tmp/foshttpcache-varnish.pid'; |
|
26
|
|
|
|
|
27
|
|
|
protected $configFile; |
|
28
|
|
|
|
|
29
|
|
|
protected $configDir; |
|
30
|
|
|
|
|
31
|
|
|
protected $cacheDir; |
|
32
|
|
|
|
|
33
|
|
|
protected $allowInlineC = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $configFile Path to VCL file |
|
39
|
|
|
*/ |
|
40
|
28 |
|
public function __construct($configFile) |
|
41
|
|
|
{ |
|
42
|
28 |
|
$this->setConfigFile($configFile); |
|
43
|
27 |
|
$this->setCacheDir(sys_get_temp_dir().DIRECTORY_SEPARATOR.'foshttpcache-varnish'); |
|
44
|
27 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
26 |
|
public function start() |
|
50
|
|
|
{ |
|
51
|
26 |
|
$vclPath = ((int) $this->getVarnishVersion()) >= 5 ? 'vcl_path' : 'vcl_dir'; |
|
52
|
|
|
|
|
53
|
|
|
$args = [ |
|
54
|
26 |
|
'-a', $this->ip.':'.$this->getPort(), |
|
55
|
26 |
|
'-T', $this->ip.':'.$this->getManagementPort(), |
|
56
|
26 |
|
'-f', $this->getConfigFile(), |
|
57
|
26 |
|
'-n', $this->getCacheDir(), |
|
58
|
26 |
|
'-p', $vclPath.'='.$this->getConfigDir(), |
|
59
|
|
|
|
|
60
|
26 |
|
'-P', $this->pid, |
|
61
|
|
|
]; |
|
62
|
26 |
|
if ($this->getAllowInlineC()) { |
|
63
|
1 |
|
$args[] = '-p'; |
|
64
|
1 |
|
$args[] = 'vcc_allow_inline_c=on'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
26 |
|
$this->runCommand($this->getBinary(), $args); |
|
68
|
|
|
|
|
69
|
26 |
|
$this->waitFor($this->ip, $this->getPort(), 5000); |
|
70
|
25 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
24 |
|
public function stop() |
|
76
|
|
|
{ |
|
77
|
24 |
|
if (file_exists($this->pid)) { |
|
78
|
|
|
try { |
|
79
|
23 |
|
$this->runCommand('kill', ['-9', trim(file_get_contents($this->pid))]); |
|
80
|
|
|
} catch (\RuntimeException $e) { |
|
81
|
|
|
// Ignore if command fails when Varnish wasn't running |
|
82
|
|
|
} |
|
83
|
23 |
|
unlink($this->pid); |
|
84
|
23 |
|
$this->waitUntil($this->ip, $this->getPort(), 8000); |
|
85
|
|
|
} |
|
86
|
24 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
23 |
|
public function clear() |
|
92
|
|
|
{ |
|
93
|
23 |
|
$this->stop(); |
|
94
|
23 |
|
$this->start(); |
|
95
|
23 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $configDir |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function setConfigDir($configDir) |
|
101
|
|
|
{ |
|
102
|
2 |
|
$this->configDir = $configDir; |
|
103
|
2 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
26 |
|
public function getConfigDir() |
|
109
|
|
|
{ |
|
110
|
26 |
|
if (null === $this->configDir && null !== $this->configFile) { |
|
111
|
24 |
|
return dirname(realpath($this->getConfigFile())); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
return $this->configDir; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Set Varnish management port (defaults to 6182). |
|
119
|
|
|
* |
|
120
|
|
|
* @param int $managementPort |
|
121
|
|
|
*/ |
|
122
|
3 |
|
public function setManagementPort($managementPort) |
|
123
|
|
|
{ |
|
124
|
3 |
|
$this->managementPort = $managementPort; |
|
125
|
3 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get Varnish management port. |
|
129
|
|
|
* |
|
130
|
|
|
* @return int |
|
131
|
|
|
*/ |
|
132
|
27 |
|
public function getManagementPort() |
|
133
|
|
|
{ |
|
134
|
27 |
|
return $this->managementPort; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Set Varnish cache directory. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $cacheDir |
|
141
|
|
|
*/ |
|
142
|
27 |
|
public function setCacheDir($cacheDir) |
|
143
|
|
|
{ |
|
144
|
27 |
|
$this->cacheDir = $cacheDir; |
|
145
|
27 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get Varnish cache directory. |
|
149
|
|
|
* |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
27 |
|
public function getCacheDir() |
|
153
|
|
|
{ |
|
154
|
27 |
|
return $this->cacheDir; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Whether the inline C flag should be set. |
|
159
|
|
|
* |
|
160
|
|
|
* @return bool |
|
161
|
|
|
*/ |
|
162
|
26 |
|
public function getAllowInlineC() |
|
163
|
|
|
{ |
|
164
|
26 |
|
return $this->allowInlineC; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Set whether the inline c flag should be on or off. |
|
169
|
|
|
* |
|
170
|
|
|
* @param bool $allowInlineC True for on, false for off |
|
171
|
|
|
*/ |
|
172
|
2 |
|
public function setAllowInlineC($allowInlineC) |
|
173
|
|
|
{ |
|
174
|
2 |
|
$this->allowInlineC = (bool) $allowInlineC; |
|
175
|
2 |
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Defaults to 4. |
|
179
|
|
|
* |
|
180
|
|
|
* @return int |
|
181
|
|
|
*/ |
|
182
|
26 |
|
private function getVarnishVersion() |
|
183
|
|
|
{ |
|
184
|
26 |
|
return getenv('VARNISH_VERSION') ?: '4.0'; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|