TrackManiaControl/core/Files/AsynchronousFileReader.php

121 lines
3.5 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-02-18 18:13:59 +01:00
use cURL\Request;
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>
2015-01-19 11:06:20 +01:00
* @copyright 2014-2015 ManiaControl Team
2014-05-02 17:40:47 +02:00
* @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 {
/*
* Constants
*/
const CONTENT_TYPE_JSON = 'application/json';
/*
* Private properties
2014-02-07 00:19:53 +01:00
*/
/** @var ManiaControl $maniaControl */
2014-02-07 00:19:53 +01:00
private $maniaControl = null;
2014-06-27 00:17:02 +02:00
/** @var Request[] $requests */
2014-02-18 18:13:59 +01:00
private $requests = array();
2014-02-07 00:19:53 +01:00
/**
2014-06-27 00:17:02 +02:00
* Construct a new Asynchronous File Reader Instance
2014-02-07 00:19:53 +01:00
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
2014-07-17 20:02:23 +02:00
public static function newRequestTest($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
->set(CURLOPT_ENCODING, '')// accept encoding
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent
2014-07-17 20:02:23 +02:00
->set(CURLOPT_RETURNTRANSFER, true); // return instead of output content
return $request;
}
2014-02-07 14:17:03 +01:00
/**
2014-06-27 00:17:02 +02:00
* Append available Data of active Requests
2014-02-07 14:17:03 +01:00
*/
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-07-17 20:02:23 +02:00
if ($request->socketPerform()) {
$request->socketSelect();
} else {
unset($this->requests[$key]);
}
2014-02-17 00:38:32 +01:00
}
2014-02-07 00:19:53 +01:00
}
2015-10-13 19:12:27 +02:00
/**
* Create a new cURL Request for the given URL, DO NOT CALL MANUALLY!
*
* @param string $url
* @return Request
*/
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
->set(CURLOPT_ENCODING, '')// accept encoding
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent
->set(CURLOPT_RETURNTRANSFER, true); // return instead of output content
return $request;
}
2014-02-15 13:52:46 +01:00
/**
* Load a Remote File
2014-02-15 13:52:46 +01:00
*
* @param string $url
* @param callable $function
* @param string $contentType
* @param int $keepAlive
* @param array $headers Additional Headers
2015-09-30 22:51:12 +02:00
* @deprecated @see ManiaControl\Files\AsyncHttpRequest
2014-02-15 13:52:46 +01:00
*/
public function loadFile($url, callable $function, $contentType = 'UTF-8', $keepAlive = 0, $headers = array()) {
2015-09-30 22:42:52 +02:00
$httpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$httpRequest->setCallable($function)->setContentType($contentType)->setHeaders($headers);
$httpRequest->getData($keepAlive);
2014-02-15 13:52:46 +01:00
}
2014-02-18 23:29:13 +01:00
/**
* Send Data via POST Method
*
2014-06-27 00:17:02 +02:00
* @param string $url
* @param callable $function
* @param string $content
* @param bool $compression
* @param string $contentType
* @param array $headers Additional Headers
2015-09-30 22:51:12 +02:00
* @deprecated @see ManiaControl\Files\AsyncHttpRequest
2014-02-18 23:29:13 +01:00
*/
public function postData($url, callable $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8;', $headers = array()) {
2015-09-30 22:33:48 +02:00
$httpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$httpRequest->setCallable($function)->setContent($content)->setCompression($compression)->setContentType($contentType)->setHeaders($headers);
$httpRequest->postData();
2014-02-07 00:19:53 +01:00
}
2015-09-30 22:51:12 +02:00
/**
* Add a Request to the queue, DO NOT CALL MANUALLY!
*
* @param Request $request
*/
public function addRequest(Request $request) {
array_push($this->requests, $request);
}
2014-06-27 00:17:02 +02:00
}