forked from evaisse/SimpleHttpBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiManager.php
More file actions
222 lines (188 loc) · 5.98 KB
/
MultiManager.php
File metadata and controls
222 lines (188 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
* (c) Darrell Hamilton <darrell.noice@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace evaisse\SimpleHttpBundle\Curl;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* Manage the execution of multiple Curl\Request objects in parallel
*/
class MultiManager implements CurlRequest
{
private static $errors = array(
CURLM_BAD_HANDLE => 'Bad Handle',
CURLM_BAD_EASY_HANDLE => 'Bad Easy Handle',
CURLM_OUT_OF_MEMORY => 'Out of Memory',
CURLM_INTERNAL_ERROR => 'Internal Error'
);
/**
* The cURL multi handle
*
* @var resource
*/
private $handle;
/**
* A hash of object ids and the associated Request object registered with this object
*
* @var array
*/
private $requests = array();
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* Whether we should block until finished, let it go and finish processing
* via the destructor
*
* @var boolean
*/
private $blocking;
/**
* @param EventDispatcherInterface $dispatcher The event dispatcher that will be notified
* upon completed request
* @param boolean $blocking Whether the execution should block until finished or wait until
* the destructor is called to block
*/
public function __construct(?EventDispatcherInterface $dispatcher = null, $blocking = true)
{
$this->dispatcher = $dispatcher;
$this->handle = curl_multi_init();
$this->blocking = $blocking;
}
public function __destruct() {
if(!$this->blocking) {
$this->executeBlocking();
}
foreach($this->requests as $request) {
$this->removeRequest($request);
}
curl_multi_close($this->handle);
}
/**
* Add a cURL request to be processed in parallel
*
* Alias of the curl_multi_add_handle function
*
* @param Request $request A Request object to be executed
* @return MultiManager $this
*/
public function addRequest(Request $request) {
$oid = spl_object_hash($request);
if(!isset($this->requests[$oid])) {
$this->requests[$oid] = $request;
curl_multi_add_handle($this->handle, $request->getHandle());
}
return $this;
}
/**
* Remove a request from the execution stack
*
* Analogous to the curl_multi_remove_handle function
*
* @param Request $request The Request to be removed
* @return Request|boolean the request that was removed or false if the request isn't managed by this object
*/
public function removeRequest(Request $request) {
$oid = spl_object_hash($request);
$result = false;
if(isset($this->requests[$oid])) {
unset($this->requests[$oid]);
$result = $request;
curl_multi_remove_handle($this->handle, $request->getHandle());
}
return $result;
}
/**
* Get the content returned by a Request managed by this object
*
* Analogous to curl_multi_getcontent
*
* @param Request $request The Request to get the results for
* @return string
*/
public function getContent(Request $request) {
return curl_multi_getcontent($request->getHandle());
}
/**
* Execute the registered Request objects in parallel
*
* Analogous to curl_multi_exec
*
* @return int the result of curl_multi_exec
* @throws CurlErrorException
*/
public function execute() {
if($this->blocking) {
return $this->executeBlocking();
} else {
return $this->errorCheck(
curl_multi_exec($this->handle,$active)
);
}
}
private function executeBlocking() {
$info = false;
$active = false;
$status = false;
do {
$status = curl_multi_exec($this->handle, $active);
// Block until there's something to do.
if(curl_multi_select($this->handle) != -1) {
$this->processMultiInfo();
}
} while ($status === CURLM_CALL_MULTI_PERFORM || $active);
// We shouldn't do any processing if the multi handle blows up
$this->errorCheck($status);
// Finish processing any remaining request data
$this->processMultiInfo();
return $status;
}
private function errorCheck($status) {
if(isset(static::$errors[$status])) {
throw new CurlErrorException(static::$errors[$status]);
}
return $status;
}
private function processMultiInfo() {
while($info = curl_multi_info_read($this->handle)) {
$this->processInfo($info);
}
}
/**
* Process and callbacks associated with the handle returned by
* curl_multi_info_read
*
* @param array $info data array from curl_multi_info_read
*/
private function processInfo(array $info) {
$request = $this->findRequest($info["handle"]);
if(isset($this->dispatcher) && false !== $request) {
$this->dispatcher->dispatch(
new MultiInfoEvent(
$this,
$request,
new MultiInfo($info)
),
CurlEvents::MULTI_INFO
);
}
}
/**
* Given a cURL handle, locate the request object associated with it
*
* @param resource $handle a cURL handle
* @return Request|boolean the associated Request object or false if it is not found
*/
public function findRequest($handle) {
foreach($this->requests as $request) {
if($handle === $request->getHandle()) {
return $request;
}
}
return false;
}
}