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
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
15
|
|
|
|
16
|
|
|
abstract class AbstractProxy implements ProxyInterface |
17
|
|
|
{ |
18
|
|
|
protected $port; |
19
|
|
|
protected $binary; |
20
|
|
|
protected $configFile; |
21
|
|
|
protected $ip = '127.0.0.1'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Wait for caching proxy to be started up and reachable |
25
|
|
|
* |
26
|
|
|
* @param string $ip |
27
|
|
|
* @param int $port |
28
|
|
|
* @param int $timeout Timeout in milliseconds |
29
|
|
|
* |
30
|
|
|
* @throws \RuntimeException If proxy is not reachable within timeout |
31
|
|
|
*/ |
32
|
31 |
View Code Duplication |
protected function waitFor($ip, $port, $timeout) |
|
|
|
|
33
|
|
|
{ |
34
|
31 |
|
if (!$this->wait( |
35
|
31 |
|
$timeout, |
36
|
|
|
function () use ($ip, $port) { |
37
|
29 |
|
return true == @fsockopen($ip, $port); |
38
|
|
|
} |
39
|
31 |
|
)) { |
40
|
1 |
|
throw new \RuntimeException( |
41
|
1 |
|
sprintf( |
42
|
1 |
|
'Caching proxy cannot be reached at %s:%s', |
43
|
1 |
|
$ip, |
44
|
|
|
$port |
45
|
1 |
|
) |
46
|
1 |
|
); |
47
|
|
|
} |
48
|
30 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Wait for caching proxy to be started up and reachable |
52
|
|
|
* |
53
|
|
|
* @param string $ip |
54
|
|
|
* @param int $port |
55
|
|
|
* @param int $timeout Timeout in milliseconds |
56
|
|
|
* |
57
|
|
|
* @throws \RuntimeException If proxy is not reachable within timeout |
58
|
|
|
*/ |
59
|
23 |
View Code Duplication |
protected function waitUntil($ip, $port, $timeout) |
|
|
|
|
60
|
|
|
{ |
61
|
23 |
|
if (!$this->wait( |
62
|
23 |
|
$timeout, |
63
|
23 |
|
function () use ($ip, $port) { |
64
|
|
|
// This doesn't seem to work |
65
|
23 |
|
return false == @fsockopen($ip, $port); |
66
|
|
|
} |
67
|
23 |
|
)) { |
68
|
|
|
throw new \RuntimeException( |
69
|
|
|
sprintf( |
70
|
|
|
'Caching proxy still up at %s:%s', |
71
|
|
|
$ip, |
72
|
|
|
$port |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
23 |
|
} |
77
|
|
|
|
78
|
29 |
|
protected function wait($timeout, $callback) |
79
|
|
|
{ |
80
|
29 |
|
for ($i = 0; $i < $timeout; $i++) { |
81
|
29 |
|
if ($callback()) { |
82
|
29 |
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
23 |
|
usleep(1000); |
86
|
23 |
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Run a shell command |
93
|
|
|
* |
94
|
|
|
* @param string $command |
95
|
|
|
* @param array $arguments |
96
|
|
|
* |
97
|
|
|
* @throws \RuntimeException If command execution fails |
98
|
|
|
*/ |
99
|
29 |
|
protected function runCommand($command, array $arguments) |
100
|
|
|
{ |
101
|
29 |
|
$builder = new ProcessBuilder($arguments); |
102
|
29 |
|
$builder->setPrefix($command); |
103
|
|
|
|
104
|
29 |
|
$process = $builder->getProcess(); |
105
|
29 |
|
$process->run(); |
106
|
|
|
|
107
|
29 |
|
if (!$process->isSuccessful()) { |
108
|
|
|
throw new \RuntimeException($process->getErrorOutput()); |
109
|
|
|
} |
110
|
29 |
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $port |
115
|
|
|
*/ |
116
|
29 |
|
public function setPort($port) |
117
|
|
|
{ |
118
|
29 |
|
$this->port = $port; |
119
|
29 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
31 |
|
public function getPort() |
125
|
|
|
{ |
126
|
31 |
|
return $this->port; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set Varnish binary (defaults to varnishd) |
131
|
|
|
* |
132
|
|
|
* @param string $binary |
133
|
|
|
*/ |
134
|
1 |
|
public function setBinary($binary) |
135
|
|
|
{ |
136
|
1 |
|
$this->binary = $binary; |
137
|
1 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get Varnish binary |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
31 |
|
public function getBinary() |
145
|
|
|
{ |
146
|
31 |
|
return $this->binary; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Set IP address (defaults to 127.0.0.1) |
152
|
|
|
* |
153
|
|
|
* @param string $ip |
154
|
|
|
*/ |
155
|
1 |
|
public function setIp($ip) |
156
|
|
|
{ |
157
|
1 |
|
$this->ip = $ip; |
158
|
1 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get IP address |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
6 |
|
public function getIp() |
166
|
|
|
{ |
167
|
6 |
|
return $this->ip; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $configFile |
172
|
|
|
* |
173
|
|
|
* @throws \InvalidArgumentException |
174
|
|
|
*/ |
175
|
30 |
|
public function setConfigFile($configFile) |
176
|
|
|
{ |
177
|
30 |
|
if (!file_exists($configFile)) { |
178
|
1 |
|
throw new \InvalidArgumentException('Cannot find config file: ' . $configFile); |
179
|
|
|
} |
180
|
|
|
|
181
|
29 |
|
$this->configFile = $configFile; |
182
|
29 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
31 |
|
public function getConfigFile() |
188
|
|
|
{ |
189
|
31 |
|
return $this->configFile; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.