add MultiRequest

This commit is contained in:
Jaeger 2018-12-10 19:23:15 +08:00
parent c32736bd9e
commit a779ef71f3
4 changed files with 54 additions and 2 deletions

View File

@ -7,7 +7,7 @@
namespace QL\Dom;
use Illuminate\Support\Collection;
use Tightenco\Collect\Support\Collection;
use phpQuery;
use QL\QueryList;
use Closure;

View File

@ -11,6 +11,7 @@ namespace QL\Providers;
use QL\Contracts\ServiceProviderContract;
use QL\Kernel;
use QL\Services\HttpService;
use QL\Services\MultiRequestService;
class HttpServiceProvider implements ServiceProviderContract
{
@ -27,5 +28,9 @@ class HttpServiceProvider implements ServiceProviderContract
$kernel->bind('postJson',function (...$args){
return HttpService::postJson($this,...$args);
});
$kernel->bind('multiRequest',function (...$args){
return new MultiRequestService($this,...$args);
});
}
}

View File

@ -14,8 +14,10 @@
namespace QL;
use phpQuery;
use QL\Dom\Query;
use Illuminate\Support\Collection;
use Tightenco\Collect\Support\Collection;
use Closure;
use QL\Services\MultiRequestService;
/**
* Class QueryList
@ -36,6 +38,7 @@ use Closure;
* @method QueryList get($url,$args = null,$otherArgs = [])
* @method QueryList post($url,$args = null,$otherArgs = [])
* @method QueryList postJson($url,$args = null,$otherArgs = [])
* @method MultiRequestService multiRequest($urls)
* @method QueryList use($plugins,...$opt)
* @method QueryList pipe(Closure $callback = null)
*/

View File

@ -0,0 +1,44 @@
<?php
/**
* Created by PhpStorm.
* User: Jaeger <JaegerCode@gmail.com>
* Date: 18/12/10
* Time: 下午7:05
*/
namespace QL\Services;
use Jaeger\GHttp;
use Closure;
class MultiRequestService
{
protected $ql;
protected $multiRequest;
public function __construct($ql,$urls)
{
$this->ql = $ql;
$this->multiRequest = GHttp::multiRequest($urls);
}
public function __call($name, $arguments)
{
return $this->multiRequest->$name(...$arguments);
}
public function success(Closure $success)
{
return $this->multiRequest->success(function($response, $index) use($success){
$this->ql->setHtml((String)$response->getBody());
$success($this->ql,$response, $index);
});
}
public function error(Closure $error)
{
return $this->multiRequest->error(function($reason, $index) use($error){
$error($this->ql,$reason, $index);
});
}
}