|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the hogosha-monitor package |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* Feel free to edit as you please, and have fun. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Hogosha\Monitor\Client; |
|
17
|
|
|
|
|
18
|
|
|
use Concat\Http\Middleware\Logger; |
|
19
|
|
|
use GuzzleHttp\Client; |
|
20
|
|
|
use GuzzleHttp\HandlerStack; |
|
21
|
|
|
use GuzzleHttp\MessageFormatter; |
|
22
|
|
|
use GuzzleHttp\Middleware; |
|
23
|
|
|
use Hogosha\Monitor\Middleware\Backoff; |
|
24
|
|
|
use Hogosha\Monitor\Monitor; |
|
25
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
|
26
|
|
|
use Webmozart\Console\Adapter\IOOutput; |
|
27
|
|
|
use Webmozart\Console\Api\IO\IO; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @author Guillaume Cavana <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class GuzzleClient |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* createClient. |
|
36
|
|
|
* |
|
37
|
|
|
* @param IO $io |
|
38
|
|
|
* @param array $options |
|
39
|
|
|
* |
|
40
|
|
|
* @return Client |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function createClient(IO $io, $options = []) |
|
43
|
|
|
{ |
|
44
|
|
|
$stack = HandlerStack::create(); |
|
45
|
|
|
|
|
46
|
|
|
$middleware = (new Logger((new ConsoleLogger(new IOOutput($io))))); |
|
|
|
|
|
|
47
|
|
|
$middleware->setRequestLoggingEnabled(true); |
|
48
|
|
|
$format = "<fg=white;bg=blue>Request:</>\n <fg=white;bg=default>{host} {req_headers} \nBody: {req_body} </>\n <fg=white;bg=blue>Reponse:</>\n<fg=white;bg=default>{res_body} {res_headers}</>\n"; |
|
49
|
|
|
$middleware->setFormatter((new MessageFormatter($format))); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$stack->push($middleware, 'logger'); |
|
52
|
|
|
|
|
53
|
|
|
$stack->push( |
|
54
|
|
|
Middleware::retry(Backoff::decider(), Backoff::delay()) |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$defaults = [ |
|
58
|
|
|
'handler' => $stack, |
|
59
|
|
|
'allow_redirects' => false, |
|
60
|
|
|
'headers' => [ |
|
61
|
|
|
'User-Agent' => sprintf('Irongate Monitor %s', Monitor::VERSION), |
|
62
|
|
|
], |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
$options = array_merge($defaults, $options); |
|
66
|
|
|
|
|
67
|
|
|
return new Client($options); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: