TrackManiaControl/application/core/Files/AsynchronousFileReader.php

178 lines
4.6 KiB
PHP
Raw Normal View History

2014-02-07 00:19:53 +01:00
<?php
2014-02-07 00:26:32 +01:00
namespace ManiaControl\Files;
2014-02-07 00:19:53 +01:00
2014-04-27 15:21:57 +02:00
use cURL\Event;
2014-02-18 18:13:59 +01:00
use cURL\Exception;
use cURL\Request;
use cURL\Response;
2014-02-07 00:19:53 +01:00
use ManiaControl\ManiaControl;
2014-02-07 12:30:53 +01:00
/**
* Asynchronous File Reader
*
2014-05-02 17:40:47 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-02-07 12:30:53 +01:00
*/
2014-02-07 19:33:14 +01:00
class AsynchronousFileReader {
/*
2014-02-07 00:19:53 +01:00
* Private Properties
*/
private $maniaControl = null;
2014-02-18 18:13:59 +01:00
private $requests = array();
2014-02-07 00:19:53 +01:00
/**
* Construct FileReader
2014-02-07 00:19:53 +01:00
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
2014-02-07 14:17:03 +01:00
/**
* Appends the Data
*/
2014-02-07 00:19:53 +01:00
public function appendData() {
2014-05-02 17:40:47 +02:00
foreach ($this->requests as $key => $request) {
2014-02-18 18:13:59 +01:00
/** @var Request $request */
try {
if ($request->socketPerform()) {
$request->socketSelect();
2014-02-17 00:34:20 +01:00
}
2014-05-02 17:40:47 +02:00
} catch (Exception $e) {
if ($e->getMessage() === 'Cannot perform if there are no requests in queue.') {
2014-02-18 18:13:59 +01:00
unset($this->requests[$key]);
} else {
throw $e;
2014-02-16 22:38:14 +01:00
}
2014-02-07 00:19:53 +01:00
}
2014-02-17 00:38:32 +01:00
}
2014-02-07 00:19:53 +01:00
}
2014-02-15 13:52:46 +01:00
/**
2014-02-18 18:13:59 +01:00
* Load a remote file
2014-02-15 13:52:46 +01:00
*
2014-05-02 17:40:47 +02:00
* @param string $url
* @param callable $function
* @param string $contentType
* @param int $keepAlive
2014-02-18 18:13:59 +01:00
* @return bool
2014-02-15 13:52:46 +01:00
*/
2014-02-23 17:55:32 +01:00
public function loadFile($url, $function, $contentType = 'UTF-8', $keepAlive = 0) {
2014-02-18 18:13:59 +01:00
if (!is_callable($function)) {
$this->maniaControl->log("Function is not callable");
return false;
2014-02-15 13:52:46 +01:00
}
2014-02-15 15:51:37 +01:00
2014-02-18 18:13:59 +01:00
if (!$url) {
2014-02-18 18:44:52 +01:00
$this->maniaControl->log("Url is empty");
return false;
2014-02-16 22:38:14 +01:00
}
2014-02-15 13:52:46 +01:00
2014-02-24 09:49:29 +01:00
if ($keepAlive) {
$header = array("Content-Type: " . $contentType, "Keep-Alive: " . $keepAlive, "Connection: Keep-Alive");
} else {
2014-02-23 17:55:32 +01:00
$header = array("Content-Type: " . $contentType);
}
2014-02-18 18:44:52 +01:00
$request = new Request($url);
2014-02-15 13:52:46 +01:00
$request->getOptions()->set(CURLOPT_TIMEOUT, 10) //
2014-02-18 23:29:13 +01:00
->set(CURLOPT_HEADER, false) //don't display response header
->set(CURLOPT_CRLF, true) //linux linefeed
->set(CURLOPT_ENCODING, '')//accept encoding
2014-02-18 23:29:13 +01:00
->set(CURLOPT_AUTOREFERER, true)//accept link reference
2014-02-23 17:55:32 +01:00
->set(CURLOPT_HTTPHEADER, $header) //
2014-02-20 17:59:09 +01:00
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) //
2014-02-18 18:13:59 +01:00
->set(CURLOPT_RETURNTRANSFER, true);
2014-02-15 13:52:46 +01:00
2014-04-27 15:21:57 +02:00
$request->addListener('complete', function (Event $event) use (&$function) {
2014-02-18 18:13:59 +01:00
$response = $event->response;
$error = null;
$content = null;
2014-02-18 18:13:59 +01:00
if ($response->hasError()) {
$error = $response->getError()->getMessage();
} else {
$content = $response->getContent();
2014-02-17 00:34:20 +01:00
}
2014-02-15 14:05:15 +01:00
2014-02-18 18:13:59 +01:00
call_user_func($function, $content, $error);
});
2014-02-18 18:44:52 +01:00
$this->addRequest($request);
2014-02-18 18:13:59 +01:00
return true;
2014-02-15 13:52:46 +01:00
}
2014-02-16 22:38:14 +01:00
/**
2014-02-18 18:44:52 +01:00
* Adds a Request to the queue
2014-02-18 23:29:13 +01:00
*
2014-02-18 18:44:52 +01:00
* @param Request $request
2014-02-16 22:38:14 +01:00
*/
2014-02-18 23:29:13 +01:00
public function addRequest(Request $request) {
2014-02-18 18:44:52 +01:00
array_push($this->requests, $request);
}
2014-02-16 22:38:14 +01:00
2014-02-18 23:29:13 +01:00
/**
* Send Data via POST Method
*
2014-05-02 17:40:47 +02:00
* @param string $url
* @param callable $function
* @param string $content
* @param bool $compression
* @param string $contentType
2014-02-18 23:29:13 +01:00
* @return bool
*/
public function postData($url, $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8') {
if (!is_callable($function)) {
$this->maniaControl->log("Function is not callable");
return false;
}
if (!$url) {
$this->maniaControl->log("Url is empty");
return false;
}
2014-02-21 16:07:36 +01:00
$content = str_replace(array("\r", "\n"), '', $content);
2014-02-18 23:29:13 +01:00
if ($compression) {
2014-02-24 09:49:29 +01:00
$content = zlib_encode($content, 31);
2014-02-21 16:07:36 +01:00
$header = array("Content-Type: " . $contentType, "Keep-Alive: 300", "Connection: Keep-Alive", "Content-Encoding: gzip");
} else {
$header = array("Content-Type: " . $contentType, "Keep-Alive: 300", "Connection: Keep-Alive");
2014-02-18 23:29:13 +01:00
}
2014-02-20 17:59:09 +01:00
2014-02-21 16:07:36 +01:00
2014-02-18 23:29:13 +01:00
$request = new Request($url);
$request->getOptions()->set(CURLOPT_HEADER, false) //don't display response header
->set(CURLOPT_CRLF, true) //linux linefeed
->set(CURLOPT_ENCODING, '')//accept encoding
2014-02-18 23:29:13 +01:00
//->set(CURLOPT_AUTOREFERER, true)//accept link reference
->set(CURLOPT_POST, true) //post field
->set(CURLOPT_POSTFIELDS, $content) //post content field
2014-02-21 16:07:36 +01:00
->set(CURLOPT_HTTPHEADER, $header) //
2014-02-20 17:59:09 +01:00
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) //
2014-05-09 09:39:32 +02:00
->set(CURLOPT_RETURNTRANSFER, true) //
->set(CURLOPT_TIMEOUT, 10);
2014-02-18 23:29:13 +01:00
2014-04-27 15:21:57 +02:00
$request->addListener('complete', function (Event $event) use (&$function) {
2014-02-18 23:29:13 +01:00
/** @var Response $response */
$response = $event->response;
$error = null;
$content = null;
2014-02-18 23:29:13 +01:00
if ($response->hasError()) {
$error = $response->getError()->getMessage();
} else {
$content = $response->getContent();
}
call_user_func($function, $content, $error);
});
$this->addRequest($request);
return true;
2014-02-07 00:19:53 +01:00
}
}