1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Maciej Paprocki for Bureau-VA. |
4
|
|
|
* Date: 23/02/2016 |
5
|
|
|
* Project Name: MaciekPaprocki\WordpressGuzzle |
6
|
|
|
* Time: 17:38. |
7
|
|
|
*/ |
8
|
|
|
namespace BureauVa\WordpressGuzzle; |
9
|
|
|
|
10
|
|
|
use GuzzleHttp\Promise\Promise; |
11
|
|
|
use GuzzleHttp\Client; |
12
|
|
|
use GuzzleHttp\Exception\RequestException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Pool. |
16
|
|
|
*/ |
17
|
|
|
class Pool |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $queries = []; |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $promises = []; |
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $transformers = []; |
31
|
|
|
/** |
32
|
|
|
* @var CachePool |
33
|
|
|
*/ |
34
|
|
|
private $cachePool = null; |
35
|
|
|
/** |
36
|
|
|
* @var Client |
37
|
|
|
*/ |
38
|
|
|
protected $client = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* let us keep reference to our client. |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function getClient() |
46
|
|
|
{ |
47
|
|
|
return $this->client; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Awaits all the requests attaching callbacks before. |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function unwrap() |
56
|
|
|
{ |
57
|
|
|
$promises = $this->flushPromises(); |
58
|
|
|
if (empty($promises)) { |
59
|
|
|
return []; |
60
|
|
|
} |
61
|
|
|
foreach ($promises as $key => $promise) { |
62
|
|
|
$promises[$key] = $this->attachTransforms($promise); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return \GuzzleHttp\Promise\unwrap($promises); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $query |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function addQuery($key, $query) |
74
|
|
|
{ |
75
|
|
|
$this->queries[$key] = $query; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Promise $promise |
82
|
|
|
*/ |
83
|
|
|
public function addPromise($name, Promise $promise) |
84
|
|
|
{ |
85
|
|
|
$this->promises[$name] = $promise; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
private function flushPromises() |
92
|
|
|
{ |
93
|
|
|
foreach ($this->queries as $key => $query) { |
94
|
|
|
|
95
|
|
|
$this->promises[$key] = $this->client->getAsync((string) $query); |
96
|
|
|
unset($this->queries[$key]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->promises; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function getTransformers() |
106
|
|
|
{ |
107
|
|
|
return $this->transformers; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Adds another transformer to collection of transformers. |
112
|
|
|
* |
113
|
|
|
* @param callable $transformer |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function addTransformer(callable $transformer) |
118
|
|
|
{ |
119
|
|
|
$this->transformers[] = $transformer; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param Promise $promise |
|
|
|
|
126
|
|
|
*/ |
127
|
|
|
public function attachTransforms(Promise $req) |
128
|
|
|
{ |
129
|
|
|
$transformers = $this->getTransformers(); |
130
|
|
|
|
131
|
|
|
return $req->then(function ($res) use ($transformers) { |
132
|
|
|
|
133
|
|
|
$data = \json_decode((string) $res->getBody()); |
134
|
|
|
|
135
|
|
|
if (is_object($data)) { |
136
|
|
|
foreach ($transformers as $transformer) { |
137
|
|
|
$data = \call_user_func($transformer, $data); |
138
|
|
|
} |
139
|
|
|
} elseif (is_array($data)) { |
140
|
|
|
foreach ($data as $key => $val) { |
141
|
|
|
foreach ($transformers as $transformer) { |
142
|
|
|
$data[$key] = \call_user_func($transformer, $val); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $data; |
148
|
|
|
}, function (RequestException $e) { |
149
|
|
|
|
150
|
|
|
echo $e->getMessage().$e->getRequest()->getUri().PHP_EOL; |
151
|
|
|
}); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* |
156
|
|
|
*/ |
157
|
|
|
public function getCachePool() |
158
|
|
|
{ |
159
|
|
|
return $this->cachePool; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param null $cachePool |
164
|
|
|
*/ |
165
|
|
|
public function setCachePool($cachePool) |
166
|
|
|
{ |
167
|
|
|
$this->cachePool = $cachePool; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Let us set our client reference taken from transaction. |
174
|
|
|
* |
175
|
|
|
* @param $client |
176
|
|
|
* |
177
|
|
|
* @return RepositoryAbstract |
178
|
|
|
*/ |
179
|
|
|
public function setClient(Client $client) |
180
|
|
|
{ |
181
|
|
|
$this->client = $client; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.