From 62105611bf54ed45758bcabd074d756c73107f2a Mon Sep 17 00:00:00 2001 From: kremsy Date: Wed, 30 Sep 2015 22:28:16 +0200 Subject: [PATCH] started restructuring filereader --- core/Files/AsyncHttpRequest.php | 130 ++++++++++++++++++++++++++ core/Files/AsynchronousFileReader.php | 8 +- 2 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 core/Files/AsyncHttpRequest.php diff --git a/core/Files/AsyncHttpRequest.php b/core/Files/AsyncHttpRequest.php new file mode 100644 index 00000000..f43c48d1 --- /dev/null +++ b/core/Files/AsyncHttpRequest.php @@ -0,0 +1,130 @@ +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; + } +} \ No newline at end of file diff --git a/core/Files/AsynchronousFileReader.php b/core/Files/AsynchronousFileReader.php index fc71cc18..ed8954d0 100644 --- a/core/Files/AsynchronousFileReader.php +++ b/core/Files/AsynchronousFileReader.php @@ -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 * @return Request */ - protected function newRequest($url) { + public function newRequest($url) { $request = new Request($url); $request->getOptions()->set(CURLOPT_TIMEOUT, 60)->set(CURLOPT_HEADER, false)// don't display response header ->set(CURLOPT_CRLF, true)// linux line feed @@ -112,11 +112,11 @@ class AsynchronousFileReader { //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 */ - protected function addRequest(Request $request) { + public function addRequest(Request $request) { array_push($this->requests, $request); }