restructuring filereader stage 2

This commit is contained in:
kremsy 2015-09-30 22:33:48 +02:00
parent 62105611bf
commit 8aa0bbba86
2 changed files with 20 additions and 33 deletions

View File

@ -8,13 +8,9 @@ use cURL\Event;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
class AsyncHttpRequest { class AsyncHttpRequest {
const TYPE_GET = "GET";
const TYPE_POST = "POST";
/** @var ManiaControl $maniaControl */ /** @var ManiaControl $maniaControl */
private $maniaControl; private $maniaControl;
private $type;
private $url; private $url;
private $function; private $function;
private $content; private $content;
@ -22,9 +18,8 @@ class AsyncHttpRequest {
private $contentType = 'text/xml; charset=UTF-8;'; private $contentType = 'text/xml; charset=UTF-8;';
private $headers = array(); private $headers = array();
public function __construct($maniaControl, $type, $url) { public function __construct($maniaControl, $url) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->type = $type;
$this->url = $url; $this->url = $url;
} }
@ -127,4 +122,20 @@ class AsyncHttpRequest {
$this->headers = $headers; $this->headers = $headers;
return $this; return $this;
} }
/**
* @return string
*/
public function getContentType() {
return $this->contentType;
}
/**
* @param string $contentType
* @return $this
*/
public function setContentType($contentType) {
$this->contentType = $contentType;
return $this;
}
} }

View File

@ -164,32 +164,8 @@ class AsynchronousFileReader {
* @param array $headers Additional Headers * @param array $headers Additional Headers
*/ */
public function postData($url, callable $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8;', $headers = array()) { public function postData($url, callable $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8;', $headers = array()) {
array_push($headers, 'Content-Type: ' . $contentType); $httpRequest = new AsyncHttpRequest($this->maniaControl, $url);
array_push($headers, 'Keep-Alive: timeout=600, max=2000'); $httpRequest->setCallable($function)->setContent($content)->setCompression($compression)->setContentType($contentType)->setHeaders($headers);
array_push($headers, 'Connection: Keep-Alive'); $httpRequest->postData();
$content = str_replace(array("\r", "\n"), '', $content);
if ($compression) {
$content = zlib_encode($content, 31);
array_push($headers, 'Content-Encoding: gzip');
}
$request = $this->newRequest($url);
$request->getOptions()->set(CURLOPT_POST, true)// post method
->set(CURLOPT_POSTFIELDS, $content)// post content field
->set(CURLOPT_HTTPHEADER, $headers) // headers
;
$request->addListener('complete', function (Event $event) use (&$function) {
$error = null;
$content = null;
if ($event->response->hasError()) {
$error = $event->response->getError()->getMessage();
} else {
$content = $event->response->getContent();
}
call_user_func($function, $content, $error);
});
$this->addRequest($request);
} }
} }