1 | <?php |
||
23 | class MultiRequest{ |
||
24 | |||
25 | /** |
||
26 | * the curl_multi master handle |
||
27 | * |
||
28 | * @var resource |
||
29 | */ |
||
30 | protected $curl_multi; |
||
31 | |||
32 | /** |
||
33 | * cURL options for each handle |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $curl_options = []; |
||
38 | |||
39 | /** |
||
40 | * An array of the request URLs |
||
41 | * |
||
42 | * @var array<\chillerlan\TinyCurl\URL> |
||
43 | */ |
||
44 | protected $stack = []; |
||
45 | |||
46 | /** |
||
47 | * The returned value from MultiResponseHandlerInterface::handleResponse() for each request |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $responses = []; |
||
52 | |||
53 | /** |
||
54 | * @var \chillerlan\TinyCurl\MultiRequestOptions |
||
55 | */ |
||
56 | protected $options; |
||
57 | |||
58 | /** |
||
59 | * @var \chillerlan\TinyCurl\MultiResponseHandlerInterface |
||
60 | */ |
||
61 | protected $multiResponseHandler; |
||
62 | |||
63 | /** |
||
64 | * MultiRequest constructor. |
||
65 | * |
||
66 | * @param \chillerlan\Traits\ContainerInterface|null $options |
||
67 | */ |
||
68 | public function __construct(ContainerInterface $options = null){ |
||
69 | $this->setOptions($options ?: new MultiRequestOptions); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * closes the curl_multi instance |
||
74 | * |
||
75 | * @codeCoverageIgnore |
||
76 | */ |
||
77 | public function __destruct(){ |
||
78 | if($this->curl_multi){ |
||
79 | curl_multi_close($this->curl_multi); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param \chillerlan\Traits\ContainerInterface $options |
||
85 | * |
||
86 | * @return \chillerlan\TinyCurl\MultiRequest |
||
87 | */ |
||
88 | public function setOptions(ContainerInterface $options):MultiRequest { |
||
89 | $this->options = $options; |
||
|
|||
90 | |||
91 | if($this->options->handler){ |
||
92 | $this->setHandler(); |
||
93 | } |
||
94 | |||
95 | $this->curl_options = $this->options->curl_options + [ |
||
96 | CURLOPT_HEADER => true, |
||
97 | CURLOPT_RETURNTRANSFER => true, |
||
98 | CURLOPT_USERAGENT => $this->options->user_agent, |
||
99 | CURLOPT_PROTOCOLS => CURLPROTO_HTTP|CURLPROTO_HTTPS, |
||
100 | CURLOPT_SSL_VERIFYPEER => true, |
||
101 | CURLOPT_SSL_VERIFYHOST => 2, // Support for value 1 removed in cURL 7.28.1 |
||
102 | CURLOPT_CAINFO => is_file($this->options->ca_info) ? $this->options->ca_info : null, |
||
103 | ]; |
||
104 | |||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param \chillerlan\TinyCurl\MultiResponseHandlerInterface|null $handler |
||
110 | * |
||
111 | * @return \chillerlan\TinyCurl\MultiRequest |
||
112 | * @throws \chillerlan\TinyCurl\RequestException |
||
113 | */ |
||
114 | public function setHandler(MultiResponseHandlerInterface $handler = null):MultiRequest { |
||
115 | |||
116 | if(!$handler){ |
||
117 | |||
118 | if(!class_exists($this->options->handler)){ |
||
119 | throw new RequestException('no handler set'); |
||
120 | } |
||
121 | |||
122 | $handler = new $this->options->handler($this); |
||
123 | |||
124 | if(!is_a($handler, MultiResponseHandlerInterface::class)){ |
||
125 | throw new RequestException('handler is not a MultiResponseHandlerInterface'); |
||
126 | } |
||
127 | |||
128 | } |
||
129 | |||
130 | $this->multiResponseHandler = $handler; |
||
131 | |||
132 | return $this; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param array $urls array of \chillerlan\TinyCurl\URL objects |
||
137 | * |
||
138 | * @return void |
||
139 | * @throws \chillerlan\TinyCurl\RequestException |
||
140 | */ |
||
141 | public function fetch(array $urls){ |
||
142 | |||
143 | if(empty($urls)){ |
||
144 | throw new RequestException('$urls is empty'); |
||
145 | } |
||
146 | |||
147 | $this->stack = $urls; |
||
148 | $this->curl_multi = curl_multi_init(); |
||
149 | |||
150 | curl_multi_setopt($this->curl_multi, CURLMOPT_PIPELINING, 2); |
||
151 | curl_multi_setopt($this->curl_multi, CURLMOPT_MAXCONNECTS, $this->options->window_size); |
||
152 | |||
153 | // shoot out the first batch of requests |
||
154 | array_map(function(){ |
||
155 | $this->createHandle(); |
||
156 | }, range(1, $this->options->window_size)); |
||
157 | |||
158 | /// ...and start processing the stack |
||
159 | $this->processStack(); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @see \chillerlan\TinyCurl\MultiResponseHandlerInterface |
||
164 | * |
||
165 | * @param mixed $response |
||
166 | * |
||
167 | * @return void |
||
168 | */ |
||
169 | public function addResponse($response){ |
||
172 | |||
173 | /** |
||
174 | * @return array |
||
175 | */ |
||
176 | public function getResponseData():array { |
||
179 | |||
180 | /** |
||
181 | * creates a new cURL handle |
||
182 | * |
||
183 | * @return void |
||
184 | */ |
||
185 | protected function createHandle(){ |
||
209 | |||
210 | /** |
||
211 | * processes the requests |
||
212 | * |
||
213 | * @return void |
||
214 | */ |
||
215 | protected function processStack(){ |
||
245 | |||
246 | } |
||
247 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.