BiliHelper-personal/src/Request/MultiRequest.php
2022-06-03 23:34:44 +08:00

164 lines
3.6 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php declare(strict_types=1);
/**
* Website: https://mudew.com/
* Author: Lkeme
* License: The MIT License
* Email: Useri@live.cn
* Updated: 2022 ~ 2023
*
* _____ _ _ _ _ _ _____ _ _____ _____ _____
* | _ \ | | | | | | | | | | | ____| | | | _ \ | ____| | _ \ & l、
* | |_| | | | | | | | | |_| | | |__ | | | |_| | | |__ | |_| | (゚、 。
* | _ { | | | | | | | _ | | __| | | | ___/ | __| | _ /   \、゙ ~ヽ *
* | |_| | | | | |___ | | | | | | | |___ | |___ | | | |___ | | \ \  じしf_, )
* |_____/ |_| |_____| |_| |_| |_| |_____| |_____| |_| |_____| |_| \_\
*/
namespace Bhp\Request;
use Closure;
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request as PRequest;
class MultiRequest
{
/**
* @var Client
*/
protected Client $client;
protected array $headers = [];
protected array $options = [];
protected Closure $successCallback;
protected Closure $errorCallback;
protected array$urls = [];
protected string$method;
protected int $concurrency = 10;
/**
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}
/**
* @param Client $client
* @return MultiRequest
*/
public static function newMultiRequest(Client $client): MultiRequest
{
return new self($client);
}
/**
* @param array $headers
* @return $this
*/
public function withHeaders(array $headers): static
{
$this->headers = array_merge($this->headers, $headers);
return $this;
}
/**
* @param $options
* @return $this
*/
public function withOptions($options): static
{
$this->options = $options;
return $this;
}
/**
* @param $concurrency
* @return $this
*/
public function concurrency($concurrency): static
{
$this->concurrency = $concurrency;
return $this;
}
/**
* @param Closure $success
* @return $this
*/
public function success(Closure $success): static
{
$this->successCallback = $success;
return $this;
}
/**
* @param Closure $error
* @return $this
*/
public function error(Closure $error): static
{
$this->errorCallback = $error;
return $this;
}
/**
* @param array $urls
* @return $this
*/
public function urls(array $urls): static
{
$this->urls = $urls;
return $this;
}
/**
* @return void
*/
public function get(): void
{
$this->method = 'GET';
$this->send();
}
/**
* @return void
*/
public function post(): void
{
$this->method = 'POST';
$this->send();
}
/**
* @return void
*/
protected function send(): void
{
$client = $this->client;
$requests = function ($urls) use ($client) {
foreach ($urls as $url) {
if (is_string($url)) {
yield new PRequest($this->method, $url, $this->headers);
} else {
yield $url;
}
}
};
$pool = new Pool($client, $requests($this->urls), [
'concurrency' => $this->concurrency,
'fulfilled' => $this->successCallback,
'rejected' => $this->errorCallback,
'options' => $this->options
]);
$promise = $pool->promise();
$promise->wait();
}
}