TrackManiaControl/core/Files/AsynchronousFileReader.php

102 lines
2.7 KiB
PHP
Raw Permalink 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;
use cURL\RequestsQueue;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
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>
2020-01-22 10:39:35 +01:00
* @copyright 2014-2020 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
*/
class AsynchronousFileReader implements UsageInformationAble {
use UsageInformationTrait;
/*
* 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;
/** @var \cURL\RequestsQueue|null $requestQueue */
private $requestQueue = null;
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;
$this->requestQueue = new RequestsQueue();
2014-02-07 00:19:53 +01:00
}
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() {
do {
if (($count = $this->requestQueue->count()) == 0) {
break;
}
if ($this->requestQueue->socketPerform()) {
$this->requestQueue->socketSelect();
}
} while ($count != $this->requestQueue->count());
2014-02-07 00:19:53 +01:00
}
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) {
$request->attachTo($this->requestQueue);
2015-09-30 22:51:12 +02:00
}
2014-06-27 00:17:02 +02:00
}