1 | <?php |
||
2 | /** |
||
3 | * php-guard/curl <https://github.com/php-guard/curl> |
||
4 | * Copyright (C) ${YEAR} by Alexandre Le Borgne <[email protected]>. |
||
5 | * |
||
6 | * This program is free software: you can redistribute it and/or modify |
||
7 | * it under the terms of the GNU General Public License as published by |
||
8 | * the Free Software Foundation, either version 3 of the License, or |
||
9 | * (at your option) any later version. |
||
10 | * |
||
11 | * This program is distributed in the hope that it will be useful, |
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | * GNU General Public License for more details. |
||
15 | * |
||
16 | * You should have received a copy of the GNU General Public License |
||
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
18 | */ |
||
19 | |||
20 | namespace PhpGuard\Curl; |
||
21 | |||
22 | class MultiCurl |
||
23 | { |
||
24 | protected $requests; |
||
25 | /** |
||
26 | * @var CurlRequestFactory |
||
27 | */ |
||
28 | private $curlRequestFactory; |
||
29 | /** |
||
30 | * @var Curl |
||
31 | */ |
||
32 | private $curl; |
||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private $options; |
||
37 | |||
38 | 3 | public function __construct(Curl $curl, array $options = []) |
|
39 | { |
||
40 | 3 | $this->requests = []; |
|
41 | 3 | $this->curl = $curl; |
|
42 | 3 | $this->options = $options; |
|
43 | 3 | $this->curlRequestFactory = $curl->getCurlRequestFactory(); |
|
44 | 3 | } |
|
45 | |||
46 | /** |
||
47 | * @param string $url |
||
48 | * @param mixed $query |
||
49 | * @param array $headers |
||
50 | * |
||
51 | * @return MultiCurl |
||
52 | */ |
||
53 | 3 | public function get(string $url, $query = null, array $headers = []): self |
|
54 | { |
||
55 | 3 | $this->requests[] = $this->curlRequestFactory->create('GET', $url, null, $query, $headers); |
|
56 | |||
57 | 3 | return $this; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $url |
||
62 | * @param mixed $data |
||
63 | * @param mixed $query |
||
64 | * @param array $headers |
||
65 | * |
||
66 | * @return MultiCurl |
||
67 | */ |
||
68 | 3 | public function post(string $url, $data = null, $query = null, array $headers = []): self |
|
69 | { |
||
70 | 3 | $this->requests[] = $this->curlRequestFactory->create('POST', $url, $data, $query, $headers); |
|
71 | |||
72 | 3 | return $this; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param string $url |
||
77 | * @param mixed $data |
||
78 | * @param mixed $query |
||
79 | * @param array $headers |
||
80 | * |
||
81 | * @return MultiCurl |
||
82 | */ |
||
83 | public function put(string $url, $data = null, $query = null, array $headers = []): self |
||
84 | { |
||
85 | $this->requests[] = $this->curlRequestFactory->create('PUT', $url, $data, $query, $headers); |
||
86 | |||
87 | return $this; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param string $url |
||
92 | * @param mixed $data |
||
93 | * @param mixed $query |
||
94 | * @param array $headers |
||
95 | * |
||
96 | * @return MultiCurl |
||
97 | */ |
||
98 | public function patch(string $url, $data = null, $query = null, array $headers = []): self |
||
99 | { |
||
100 | $this->requests[] = $this->curlRequestFactory->create('PATCH', $url, $data, $query, $headers); |
||
101 | |||
102 | return $this; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param string $url |
||
107 | * @param mixed $data |
||
108 | * @param mixed $query |
||
109 | * @param array $headers |
||
110 | * |
||
111 | * @return MultiCurl |
||
112 | */ |
||
113 | public function delete(string $url, $data = null, $query = null, array $headers = []): self |
||
114 | { |
||
115 | $this->requests[] = $this->curlRequestFactory->create('DELETE', $url, $data, $query, $headers); |
||
116 | |||
117 | return $this; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @return CurlResponse[] |
||
122 | * |
||
123 | * @throws CurlError |
||
124 | */ |
||
125 | 3 | public function execute(): array |
|
126 | { |
||
127 | 3 | $mh = curl_multi_init(); |
|
128 | |||
129 | 3 | foreach ($this->options as $key => $value) { |
|
130 | curl_multi_setopt($mh, $key, $value); |
||
131 | } |
||
132 | |||
133 | 3 | $chs = []; |
|
134 | 3 | foreach ($this->requests as $request) { |
|
135 | 3 | $ch = $this->curl->getRequestModifierPipeline()->process($request)->resource(); |
|
136 | 3 | $chs[] = $ch; |
|
137 | 3 | curl_multi_add_handle($mh, $ch); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
138 | } |
||
139 | |||
140 | 3 | $active = null; |
|
141 | |||
142 | do { |
||
143 | 3 | $code = curl_multi_exec($mh, $active); |
|
144 | 3 | curl_multi_select($mh); |
|
145 | 3 | } while ($active > 0); |
|
146 | |||
147 | try { |
||
148 | 3 | if ($code > CURLM_OK) { |
|
149 | throw new CurlError(curl_multi_strerror($code), $code); |
||
150 | } |
||
151 | |||
152 | 3 | $responses = []; |
|
153 | 3 | foreach ($chs as $ch) { |
|
154 | 3 | $responses[] = $this->curl->getCurlResponseFactory()->create($ch, curl_multi_getcontent($ch)); |
|
155 | } |
||
156 | |||
157 | 3 | return $responses; |
|
158 | } finally { |
||
159 | 3 | foreach ($chs as $ch) { |
|
160 | 3 | curl_multi_remove_handle($mh, $ch); |
|
161 | } |
||
162 | 3 | curl_multi_close($mh); |
|
163 | } |
||
164 | } |
||
165 | } |
||
166 |