1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jenky\Hermes\Concerns; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\HandlerStack; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
trait InteractsWithConfiguration |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Make the Guzzle client options from config. |
12
|
|
|
* |
13
|
|
|
* @param array $config |
14
|
|
|
* @return array |
15
|
|
|
*/ |
16
|
|
|
public function makeClientOptions(array $config) |
17
|
|
|
{ |
18
|
|
|
$options = $config['options'] ?? []; |
19
|
|
|
$options['handler'] = $options['handler'] ?? $this->createHandler($config); |
20
|
|
|
|
21
|
|
|
return $options; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create the client handler stack instance. |
26
|
|
|
* |
27
|
|
|
* @param array $config |
28
|
|
|
* @return \GuzzleHttp\HandlerStack |
29
|
|
|
*/ |
30
|
|
|
public function createHandler(array $config = []) |
31
|
|
|
{ |
32
|
|
|
return $this->prepareHandler( |
33
|
|
|
HandlerStack::create($this->handler($config)), $config |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the handle stack's handler instance. |
39
|
|
|
* |
40
|
|
|
* @param array $config |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function handler(array $config) |
44
|
|
|
{ |
45
|
|
|
if (empty($config['handler'])) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$factory = is_callable($handler = $config['handler']) |
50
|
|
|
? $handler |
51
|
|
|
: $this->app->make($handler, $config['with'] ?? []); |
52
|
|
|
|
53
|
|
|
return $factory($config); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Prepare handler stack for usage by Guzzle client. |
58
|
|
|
* |
59
|
|
|
* @param \GuzzleHttp\HandlerStack $handler |
60
|
|
|
* @param array $config |
61
|
|
|
* @return \GuzzleHttp\HandlerStack |
62
|
|
|
*/ |
63
|
|
|
protected function prepareHandler(HandlerStack $handler, array $config = []) |
64
|
|
|
{ |
65
|
|
|
foreach ($this->middleware($config) as [$middleware, $name]) { |
66
|
|
|
$handler->push($middleware, $name); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->tap($handler, $config); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Apply the configured taps for the handle stack. |
74
|
|
|
* |
75
|
|
|
* @param \GuzzleHttp\HandlerStack $handler |
76
|
|
|
* @param array $config |
77
|
|
|
* @return \GuzzleHttp\HandlerStack |
78
|
|
|
*/ |
79
|
|
|
protected function tap(HandlerStack $handler, array $config = []) |
80
|
|
|
{ |
81
|
|
|
foreach ($config['tap'] ?? [] as $tap) { |
82
|
|
|
[$class, $arguments] = $this->parseTap($tap); |
83
|
|
|
|
84
|
|
|
$this->app->make($class)->__invoke( |
85
|
|
|
$handler, ...array_filter(explode(',', $arguments)) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $handler; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Parse the given tap class string into a class name and arguments string. |
94
|
|
|
* |
95
|
|
|
* @param string $tap |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected function parseTap($tap) |
99
|
|
|
{ |
100
|
|
|
return Str::contains($tap, ':') ? explode(':', $tap, 2) : [$tap, '']; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Parse the given class string into a class name and arguments array. |
105
|
|
|
* |
106
|
|
|
* @param mixed $key |
107
|
|
|
* @param mixed $value |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
protected function parseClassAndArguments($key, $value) |
111
|
|
|
{ |
112
|
|
|
if (is_string($key) && is_array($value)) { |
113
|
|
|
return [$key, $value]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return [$value, []]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get all middleware that will be pushed to handle stack instance. |
121
|
|
|
* |
122
|
|
|
* @param array $config |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function middleware(array $config) |
126
|
|
|
{ |
127
|
|
|
$middleware = []; |
128
|
|
|
|
129
|
|
|
foreach (array_merge($config['middleware'] ?? [], $config['interceptors'] ?? []) as $key => $value) { |
130
|
|
|
$middleware[] = $this->parseMiddleware($key, $value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $middleware; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Parse the given middleware and create middleware instance with it's parameters. |
138
|
|
|
* |
139
|
|
|
* @param mixed $key |
140
|
|
|
* @param mixed $value |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
protected function parseMiddleware($key, $value) |
144
|
|
|
{ |
145
|
|
|
$name = is_numeric($key) ? '' : $key; |
146
|
|
|
|
147
|
|
|
if (is_callable($value)) { |
148
|
|
|
return [$value, $name]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
[$class, $arguments] = $this->parseClassAndArguments($key, $value); |
152
|
|
|
|
153
|
|
|
return [$this->app->make($class, $arguments), $class]; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|