1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/http. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overtrue\Http\Traits; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use GuzzleHttp\HandlerStack; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Trait CreatesDefaultHttpClient. |
19
|
|
|
*/ |
20
|
|
|
trait CreatesDefaultHttpClient |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $middlewares = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \GuzzleHttp\HandlerStack |
29
|
|
|
*/ |
30
|
|
|
protected $handlerStack; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array $options |
34
|
|
|
* |
35
|
|
|
* @return \GuzzleHttp\Client |
36
|
|
|
*/ |
37
|
|
|
public function createDefaultHttClient(array $options) |
38
|
|
|
{ |
39
|
|
|
return new Client(array_merge([ |
40
|
|
|
'handler' => $this->getHandlerStack(), |
41
|
|
|
], $options)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Add a middleware. |
46
|
|
|
* |
47
|
|
|
* @param callable $middleware |
48
|
|
|
* @param string|null $name |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function pushMiddleware(callable $middleware, string $name = null) |
53
|
|
|
{ |
54
|
|
|
if (!is_null($name)) { |
55
|
|
|
$this->middlewares[$name] = $middleware; |
56
|
|
|
} else { |
57
|
|
|
array_push($this->middlewares, $middleware); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return all middlewares. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getMiddlewares(): array |
69
|
|
|
{ |
70
|
|
|
return $this->middlewares; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $middlewares |
75
|
|
|
* |
76
|
|
|
* @return \Overtrue\Http\Traits\CreatesDefaultHttpClient |
77
|
|
|
*/ |
78
|
|
|
public function setMiddlewares(array $middlewares) |
79
|
|
|
{ |
80
|
|
|
$this->middlewares = $middlewares; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param \GuzzleHttp\HandlerStack $handlerStack |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setHandlerStack(HandlerStack $handlerStack) |
91
|
|
|
{ |
92
|
|
|
$this->handlerStack = $handlerStack; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Build a handler stack. |
99
|
|
|
* |
100
|
|
|
* @return \GuzzleHttp\HandlerStack |
101
|
|
|
*/ |
102
|
|
|
public function getHandlerStack(): HandlerStack |
103
|
|
|
{ |
104
|
|
|
if ($this->handlerStack) { |
105
|
|
|
return $this->handlerStack; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->handlerStack = HandlerStack::create(); |
109
|
|
|
|
110
|
|
|
foreach ($this->middlewares as $name => $middleware) { |
111
|
|
|
$this->handlerStack->push($middleware, $name); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->handlerStack; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|