1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information please see |
17
|
|
|
* <http://phing.info>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
use GuzzleHttp\Client; |
21
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
22
|
|
|
use GuzzleHttp\HandlerStack; |
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Base class for GuzzleHttp-backed tasks |
27
|
|
|
* |
28
|
|
|
* Handles nested <config /> and <header /> tags, contains a method for |
29
|
|
|
* GuzzleHttp instance creation |
30
|
|
|
* |
31
|
|
|
* @package phing.tasks.ext |
32
|
|
|
* @author Alexey Borzov <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
abstract class HttpTask extends Task |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Holds the request URL |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $url = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Holds additional header data |
45
|
|
|
* |
46
|
|
|
* @var Parameter[] |
47
|
|
|
*/ |
48
|
|
|
protected $headers = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Holds additional config data for GuzzleHttp |
52
|
|
|
* |
53
|
|
|
* @var Parameter[] |
54
|
|
|
*/ |
55
|
|
|
protected $configData = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Holds the request method |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $method = 'GET'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Holds the authentication user name |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $authUser = ''; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Holds the authentication password |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
protected $authPassword = ''; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Holds the authentication scheme |
80
|
|
|
* |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
protected $authScheme = 'basic'; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var HandlerStack |
87
|
|
|
*/ |
88
|
|
|
protected static $handlerStack = null; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return HandlerStack |
92
|
|
|
*/ |
93
|
14 |
|
public static function getHandlerStack(): HandlerStack |
94
|
|
|
{ |
95
|
14 |
|
if (self::$handlerStack === null) { |
96
|
1 |
|
self::$handlerStack = HandlerStack::create(); |
97
|
|
|
} |
98
|
14 |
|
return self::$handlerStack; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Load the necessary environment for running this task. |
103
|
|
|
* |
104
|
|
|
* @throws BuildException |
105
|
|
|
*/ |
106
|
23 |
|
public function init() |
107
|
|
|
{ |
108
|
23 |
|
if (!class_exists('\GuzzleHttp\Client')) { |
109
|
|
|
throw new BuildException( |
110
|
|
|
get_class($this) . ' depends on Guzzle being installed ' |
111
|
|
|
. 'and on include_path.', |
112
|
|
|
$this->getLocation() |
113
|
|
|
); |
114
|
|
|
} |
115
|
23 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Sets the request URL |
119
|
|
|
* |
120
|
|
|
* @param string $url |
121
|
|
|
*/ |
122
|
15 |
|
public function setUrl($url) |
123
|
|
|
{ |
124
|
15 |
|
$this->url = $url; |
125
|
15 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Creates, configures, and sends a request |
129
|
|
|
* |
130
|
|
|
* @param array $options |
131
|
|
|
* @return ResponseInterface |
132
|
|
|
*/ |
133
|
14 |
|
protected function request($options = []) |
134
|
|
|
{ |
135
|
|
|
// set the authentication data |
136
|
14 |
|
if (!empty($this->authUser)) { |
137
|
2 |
|
$options['auth'] = [ |
138
|
2 |
|
$this->authUser, |
139
|
2 |
|
$this->authPassword, |
140
|
2 |
|
$this->authScheme |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
14 |
|
if (!empty($this->headers)) { |
145
|
2 |
|
$options['headers'] = []; |
146
|
|
|
|
147
|
2 |
|
foreach ($this->headers as $header) { |
148
|
2 |
|
$options['headers'][$header->getName()] = $header->getValue(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
14 |
|
foreach (array_keys($this->getProject()->getProperties()) as $propName) { |
153
|
14 |
|
if (0 === strpos($propName, 'phing.http.')) { |
154
|
3 |
|
$options[substr($propName, 11)] = (string) $this->getProject()->getProperty($propName); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
14 |
|
foreach ($this->configData as $parameter) { |
159
|
2 |
|
$options[$parameter->getName()] = $parameter->getValue(); |
160
|
|
|
} |
161
|
|
|
|
162
|
14 |
|
$client = new Client(['handler' => self::$handlerStack]); |
163
|
|
|
|
164
|
14 |
|
return $client->request($this->method, $this->url, $options); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Processes the server's response |
169
|
|
|
* |
170
|
|
|
* @param ResponseInterface $response |
171
|
|
|
* @return void |
172
|
|
|
* @throws BuildException |
173
|
|
|
*/ |
174
|
|
|
abstract protected function processResponse(ResponseInterface $response); |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Makes a HTTP request and processes its response |
178
|
|
|
* |
179
|
|
|
* @throws BuildException |
180
|
|
|
* @throws Exception |
181
|
|
|
*/ |
182
|
17 |
|
public function main() |
183
|
|
|
{ |
184
|
17 |
|
if (!isset($this->url)) { |
185
|
2 |
|
throw new BuildException("Required attribute 'url' is missing"); |
186
|
|
|
} |
187
|
|
|
|
188
|
15 |
|
$this->processResponse($this->request()); |
189
|
4 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Creates an additional header for this task |
193
|
|
|
* |
194
|
|
|
* @return Parameter The created header |
195
|
|
|
*/ |
196
|
2 |
|
public function createHeader() |
197
|
|
|
{ |
198
|
2 |
|
$num = array_push($this->headers, new Parameter()); |
199
|
|
|
|
200
|
2 |
|
return $this->headers[$num - 1]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Creates a config parameter for this task |
205
|
|
|
* |
206
|
|
|
* @return Parameter The created config parameter |
207
|
|
|
*/ |
208
|
2 |
|
public function createConfig() |
209
|
|
|
{ |
210
|
2 |
|
$num = array_push($this->configData, new Parameter()); |
211
|
|
|
|
212
|
2 |
|
return $this->configData[$num - 1]; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Sets the authentication user name |
217
|
|
|
* |
218
|
|
|
* @param string $user |
219
|
|
|
*/ |
220
|
2 |
|
public function setAuthUser($user) |
221
|
|
|
{ |
222
|
2 |
|
$this->authUser = $user; |
223
|
2 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Sets the authentication password |
227
|
|
|
* |
228
|
|
|
* @param string $password |
229
|
|
|
*/ |
230
|
2 |
|
public function setAuthPassword($password) |
231
|
|
|
{ |
232
|
2 |
|
$this->authPassword = $password; |
233
|
2 |
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Sets the authentication scheme |
237
|
|
|
* |
238
|
|
|
* @param string $scheme |
239
|
|
|
*/ |
240
|
2 |
|
public function setAuthScheme($scheme) |
241
|
|
|
{ |
242
|
2 |
|
$this->authScheme = $scheme; |
243
|
2 |
|
} |
244
|
|
|
} |
245
|
|
|
|