mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-30 04:01:47 +00:00
Fix decoders set on individual instances ignored
This commit is contained in:
+11
-4
@@ -50,10 +50,10 @@ class Curl
|
||||
private $headers = array();
|
||||
private $options = array();
|
||||
|
||||
private $jsonDecoder = '\Curl\Decoder::decodeJson';
|
||||
public $jsonDecoder = null;
|
||||
private $jsonDecoderArgs = array();
|
||||
private $jsonPattern = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i';
|
||||
private $xmlDecoder = '\Curl\Decoder::decodeXml';
|
||||
public $xmlDecoder = null;
|
||||
private $xmlPattern = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i';
|
||||
private $defaultDecoder = null;
|
||||
|
||||
@@ -342,6 +342,13 @@ class Curl
|
||||
{
|
||||
$this->attempts += 1;
|
||||
|
||||
if ($this->jsonDecoder === null) {
|
||||
$this->setDefaultJsonDecoder();
|
||||
}
|
||||
if ($this->xmlDecoder === null) {
|
||||
$this->setDefaultXmlDecoder();
|
||||
}
|
||||
|
||||
if ($ch === null) {
|
||||
$this->responseCookies = array();
|
||||
$this->call($this->beforeSendCallback);
|
||||
@@ -881,9 +888,9 @@ class Curl
|
||||
$this->defaultDecoder = $mixed;
|
||||
} else {
|
||||
if ($mixed === 'json') {
|
||||
$this->defaultDecoder = $this->jsonDecoder;
|
||||
$this->defaultDecoder = '\Curl\Decoder::decodeJson';
|
||||
} elseif ($mixed === 'xml') {
|
||||
$this->defaultDecoder = $this->xmlDecoder;
|
||||
$this->defaultDecoder = '\Curl\Decoder::decodeXml';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -820,12 +820,18 @@ class MultiCurl
|
||||
$curl->complete($this->completeCallback);
|
||||
}
|
||||
|
||||
// Set decoders if not already individually set.
|
||||
if ($curl->jsonDecoder === null) {
|
||||
$curl->setJsonDecoder($this->jsonDecoder);
|
||||
}
|
||||
if ($curl->xmlDecoder === null) {
|
||||
$curl->setXmlDecoder($this->xmlDecoder);
|
||||
}
|
||||
|
||||
$curl->setOpts($this->options);
|
||||
$curl->setHeaders($this->headers);
|
||||
$curl->setRetry($this->retry);
|
||||
$curl->setCookies($this->cookies);
|
||||
$curl->setJsonDecoder($this->jsonDecoder);
|
||||
$curl->setXmlDecoder($this->xmlDecoder);
|
||||
|
||||
$curlm_error_code = curl_multi_add_handle($this->multiCurl, $curl->curl);
|
||||
if (!($curlm_error_code === CURLM_OK)) {
|
||||
|
||||
@@ -2077,6 +2077,30 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
|
||||
'value' => 'application/json',
|
||||
);
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'json_response');
|
||||
$multi_curl->setJsonDecoder(function ($response) {
|
||||
return 'first decoder';
|
||||
});
|
||||
|
||||
$post_1 = $multi_curl->addPost(Test::TEST_URL, $data);
|
||||
$post_1->complete(function ($instance) {
|
||||
\PHPUnit\Framework\Assert::assertEquals('first decoder', $instance->response);
|
||||
});
|
||||
|
||||
$post_2 = $multi_curl->addPost(Test::TEST_URL, $data);
|
||||
$post_2->setJsonDecoder(function ($response) {
|
||||
return 'second decoder';
|
||||
});
|
||||
$post_2->complete(function ($instance) {
|
||||
\PHPUnit\Framework\Assert::assertEquals('second decoder', $instance->response);
|
||||
});
|
||||
|
||||
$multi_curl->start();
|
||||
$this->assertEquals('first decoder', $post_1->response);
|
||||
$this->assertEquals('second decoder', $post_2->response);
|
||||
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'json_response');
|
||||
|
||||
@@ -2152,6 +2176,30 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
public function testXMLDecoder()
|
||||
{
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'xml_with_cdata_response');
|
||||
$multi_curl->setXmlDecoder(function ($response) {
|
||||
return 'first decoder';
|
||||
});
|
||||
|
||||
$post_1 = $multi_curl->addPost(Test::TEST_URL);
|
||||
$post_1->complete(function ($instance) {
|
||||
\PHPUnit\Framework\Assert::assertEquals('first decoder', $instance->response);
|
||||
});
|
||||
|
||||
$post_2 = $multi_curl->addPost(Test::TEST_URL);
|
||||
$post_2->setXmlDecoder(function ($response) {
|
||||
return 'second decoder';
|
||||
});
|
||||
$post_2->complete(function ($instance) {
|
||||
\PHPUnit\Framework\Assert::assertEquals('second decoder', $instance->response);
|
||||
});
|
||||
|
||||
$multi_curl->start();
|
||||
$this->assertEquals('first decoder', $post_1->response);
|
||||
$this->assertEquals('second decoder', $post_2->response);
|
||||
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'xml_with_cdata_response');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user