started restructuring filereader
This commit is contained in:
parent
48076587e0
commit
62105611bf
130
core/Files/AsyncHttpRequest.php
Normal file
130
core/Files/AsyncHttpRequest.php
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
//TODO documentation, finishing
|
||||||
|
|
||||||
|
namespace ManiaControl\Files;
|
||||||
|
|
||||||
|
use cURL\Event;
|
||||||
|
use ManiaControl\ManiaControl;
|
||||||
|
|
||||||
|
class AsyncHttpRequest {
|
||||||
|
const TYPE_GET = "GET";
|
||||||
|
const TYPE_POST = "POST";
|
||||||
|
|
||||||
|
/** @var ManiaControl $maniaControl */
|
||||||
|
private $maniaControl;
|
||||||
|
|
||||||
|
private $type;
|
||||||
|
private $url;
|
||||||
|
private $function;
|
||||||
|
private $content;
|
||||||
|
private $compression = false;
|
||||||
|
private $contentType = 'text/xml; charset=UTF-8;';
|
||||||
|
private $headers = array();
|
||||||
|
|
||||||
|
public function __construct($maniaControl, $type, $url) {
|
||||||
|
$this->maniaControl = $maniaControl;
|
||||||
|
$this->type = $type;
|
||||||
|
$this->url = $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postData() {
|
||||||
|
array_push($this->headers, 'Content-Type: ' . $this->contentType);
|
||||||
|
array_push($this->headers, 'Keep-Alive: timeout=600, max=2000');
|
||||||
|
array_push($this->headers, 'Connection: Keep-Alive');
|
||||||
|
|
||||||
|
$content = str_replace(array("\r", "\n"), '', $this->content);
|
||||||
|
if ($this->compression) {
|
||||||
|
$content = zlib_encode($content, 31);
|
||||||
|
array_push($headers, 'Content-Encoding: gzip');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$fileReader = new AsynchronousFileReader($this->maniaControl);
|
||||||
|
|
||||||
|
$request = $fileReader->newRequest($this->url);
|
||||||
|
$request->getOptions()->set(CURLOPT_POST, true)// post method
|
||||||
|
->set(CURLOPT_POSTFIELDS, $content)// post content field
|
||||||
|
->set(CURLOPT_HTTPHEADER, $this->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);
|
||||||
|
});
|
||||||
|
|
||||||
|
$fileReader->addRequest($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setURL($url) {
|
||||||
|
$this->url = $url;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $function
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setCallable($function) {
|
||||||
|
$this->function = $function;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getContent() {
|
||||||
|
return $this->content;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $content
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setContent($content) {
|
||||||
|
$this->content = $content;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function getCompression() {
|
||||||
|
return $this->compression;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param boolean $compression
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setCompression($compression) {
|
||||||
|
$this->compression = $compression;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getHeaders() {
|
||||||
|
return $this->headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $headers
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setHeaders($headers) {
|
||||||
|
$this->headers = $headers;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
@ -94,12 +94,12 @@ class AsynchronousFileReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new cURL Request for the given URL
|
* Create a new cURL Request for the given URL, DO NOT CALL MANUALLY!
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @return Request
|
* @return Request
|
||||||
*/
|
*/
|
||||||
protected function newRequest($url) {
|
public function newRequest($url) {
|
||||||
$request = new Request($url);
|
$request = new Request($url);
|
||||||
$request->getOptions()->set(CURLOPT_TIMEOUT, 60)->set(CURLOPT_HEADER, false)// don't display response header
|
$request->getOptions()->set(CURLOPT_TIMEOUT, 60)->set(CURLOPT_HEADER, false)// don't display response header
|
||||||
->set(CURLOPT_CRLF, true)// linux line feed
|
->set(CURLOPT_CRLF, true)// linux line feed
|
||||||
@ -112,11 +112,11 @@ class AsynchronousFileReader {
|
|||||||
//TODO remove, they are just for testing dedimania
|
//TODO remove, they are just for testing dedimania
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a Request to the queue
|
* Add a Request to the queue, DO NOT CALL MANUALLY!
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
*/
|
*/
|
||||||
protected function addRequest(Request $request) {
|
public function addRequest(Request $request) {
|
||||||
array_push($this->requests, $request);
|
array_push($this->requests, $request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user