From 688ff7be0cc3d15c160990a60fe235c19e7c3d44 Mon Sep 17 00:00:00 2001 From: CJMAXiK Date: Tue, 11 Apr 2017 10:39:13 +1000 Subject: [PATCH 01/11] Road to Caching, Requests was extracted to own class --- .gitignore | 1 + .travis.yml | 1 + Tests/ClientTest.php | 8 +- composer.json | 100 ++--- composer.lock | 463 +++++++++++---------- readme.md | 44 +- src/API/APIClient.php | 101 ++--- src/API/Request.php | 57 +++ src/Exceptions/PlayerNotFoundException.php | 2 +- src/Types/Ban.php | 19 +- src/Types/Bans.php | 48 ++- src/Types/GameTime.php | 15 +- src/Types/Player.php | 30 +- src/Types/Server.php | 17 + src/Types/Servers.php | 16 +- src/Types/Version.php | 26 +- 16 files changed, 560 insertions(+), 388 deletions(-) create mode 100644 src/API/Request.php diff --git a/.gitignore b/.gitignore index 8efa18b..e0f7956 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ ### Composer ### composer.phar /vendor/ +/.idea diff --git a/.travis.yml b/.travis.yml index deaa758..a3caff7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ cache: php: - 5.6 - 7.0 + - 7.1 - hhvm before_install: diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index e432bf6..81c5c3c 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -26,13 +26,7 @@ public function __construct() { parent::__construct(); - $config = [ - - ]; - $guzzle = new GuzzleClient($config); - $adapter = new GuzzleAdapter($guzzle); - - $this->client = new APIClient($adapter); + $this->client = new APIClient(); } public function testPlayer() diff --git a/composer.json b/composer.json index 342dbf1..3a3238f 100644 --- a/composer.json +++ b/composer.json @@ -1,52 +1,58 @@ { - "name": "truckersmp/api-client", - "description": "TruckersMP API Library helps connecting to and using the TruckersMP API.", - "homepage": "https://truckersmp.com/", - "license": "MIT", - "authors": [ - { - "name": "HumaneWolf", - "email": "humanewolf@truckersmp.com", - "homepage": "https://humanewolf.com/", - "role": "CM/Dev" - }, - { - "name": "Thor Erik (Tuxy Fluffyclaws) Lie", - "email": "tuxy@truckersmp.com", - "homepage": "https://truckersmp.com/", - "role": "Dev" - } - ], - "require": { - "php": ">=5.6.0", - "nesbot/carbon": "^1.21", - "php-http/client-implementation": "^1.0", - "php-http/message": "^1.2", - "guzzlehttp/psr7": "^1.3" + "name": "truckersmp/api-client", + "description": "TruckersMP API Library helps connecting to and using the TruckersMP API.", + "homepage": "https://truckersmp.com/", + "license": "MIT", + "authors": [ + { + "name": "HumaneWolf", + "email": "humanewolf@truckersmp.com", + "homepage": "https://humanewolf.com/", + "role": "CM/Dev" }, - "require-dev": { - "phpunit/phpunit": "^5.3", - "squizlabs/php_codesniffer": "^2.6", - "php-http/guzzle6-adapter": "^1.0" + { + "name": "Thor Erik (Tuxy Fluffyclaws) Lie", + "email": "tuxy@truckersmp.com", + "homepage": "https://truckersmp.com/", + "role": "Dev" }, - "autoload": { - "psr-4": { - "TruckersMP\\": "src/" - }, - "classmap": [ - "src/" - ] + { + "name": "CJMAXiK", + "email": "me@cjmaxik.ru", + "homepage": "https://cjmaxik.ru", + "role": "IGA/Dev" + } + ], + "require": { + "php": ">=5.6.0", + "nesbot/carbon": "^1.21", + "php-http/client-implementation": "^1.0", + "php-http/message": "^1.2", + "guzzlehttp/psr7": "^1.3" + }, + "require-dev": { + "phpunit/phpunit": "^5.3", + "squizlabs/php_codesniffer": "^2.6", + "php-http/guzzle6-adapter": "^1.0" + }, + "autoload": { + "psr-4": { + "TruckersMP\\": "src/" }, - "autoload-dev": { - "psr-4": { - "TruckersMP\\Tests\\API\\": "tests/" - } - }, - "scripts": { - "test": [ - "./vendor/bin/phpunit", - "./vendor/bin/phpcs src --standard=PSR2", - "./vendor/bin/phpcs Tests --standard=PSR2" - ] - } + "classmap": [ + "src/" + ] + }, + "autoload-dev": { + "psr-4": { + "TruckersMP\\Tests\\API\\": "tests/" + } + }, + "scripts": { + "test": [ + "./vendor/bin/phpunit", + "./vendor/bin/phpcs src --standard=PSR2", + "./vendor/bin/phpcs Tests --standard=PSR2" + ] + } } diff --git a/composer.lock b/composer.lock index c6ea915..3943a75 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,6 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "71ec3163663f18416c9bf2d1e0cb65fa", "content-hash": "2c1d4698425cf2ca8f19291e44fd8693", "packages": [ { @@ -54,31 +53,31 @@ "stream_filter_append", "stream_filter_register" ], - "time": "2015-11-08 23:41:30" + "time": "2015-11-08T23:41:30+00:00" }, { "name": "guzzlehttp/guzzle", - "version": "6.2.0", + "version": "6.2.3", "source": { "type": "git", "url": "https://github.com/guzzle/guzzle.git", - "reference": "d094e337976dff9d8e2424e8485872194e768662" + "reference": "8d6c6cc55186db87b7dc5009827429ba4e9dc006" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/guzzle/zipball/d094e337976dff9d8e2424e8485872194e768662", - "reference": "d094e337976dff9d8e2424e8485872194e768662", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/8d6c6cc55186db87b7dc5009827429ba4e9dc006", + "reference": "8d6c6cc55186db87b7dc5009827429ba4e9dc006", "shasum": "" }, "require": { - "guzzlehttp/promises": "~1.0", - "guzzlehttp/psr7": "~1.1", - "php": ">=5.5.0" + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.4", + "php": ">=5.5" }, "require-dev": { "ext-curl": "*", - "phpunit/phpunit": "~4.0", - "psr/log": "~1.0" + "phpunit/phpunit": "^4.0", + "psr/log": "^1.0" }, "type": "library", "extra": { @@ -116,32 +115,32 @@ "rest", "web service" ], - "time": "2016-03-21 20:02:09" + "time": "2017-02-28T22:50:30+00:00" }, { "name": "guzzlehttp/promises", - "version": "1.2.0", + "version": "v1.3.1", "source": { "type": "git", "url": "https://github.com/guzzle/promises.git", - "reference": "c10d860e2a9595f8883527fa0021c7da9e65f579" + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/promises/zipball/c10d860e2a9595f8883527fa0021c7da9e65f579", - "reference": "c10d860e2a9595f8883527fa0021c7da9e65f579", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", "shasum": "" }, "require": { "php": ">=5.5.0" }, "require-dev": { - "phpunit/phpunit": "~4.0" + "phpunit/phpunit": "^4.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0-dev" + "dev-master": "1.4-dev" } }, "autoload": { @@ -167,20 +166,20 @@ "keywords": [ "promise" ], - "time": "2016-05-18 16:56:05" + "time": "2016-12-20T10:07:11+00:00" }, { "name": "guzzlehttp/psr7", - "version": "1.3.1", + "version": "1.4.2", "source": { "type": "git", "url": "https://github.com/guzzle/psr7.git", - "reference": "5c6447c9df362e8f8093bda8f5d8873fe5c7f65b" + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/psr7/zipball/5c6447c9df362e8f8093bda8f5d8873fe5c7f65b", - "reference": "5c6447c9df362e8f8093bda8f5d8873fe5c7f65b", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c", + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c", "shasum": "" }, "require": { @@ -216,39 +215,52 @@ "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Schultze", + "homepage": "https://github.com/Tobion" } ], - "description": "PSR-7 message implementation", + "description": "PSR-7 message implementation that also provides common utility methods", "keywords": [ "http", "message", + "request", + "response", "stream", - "uri" + "uri", + "url" ], - "time": "2016-06-24 23:00:38" + "time": "2017-03-20T17:10:46+00:00" }, { "name": "nesbot/carbon", - "version": "1.21.0", + "version": "1.22.1", "source": { "type": "git", "url": "https://github.com/briannesbitt/Carbon.git", - "reference": "7b08ec6f75791e130012f206e3f7b0e76e18e3d7" + "reference": "7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/7b08ec6f75791e130012f206e3f7b0e76e18e3d7", - "reference": "7b08ec6f75791e130012f206e3f7b0e76e18e3d7", + "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc", + "reference": "7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc", "shasum": "" }, "require": { "php": ">=5.3.0", - "symfony/translation": "~2.6|~3.0" + "symfony/translation": "~2.6 || ~3.0" }, "require-dev": { - "phpunit/phpunit": "~4.0|~5.0" + "friendsofphp/php-cs-fixer": "~2", + "phpunit/phpunit": "~4.0 || ~5.0" }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.23-dev" + } + }, "autoload": { "psr-4": { "Carbon\\": "src/Carbon/" @@ -272,7 +284,7 @@ "datetime", "time" ], - "time": "2015-11-04 20:07:17" + "time": "2017-01-16T07:55:07+00:00" }, { "name": "php-http/guzzle6-adapter", @@ -332,20 +344,20 @@ "Guzzle", "http" ], - "time": "2016-05-10 06:13:32" + "time": "2016-05-10T06:13:32+00:00" }, { "name": "php-http/httplug", - "version": "v1.0.0", + "version": "v1.1.0", "source": { "type": "git", "url": "https://github.com/php-http/httplug.git", - "reference": "2061047ca53a08a6b8f52e997b2a76f386b397dd" + "reference": "1c6381726c18579c4ca2ef1ec1498fdae8bdf018" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-http/httplug/zipball/2061047ca53a08a6b8f52e997b2a76f386b397dd", - "reference": "2061047ca53a08a6b8f52e997b2a76f386b397dd", + "url": "https://api.github.com/repos/php-http/httplug/zipball/1c6381726c18579c4ca2ef1ec1498fdae8bdf018", + "reference": "1c6381726c18579c4ca2ef1ec1498fdae8bdf018", "shasum": "" }, "require": { @@ -388,20 +400,20 @@ "client", "http" ], - "time": "2016-01-26 14:34:50" + "time": "2016-08-31T08:30:17+00:00" }, { "name": "php-http/message", - "version": "v1.2.0", + "version": "1.5.0", "source": { "type": "git", "url": "https://github.com/php-http/message.git", - "reference": "c8fadec9533f5e34a8a730a947fa76363f3aa620" + "reference": "13df8c48f40ca7925303aa336f19be4b80984f01" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-http/message/zipball/c8fadec9533f5e34a8a730a947fa76363f3aa620", - "reference": "c8fadec9533f5e34a8a730a947fa76363f3aa620", + "url": "https://api.github.com/repos/php-http/message/zipball/13df8c48f40ca7925303aa336f19be4b80984f01", + "reference": "13df8c48f40ca7925303aa336f19be4b80984f01", "shasum": "" }, "require": { @@ -411,22 +423,25 @@ "psr/http-message": "^1.0" }, "require-dev": { + "akeneo/phpspec-skip-example-extension": "^1.0", "coduo/phpspec-data-provider-extension": "^1.0", "ext-zlib": "*", "guzzlehttp/psr7": "^1.0", "henrikbjorn/phpspec-code-coverage": "^1.0", "phpspec/phpspec": "^2.4", + "slim/slim": "^3.0", "zendframework/zend-diactoros": "^1.0" }, "suggest": { "ext-zlib": "Used with compressor/decompressor streams", "guzzlehttp/psr7": "Used with Guzzle PSR-7 Factories", + "slim/slim": "Used with Slim Framework PSR-7 implementation", "zendframework/zend-diactoros": "Used with Diactoros Factories" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.3-dev" + "dev-master": "1.6-dev" } }, "autoload": { @@ -454,7 +469,7 @@ "message", "psr-7" ], - "time": "2016-03-29 20:44:24" + "time": "2017-02-14T08:58:37+00:00" }, { "name": "php-http/message-factory", @@ -504,7 +519,7 @@ "stream", "uri" ], - "time": "2015-12-19 14:08:53" + "time": "2015-12-19T14:08:53+00:00" }, { "name": "php-http/promise", @@ -554,20 +569,20 @@ "keywords": [ "promise" ], - "time": "2016-01-26 13:27:02" + "time": "2016-01-26T13:27:02+00:00" }, { "name": "psr/http-message", - "version": "1.0", + "version": "1.0.1", "source": { "type": "git", "url": "https://github.com/php-fig/http-message.git", - "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298" + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-message/zipball/85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", - "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", "shasum": "" }, "require": { @@ -595,6 +610,7 @@ } ], "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", "keywords": [ "http", "http-message", @@ -603,20 +619,20 @@ "request", "response" ], - "time": "2015-05-04 20:22:00" + "time": "2016-08-06T14:39:51+00:00" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.2.0", + "version": "v1.3.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "dff51f72b0706335131b00a7f49606168c582594" + "reference": "e79d363049d1c2128f133a2667e4f4190904f7f4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/dff51f72b0706335131b00a7f49606168c582594", - "reference": "dff51f72b0706335131b00a7f49606168c582594", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/e79d363049d1c2128f133a2667e4f4190904f7f4", + "reference": "e79d363049d1c2128f133a2667e4f4190904f7f4", "shasum": "" }, "require": { @@ -628,7 +644,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.2-dev" + "dev-master": "1.3-dev" } }, "autoload": { @@ -662,20 +678,20 @@ "portable", "shim" ], - "time": "2016-05-18 14:26:46" + "time": "2016-11-14T01:06:16+00:00" }, { "name": "symfony/translation", - "version": "v3.1.2", + "version": "v3.2.7", "source": { "type": "git", "url": "https://github.com/symfony/translation.git", - "reference": "d63a94528530c3ea5ff46924c8001cec4a398609" + "reference": "c740eee70783d2af4d3d6b70d5146f209e6b4d13" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/translation/zipball/d63a94528530c3ea5ff46924c8001cec4a398609", - "reference": "d63a94528530c3ea5ff46924c8001cec4a398609", + "url": "https://api.github.com/repos/symfony/translation/zipball/c740eee70783d2af4d3d6b70d5146f209e6b4d13", + "reference": "c740eee70783d2af4d3d6b70d5146f209e6b4d13", "shasum": "" }, "require": { @@ -688,7 +704,7 @@ "require-dev": { "psr/log": "~1.0", "symfony/config": "~2.8|~3.0", - "symfony/intl": "~2.8|~3.0", + "symfony/intl": "^2.8.18|^3.2.5", "symfony/yaml": "~2.8|~3.0" }, "suggest": { @@ -699,7 +715,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.1-dev" + "dev-master": "3.2-dev" } }, "autoload": { @@ -726,7 +742,7 @@ ], "description": "Symfony Translation Component", "homepage": "https://symfony.com", - "time": "2016-06-29 05:41:56" + "time": "2017-03-21T21:44:32+00:00" } ], "packages-dev": [ @@ -782,20 +798,20 @@ "constructor", "instantiate" ], - "time": "2015-06-14 21:17:01" + "time": "2015-06-14T21:17:01+00:00" }, { "name": "myclabs/deep-copy", - "version": "1.5.1", + "version": "1.6.0", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "a8773992b362b58498eed24bf85005f363c34771" + "reference": "5a5a9fc8025a08d8919be87d6884d5a92520cefe" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/a8773992b362b58498eed24bf85005f363c34771", - "reference": "a8773992b362b58498eed24bf85005f363c34771", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/5a5a9fc8025a08d8919be87d6884d5a92520cefe", + "reference": "5a5a9fc8025a08d8919be87d6884d5a92520cefe", "shasum": "" }, "require": { @@ -824,7 +840,7 @@ "object", "object graph" ], - "time": "2015-11-20 12:04:31" + "time": "2017-01-26T22:05:40+00:00" }, { "name": "phpdocumentor/reflection-common", @@ -878,20 +894,20 @@ "reflection", "static analysis" ], - "time": "2015-12-27 11:43:31" + "time": "2015-12-27T11:43:31+00:00" }, { "name": "phpdocumentor/reflection-docblock", - "version": "3.1.0", + "version": "3.1.1", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "9270140b940ff02e58ec577c237274e92cd40cdd" + "reference": "8331b5efe816ae05461b7ca1e721c01b46bafb3e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/9270140b940ff02e58ec577c237274e92cd40cdd", - "reference": "9270140b940ff02e58ec577c237274e92cd40cdd", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/8331b5efe816ae05461b7ca1e721c01b46bafb3e", + "reference": "8331b5efe816ae05461b7ca1e721c01b46bafb3e", "shasum": "" }, "require": { @@ -923,20 +939,20 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2016-06-10 09:48:41" + "time": "2016-09-30T07:12:33+00:00" }, { "name": "phpdocumentor/type-resolver", - "version": "0.2", + "version": "0.2.1", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "b39c7a5b194f9ed7bd0dd345c751007a41862443" + "reference": "e224fb2ea2fba6d3ad6fdaef91cd09a172155ccb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/b39c7a5b194f9ed7bd0dd345c751007a41862443", - "reference": "b39c7a5b194f9ed7bd0dd345c751007a41862443", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/e224fb2ea2fba6d3ad6fdaef91cd09a172155ccb", + "reference": "e224fb2ea2fba6d3ad6fdaef91cd09a172155ccb", "shasum": "" }, "require": { @@ -970,31 +986,32 @@ "email": "me@mikevanriel.com" } ], - "time": "2016-06-10 07:14:17" + "time": "2016-11-25T06:54:22+00:00" }, { "name": "phpspec/prophecy", - "version": "v1.6.1", + "version": "v1.7.0", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "58a8137754bc24b25740d4281399a4a3596058e0" + "reference": "93d39f1f7f9326d746203c7c056f300f7f126073" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/58a8137754bc24b25740d4281399a4a3596058e0", - "reference": "58a8137754bc24b25740d4281399a4a3596058e0", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/93d39f1f7f9326d746203c7c056f300f7f126073", + "reference": "93d39f1f7f9326d746203c7c056f300f7f126073", "shasum": "" }, "require": { "doctrine/instantiator": "^1.0.2", "php": "^5.3|^7.0", "phpdocumentor/reflection-docblock": "^2.0|^3.0.2", - "sebastian/comparator": "^1.1", - "sebastian/recursion-context": "^1.0" + "sebastian/comparator": "^1.1|^2.0", + "sebastian/recursion-context": "^1.0|^2.0|^3.0" }, "require-dev": { - "phpspec/phpspec": "^2.0" + "phpspec/phpspec": "^2.5|^3.2", + "phpunit/phpunit": "^4.8 || ^5.6.5" }, "type": "library", "extra": { @@ -1032,39 +1049,39 @@ "spy", "stub" ], - "time": "2016-06-07 08:13:47" + "time": "2017-03-02T20:05:34+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "4.0.0", + "version": "4.0.8", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "900370c81280cc0d942ffbc5912d80464eaee7e9" + "reference": "ef7b2f56815df854e66ceaee8ebe9393ae36a40d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/900370c81280cc0d942ffbc5912d80464eaee7e9", - "reference": "900370c81280cc0d942ffbc5912d80464eaee7e9", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/ef7b2f56815df854e66ceaee8ebe9393ae36a40d", + "reference": "ef7b2f56815df854e66ceaee8ebe9393ae36a40d", "shasum": "" }, "require": { + "ext-dom": "*", + "ext-xmlwriter": "*", "php": "^5.6 || ^7.0", - "phpunit/php-file-iterator": "~1.3", - "phpunit/php-text-template": "~1.2", - "phpunit/php-token-stream": "^1.4.2", - "sebastian/code-unit-reverse-lookup": "~1.0", - "sebastian/environment": "^1.3.2", - "sebastian/version": "~1.0|~2.0" + "phpunit/php-file-iterator": "^1.3", + "phpunit/php-text-template": "^1.2", + "phpunit/php-token-stream": "^1.4.2 || ^2.0", + "sebastian/code-unit-reverse-lookup": "^1.0", + "sebastian/environment": "^1.3.2 || ^2.0", + "sebastian/version": "^1.0 || ^2.0" }, "require-dev": { - "ext-xdebug": ">=2.1.4", - "phpunit/phpunit": "^5.4" + "ext-xdebug": "^2.1.4", + "phpunit/phpunit": "^5.7" }, "suggest": { - "ext-dom": "*", - "ext-xdebug": ">=2.4.0", - "ext-xmlwriter": "*" + "ext-xdebug": "^2.5.1" }, "type": "library", "extra": { @@ -1095,20 +1112,20 @@ "testing", "xunit" ], - "time": "2016-06-03 05:03:56" + "time": "2017-04-02T07:44:40+00:00" }, { "name": "phpunit/php-file-iterator", - "version": "1.4.1", + "version": "1.4.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "6150bf2c35d3fc379e50c7602b75caceaa39dbf0" + "reference": "3cc8f69b3028d0f96a9078e6295d86e9bf019be5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/6150bf2c35d3fc379e50c7602b75caceaa39dbf0", - "reference": "6150bf2c35d3fc379e50c7602b75caceaa39dbf0", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/3cc8f69b3028d0f96a9078e6295d86e9bf019be5", + "reference": "3cc8f69b3028d0f96a9078e6295d86e9bf019be5", "shasum": "" }, "require": { @@ -1142,7 +1159,7 @@ "filesystem", "iterator" ], - "time": "2015-06-21 13:08:43" + "time": "2016-10-03T07:40:28+00:00" }, { "name": "phpunit/php-text-template", @@ -1183,29 +1200,34 @@ "keywords": [ "template" ], - "time": "2015-06-21 13:50:34" + "time": "2015-06-21T13:50:34+00:00" }, { "name": "phpunit/php-timer", - "version": "1.0.8", + "version": "1.0.9", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-timer.git", - "reference": "38e9124049cf1a164f1e4537caf19c99bf1eb260" + "reference": "3dcf38ca72b158baf0bc245e9184d3fdffa9c46f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/38e9124049cf1a164f1e4537caf19c99bf1eb260", - "reference": "38e9124049cf1a164f1e4537caf19c99bf1eb260", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/3dcf38ca72b158baf0bc245e9184d3fdffa9c46f", + "reference": "3dcf38ca72b158baf0bc245e9184d3fdffa9c46f", "shasum": "" }, "require": { - "php": ">=5.3.3" + "php": "^5.3.3 || ^7.0" }, "require-dev": { - "phpunit/phpunit": "~4|~5" + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, "autoload": { "classmap": [ "src/" @@ -1227,20 +1249,20 @@ "keywords": [ "timer" ], - "time": "2016-05-12 18:03:57" + "time": "2017-02-26T11:10:40+00:00" }, { "name": "phpunit/php-token-stream", - "version": "1.4.8", + "version": "1.4.11", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "3144ae21711fb6cac0b1ab4cbe63b75ce3d4e8da" + "reference": "e03f8f67534427a787e21a385a67ec3ca6978ea7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/3144ae21711fb6cac0b1ab4cbe63b75ce3d4e8da", - "reference": "3144ae21711fb6cac0b1ab4cbe63b75ce3d4e8da", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/e03f8f67534427a787e21a385a67ec3ca6978ea7", + "reference": "e03f8f67534427a787e21a385a67ec3ca6978ea7", "shasum": "" }, "require": { @@ -1276,50 +1298,54 @@ "keywords": [ "tokenizer" ], - "time": "2015-09-15 10:49:45" + "time": "2017-02-27T10:12:30+00:00" }, { "name": "phpunit/phpunit", - "version": "5.4.6", + "version": "5.7.19", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "2f1fc94b77ea6418bd6a06c64a1dac0645fbce59" + "reference": "69c4f49ff376af2692bad9cebd883d17ebaa98a1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/2f1fc94b77ea6418bd6a06c64a1dac0645fbce59", - "reference": "2f1fc94b77ea6418bd6a06c64a1dac0645fbce59", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/69c4f49ff376af2692bad9cebd883d17ebaa98a1", + "reference": "69c4f49ff376af2692bad9cebd883d17ebaa98a1", "shasum": "" }, "require": { "ext-dom": "*", "ext-json": "*", - "ext-pcre": "*", - "ext-reflection": "*", - "ext-spl": "*", + "ext-libxml": "*", + "ext-mbstring": "*", + "ext-xml": "*", "myclabs/deep-copy": "~1.3", "php": "^5.6 || ^7.0", - "phpspec/prophecy": "^1.3.1", - "phpunit/php-code-coverage": "^4.0", + "phpspec/prophecy": "^1.6.2", + "phpunit/php-code-coverage": "^4.0.4", "phpunit/php-file-iterator": "~1.4", "phpunit/php-text-template": "~1.2", "phpunit/php-timer": "^1.0.6", "phpunit/phpunit-mock-objects": "^3.2", - "sebastian/comparator": "~1.1", + "sebastian/comparator": "^1.2.4", "sebastian/diff": "~1.2", - "sebastian/environment": "^1.3 || ^2.0", - "sebastian/exporter": "~1.2", - "sebastian/global-state": "~1.0", - "sebastian/object-enumerator": "~1.0", + "sebastian/environment": "^1.3.4 || ^2.0", + "sebastian/exporter": "~2.0", + "sebastian/global-state": "^1.1", + "sebastian/object-enumerator": "~2.0", "sebastian/resource-operations": "~1.0", - "sebastian/version": "~1.0|~2.0", + "sebastian/version": "~1.0.3|~2.0", "symfony/yaml": "~2.1|~3.0" }, "conflict": { "phpdocumentor/reflection-docblock": "3.0.2" }, + "require-dev": { + "ext-pdo": "*" + }, "suggest": { + "ext-xdebug": "*", "phpunit/php-invoker": "~1.1" }, "bin": [ @@ -1328,7 +1354,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "5.4.x-dev" + "dev-master": "5.7.x-dev" } }, "autoload": { @@ -1354,27 +1380,27 @@ "testing", "xunit" ], - "time": "2016-06-16 06:01:15" + "time": "2017-04-03T02:22:27+00:00" }, { "name": "phpunit/phpunit-mock-objects", - "version": "3.2.3", + "version": "3.4.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "b13d0d9426ced06958bd32104653526a6c998a52" + "reference": "3ab72b65b39b491e0c011e2e09bb2206c2aa8e24" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/b13d0d9426ced06958bd32104653526a6c998a52", - "reference": "b13d0d9426ced06958bd32104653526a6c998a52", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/3ab72b65b39b491e0c011e2e09bb2206c2aa8e24", + "reference": "3ab72b65b39b491e0c011e2e09bb2206c2aa8e24", "shasum": "" }, "require": { "doctrine/instantiator": "^1.0.2", "php": "^5.6 || ^7.0", "phpunit/php-text-template": "^1.2", - "sebastian/exporter": "^1.2" + "sebastian/exporter": "^1.2 || ^2.0" }, "conflict": { "phpunit/phpunit": "<5.4.0" @@ -1413,27 +1439,27 @@ "mock", "xunit" ], - "time": "2016-06-12 07:37:26" + "time": "2016-12-08T20:27:08+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", - "version": "1.0.0", + "version": "1.0.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", - "reference": "c36f5e7cfce482fde5bf8d10d41a53591e0198fe" + "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/c36f5e7cfce482fde5bf8d10d41a53591e0198fe", - "reference": "c36f5e7cfce482fde5bf8d10d41a53591e0198fe", + "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", + "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", "shasum": "" }, "require": { - "php": ">=5.6" + "php": "^5.6 || ^7.0" }, "require-dev": { - "phpunit/phpunit": "~5" + "phpunit/phpunit": "^5.7 || ^6.0" }, "type": "library", "extra": { @@ -1458,26 +1484,26 @@ ], "description": "Looks up which function or method a line of code belongs to", "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", - "time": "2016-02-13 06:45:14" + "time": "2017-03-04T06:30:41+00:00" }, { "name": "sebastian/comparator", - "version": "1.2.0", + "version": "1.2.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "937efb279bd37a375bcadf584dec0726f84dbf22" + "reference": "2b7424b55f5047b47ac6e5ccb20b2aea4011d9be" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/937efb279bd37a375bcadf584dec0726f84dbf22", - "reference": "937efb279bd37a375bcadf584dec0726f84dbf22", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/2b7424b55f5047b47ac6e5ccb20b2aea4011d9be", + "reference": "2b7424b55f5047b47ac6e5ccb20b2aea4011d9be", "shasum": "" }, "require": { "php": ">=5.3.3", "sebastian/diff": "~1.2", - "sebastian/exporter": "~1.2" + "sebastian/exporter": "~1.2 || ~2.0" }, "require-dev": { "phpunit/phpunit": "~4.4" @@ -1522,7 +1548,7 @@ "compare", "equality" ], - "time": "2015-07-26 15:48:44" + "time": "2017-01-29T09:50:25+00:00" }, { "name": "sebastian/diff", @@ -1574,32 +1600,32 @@ "keywords": [ "diff" ], - "time": "2015-12-08 07:14:41" + "time": "2015-12-08T07:14:41+00:00" }, { "name": "sebastian/environment", - "version": "1.3.7", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "4e8f0da10ac5802913afc151413bc8c53b6c2716" + "reference": "5795ffe5dc5b02460c3e34222fee8cbe245d8fac" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/4e8f0da10ac5802913afc151413bc8c53b6c2716", - "reference": "4e8f0da10ac5802913afc151413bc8c53b6c2716", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/5795ffe5dc5b02460c3e34222fee8cbe245d8fac", + "reference": "5795ffe5dc5b02460c3e34222fee8cbe245d8fac", "shasum": "" }, "require": { - "php": ">=5.3.3" + "php": "^5.6 || ^7.0" }, "require-dev": { - "phpunit/phpunit": "~4.4" + "phpunit/phpunit": "^5.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.3.x-dev" + "dev-master": "2.0.x-dev" } }, "autoload": { @@ -1624,25 +1650,25 @@ "environment", "hhvm" ], - "time": "2016-05-17 03:18:57" + "time": "2016-11-26T07:53:53+00:00" }, { "name": "sebastian/exporter", - "version": "1.2.2", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4" + "reference": "ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/42c4c2eec485ee3e159ec9884f95b431287edde4", - "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4", + "reference": "ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4", "shasum": "" }, "require": { "php": ">=5.3.3", - "sebastian/recursion-context": "~1.0" + "sebastian/recursion-context": "~2.0" }, "require-dev": { "ext-mbstring": "*", @@ -1651,7 +1677,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.3.x-dev" + "dev-master": "2.0.x-dev" } }, "autoload": { @@ -1691,7 +1717,7 @@ "export", "exporter" ], - "time": "2016-06-17 09:04:28" + "time": "2016-11-19T08:54:04+00:00" }, { "name": "sebastian/global-state", @@ -1742,25 +1768,25 @@ "keywords": [ "global state" ], - "time": "2015-10-12 03:26:01" + "time": "2015-10-12T03:26:01+00:00" }, { "name": "sebastian/object-enumerator", - "version": "1.0.0", + "version": "2.0.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/object-enumerator.git", - "reference": "d4ca2fb70344987502567bc50081c03e6192fb26" + "reference": "1311872ac850040a79c3c058bea3e22d0f09cbb7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/d4ca2fb70344987502567bc50081c03e6192fb26", - "reference": "d4ca2fb70344987502567bc50081c03e6192fb26", + "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/1311872ac850040a79c3c058bea3e22d0f09cbb7", + "reference": "1311872ac850040a79c3c058bea3e22d0f09cbb7", "shasum": "" }, "require": { "php": ">=5.6", - "sebastian/recursion-context": "~1.0" + "sebastian/recursion-context": "~2.0" }, "require-dev": { "phpunit/phpunit": "~5" @@ -1768,7 +1794,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "2.0.x-dev" } }, "autoload": { @@ -1788,20 +1814,20 @@ ], "description": "Traverses array structures and object graphs to enumerate all referenced objects", "homepage": "https://github.com/sebastianbergmann/object-enumerator/", - "time": "2016-01-28 13:25:10" + "time": "2017-02-18T15:18:39+00:00" }, { "name": "sebastian/recursion-context", - "version": "1.0.2", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/recursion-context.git", - "reference": "913401df809e99e4f47b27cdd781f4a258d58791" + "reference": "2c3ba150cbec723aa057506e73a8d33bdb286c9a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/913401df809e99e4f47b27cdd781f4a258d58791", - "reference": "913401df809e99e4f47b27cdd781f4a258d58791", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/2c3ba150cbec723aa057506e73a8d33bdb286c9a", + "reference": "2c3ba150cbec723aa057506e73a8d33bdb286c9a", "shasum": "" }, "require": { @@ -1813,7 +1839,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "2.0.x-dev" } }, "autoload": { @@ -1841,7 +1867,7 @@ ], "description": "Provides functionality to recursively process PHP variables", "homepage": "http://www.github.com/sebastianbergmann/recursion-context", - "time": "2015-11-11 19:50:13" + "time": "2016-11-19T07:33:16+00:00" }, { "name": "sebastian/resource-operations", @@ -1883,20 +1909,20 @@ ], "description": "Provides a list of PHP built-in functions that operate on resources", "homepage": "https://www.github.com/sebastianbergmann/resource-operations", - "time": "2015-07-28 20:34:47" + "time": "2015-07-28T20:34:47+00:00" }, { "name": "sebastian/version", - "version": "2.0.0", + "version": "2.0.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/version.git", - "reference": "c829badbd8fdf16a0bad8aa7fa7971c029f1b9c5" + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/c829badbd8fdf16a0bad8aa7fa7971c029f1b9c5", - "reference": "c829badbd8fdf16a0bad8aa7fa7971c029f1b9c5", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/99732be0ddb3361e16ad77b68ba41efc8e979019", + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019", "shasum": "" }, "require": { @@ -1926,20 +1952,20 @@ ], "description": "Library that helps with managing the version number of Git-hosted PHP projects", "homepage": "https://github.com/sebastianbergmann/version", - "time": "2016-02-04 12:56:52" + "time": "2016-10-03T07:35:21+00:00" }, { "name": "squizlabs/php_codesniffer", - "version": "2.6.1", + "version": "2.8.1", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "fb72ed32f8418db5e7770be1653e62e0d6f5dd3d" + "reference": "d7cf0d894e8aa4c73712ee4a331cc1eaa37cdc7d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/fb72ed32f8418db5e7770be1653e62e0d6f5dd3d", - "reference": "fb72ed32f8418db5e7770be1653e62e0d6f5dd3d", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/d7cf0d894e8aa4c73712ee4a331cc1eaa37cdc7d", + "reference": "d7cf0d894e8aa4c73712ee4a331cc1eaa37cdc7d", "shasum": "" }, "require": { @@ -2004,29 +2030,35 @@ "phpcs", "standards" ], - "time": "2016-05-30 22:24:32" + "time": "2017-03-01T22:17:45+00:00" }, { "name": "symfony/yaml", - "version": "v3.1.2", + "version": "v3.2.7", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "2884c26ce4c1d61aebf423a8b912950fe7c764de" + "reference": "62b4cdb99d52cb1ff253c465eb1532a80cebb621" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/2884c26ce4c1d61aebf423a8b912950fe7c764de", - "reference": "2884c26ce4c1d61aebf423a8b912950fe7c764de", + "url": "https://api.github.com/repos/symfony/yaml/zipball/62b4cdb99d52cb1ff253c465eb1532a80cebb621", + "reference": "62b4cdb99d52cb1ff253c465eb1532a80cebb621", "shasum": "" }, "require": { "php": ">=5.5.9" }, + "require-dev": { + "symfony/console": "~2.8|~3.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, "type": "library", "extra": { "branch-alias": { - "dev-master": "3.1-dev" + "dev-master": "3.2-dev" } }, "autoload": { @@ -2053,32 +2085,33 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2016-06-29 05:41:56" + "time": "2017-03-20T09:45:15+00:00" }, { "name": "webmozart/assert", - "version": "1.0.2", + "version": "1.2.0", "source": { "type": "git", "url": "https://github.com/webmozart/assert.git", - "reference": "30eed06dd6bc88410a4ff7f77b6d22f3ce13dbde" + "reference": "2db61e59ff05fe5126d152bd0655c9ea113e550f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozart/assert/zipball/30eed06dd6bc88410a4ff7f77b6d22f3ce13dbde", - "reference": "30eed06dd6bc88410a4ff7f77b6d22f3ce13dbde", + "url": "https://api.github.com/repos/webmozart/assert/zipball/2db61e59ff05fe5126d152bd0655c9ea113e550f", + "reference": "2db61e59ff05fe5126d152bd0655c9ea113e550f", "shasum": "" }, "require": { - "php": ">=5.3.3" + "php": "^5.3.3 || ^7.0" }, "require-dev": { - "phpunit/phpunit": "^4.6" + "phpunit/phpunit": "^4.6", + "sebastian/version": "^1.0.1" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0-dev" + "dev-master": "1.3-dev" } }, "autoload": { @@ -2102,7 +2135,7 @@ "check", "validate" ], - "time": "2015-08-24 13:29:44" + "time": "2016-11-23T20:04:58+00:00" } ], "aliases": [], diff --git a/readme.md b/readme.md index 6c80705..3ed2935 100644 --- a/readme.md +++ b/readme.md @@ -4,39 +4,38 @@ This is a PHP library created to simplify development using the [TruckersMP](http://truckersmp.com/) API. This client library provides functions to connect and gather data from each API endpoint, and returns the data as an array. +If you want to use API without library (or if you have advanced knowledge of PHP), you can use it [directly](https://stats.truckersmp.com/api). + +## Warning! + +Please use our service responsibly. API Consumers who require high rates of requests against our APIs should contact TruckersMP Staff with a rationale and contact email for high-rate usage. + + ## Requirements -- PHP 5.6.0 or newer. +- PHP 5.6.0 or newer - Composer ## Installation This library can be installed using [Composer](http://getcomposer.org/). -Simple method: You can incluse the library in your composer.json requirements list by using the command `composer require truckersmp/api-client`. +Two ways: +1. Execute command `composer require truckersmp/api-client` +2. Add `truckersmp/api-client` manually to the composer requirements list. -Slightly less simple method: You can also add `truckersmp/api-client` manually to the composer requirements list. - -After doing either of the above, open a commandline or terminal in that directory and use the command `composer install`. +After doing either of the above, execute the command `composer install`. ## Usage -``` +Please refer to our full [API documentation](https://stats.truckersmp.com/api). + +```php client = new APIClient($adapter); +$client = new APIClient(); // Get player data for player id 50 $player = $client->player(50); @@ -52,8 +51,17 @@ echo $player->groupName; ``` +## Configuration -Please refer to our full [API documentation](https://stats.truckersmp.com/api). +We use [Guzzle](https://github.com/guzzle/guzzle) for gathering data from API endpoint. If you want to change Guzzle [configuration](http://guzzlephp.org/), you can pass config array on Client initialization. + +```php + +$client = new ApiClient([ + // your settings +]); + +``` ## Tests diff --git a/src/API/APIClient.php b/src/API/APIClient.php index 1f364a5..d96d2a7 100644 --- a/src/API/APIClient.php +++ b/src/API/APIClient.php @@ -7,118 +7,103 @@ namespace TruckersMP\API; -use Http\Client\HttpClient; -use Http\Message\MessageFactory\GuzzleMessageFactory; use TruckersMP\Types\Bans; +use TruckersMP\Types\GameTime; use TruckersMP\Types\Player; use TruckersMP\Types\Servers; use TruckersMP\Types\Version; -use TruckersMP\Types\GameTime; class APIClient { /** - * Base URL for communicating with TruckersMP API - * @var string - */ - private $apiEndpoint; - - /** - * API Version to interface with - * @var string - */ - private $version; - - /** - * HttpClient adapter - * @var HttpClient + * @var \TruckersMP\API\Request */ - private $adapter; - - /** - * Prefixed URL for requests - * @var string - */ - private $url; + private $request; /** * APIClient constructor. - * @param HttpClient $adapter + * + * @param array $config * @param string $apiEndpoint * @param string $version - * @param bool $secure + * @param bool $secure */ public function __construct( - HttpClient $adapter, + $config = [], $apiEndpoint = 'api.truckersmp.com', $version = 'v2', $secure = true - ) { - $this->apiEndpoint = $apiEndpoint; - $this->version = $version; - + ) + { $scheme = $secure ? 'https' : 'http'; + $url = $scheme . '://' . $apiEndpoint . '/' . $version . '/'; - $this->url = $scheme . '://' . $this->apiEndpoint . '/' . $this->version . '/'; - - $this->adapter = $adapter; - + $this->request = new Request($url, $config); } /** * Fetch player information + * * @param integer $id - * @return Player + * + * @return \TruckersMP\Types\Player + * @throws \Exception + * @throws \Http\Client\Exception */ public function player($id) { - $message = new GuzzleMessageFactory(); - - $request = $message->createRequest('GET', $this->url . 'player/' . $id); - - $result = $this->adapter->sendRequest($request); + $result = $this->request->execute('player/' . $id); return new Player($result); } + /** + * @param $id + * + * @return \TruckersMP\Types\Bans + * @throws \Exception + * @throws \Http\Client\Exception + */ public function bans($id) { - $message = new GuzzleMessageFactory(); + $result = $this->request->execute('bans/' . $id); - $request = $message->createRequest('GET', $this->url . 'bans/' . $id); - - $result = $this->adapter->sendRequest($request); return new Bans($result); } + /** + * @return \TruckersMP\Types\Servers + * @throws \Exception + * @throws \Http\Client\Exception + */ public function servers() { - $message = new GuzzleMessageFactory(); - - $request = $message->createRequest('GET', $this->url . 'servers'); + $result = $this->request->execute('servers'); - $result = $this->adapter->sendRequest($request); return new Servers($result); } + /** + * @return \TruckersMP\Types\GameTime + * @throws \Exception + * @throws \Http\Client\Exception + */ public function gameTime() { - $message = new GuzzleMessageFactory(); - - $request = $message->createRequest('GET', $this->url . 'game_time'); - - $result = $this->adapter->sendRequest($request); + $result = $this->request->execute('game_time'); return new GameTime($result); } + /** + * @return \TruckersMP\Types\Version + * @throws \Exception + * @throws \Http\Client\Exception + */ public function version() { - $message = new GuzzleMessageFactory(); - - $request = $message->createRequest('GET', $this->url . 'version'); + $result = $this->request->execute('version'); - $result = $this->adapter->sendRequest($request); return new Version($result); } } diff --git a/src/API/Request.php b/src/API/Request.php new file mode 100644 index 0000000..c0481b2 --- /dev/null +++ b/src/API/Request.php @@ -0,0 +1,57 @@ +message = new GuzzleMessageFactory(); + + $this->apiEndpoint = $apiEndpoint; + $this->adapter = new GuzzleAdapter(new GuzzleClient($config)); + } + + /** + * @param $uri + * + * @return \Psr\Http\Message\ResponseInterface + */ + public function execute($uri) + { + $request = $this->message->createRequest('GET', $this->apiEndpoint . $uri); + + return $this->adapter->sendRequest($request); + } +} \ No newline at end of file diff --git a/src/Exceptions/PlayerNotFoundException.php b/src/Exceptions/PlayerNotFoundException.php index 6724acb..129cb2c 100644 --- a/src/Exceptions/PlayerNotFoundException.php +++ b/src/Exceptions/PlayerNotFoundException.php @@ -10,5 +10,5 @@ class PlayerNotFoundException extends \Exception { - + } diff --git a/src/Types/Ban.php b/src/Types/Ban.php index fbdc4fa..9cd341d 100644 --- a/src/Types/Ban.php +++ b/src/Types/Ban.php @@ -4,54 +4,65 @@ namespace TruckersMP\Types; use Carbon\Carbon; -use TruckersMP\API\APIClient; class Ban { /** * Time and Date when the ban expires. + * * @var Carbon */ public $expires; + /** * Time and Date when the ban was created. + * * @var Carbon */ public $created; + /** * True if ban is currently active + * * @var boolean */ public $active; + /** * Reason for the ban + * * @var string */ public $reason; + /** * Admin's name + * * @var string */ public $adminName; + /** * Admin's ID + * * @var integer */ public $adminID; /** * Ban constructor. + * * @param array $ban */ public function __construct($ban) { $this->expires = new Carbon($ban['expiration'], 'UTC'); $this->created = new Carbon($ban['timeAdded'], 'UTC'); - $this->active = $ban['active']; + $this->active = $ban['active']; - $this->reason = $ban['reason']; + $this->reason = $ban['reason']; $this->adminName = $ban['adminName']; - $this->adminID = $ban['adminID']; + $this->adminID = $ban['adminID']; } } diff --git a/src/Types/Bans.php b/src/Types/Bans.php index 7eb2756..8c9080b 100644 --- a/src/Types/Bans.php +++ b/src/Types/Bans.php @@ -9,21 +9,24 @@ class Bans implements \Iterator, \ArrayAccess { - /** - * Iterator position - * @var int - */ - private $position = 0; - /** * Array of bans + * * @var array */ public $bans; + /** + * Iterator position + * + * @var int + */ + private $position = 0; /** * Bans constructor. + * * @param ResponseInterface $response + * * @throws PlayerNotFoundException */ public function __construct(ResponseInterface $response) @@ -34,7 +37,8 @@ public function __construct(ResponseInterface $response) if ($json['error'] && ($json['descriptor'] == 'No player ID submitted' || - $json['descriptor'] == 'Invalid user ID')) { + $json['descriptor'] == 'Invalid user ID') + ) { throw new PlayerNotFoundException($json['descriptor']); } @@ -48,11 +52,17 @@ public function rewind() $this->position = 0; } + /** + * @return mixed + */ public function current() { return $this->bans[$this->position]; } + /** + * @return int + */ public function key() { return $this->position; @@ -63,28 +73,52 @@ public function next() ++$this->position; } + /** + * @return bool + */ public function valid() { return isset($this->bans[$this->position]); } + /** + * @param mixed $offset + * @param mixed $value + * + * @return \Exception + */ public function offsetSet($offset, $value) { // TODO: custom class that gives a better description of the error return new \Exception('Can not change bans'); } + /** + * @param mixed $offset + * + * @return bool + */ public function offsetExists($offset) { return isset($this->bans[$offset]); } + /** + * @param mixed $offset + * + * @return \Exception + */ public function offsetUnset($offset) { // TODO: custom class that gives a better description of the error return new \Exception('Can not change bans'); } + /** + * @param mixed $offset + * + * @return mixed|null + */ public function offsetGet($offset) { return isset($this->bans[$offset]) ? $this->bans[$offset] : null; diff --git a/src/Types/GameTime.php b/src/Types/GameTime.php index 636ef6c..5a7737f 100644 --- a/src/Types/GameTime.php +++ b/src/Types/GameTime.php @@ -16,6 +16,13 @@ class GameTime public $time; + /** + * GameTime constructor. + * + * @param \Psr\Http\Message\ResponseInterface $response + * + * @throws \Exception + */ public function __construct(ResponseInterface $response) { $json = json_decode((string)$response->getBody(), true, 512, JSON_BIGINT_AS_STRING); @@ -26,16 +33,16 @@ public function __construct(ResponseInterface $response) } $load['minutes'] = $json['game_time']; - $load['hours'] = $load['minutes'] / 60; + $load['hours'] = $load['minutes'] / 60; $load['minutes'] = $load['minutes'] % 60; - $load['days'] = $load['hours'] / 24; + $load['days'] = $load['hours'] / 24; $load['hours'] = $load['hours'] % 24; $load['months'] = $load['days'] / 30; - $load['days'] = $load['days'] % 30; + $load['days'] = $load['days'] % 30; - $load['years'] = intval($load['months'] / 12); + $load['years'] = intval($load['months'] / 12); $load['months'] = $load['months'] % 12; $this->time = Carbon::create($load['years'], $load['months'], $load['days'], $load['hours'], $load['minutes']); diff --git a/src/Types/Player.php b/src/Types/Player.php index 0f82924..81e35b6 100644 --- a/src/Types/Player.php +++ b/src/Types/Player.php @@ -11,72 +11,82 @@ class Player /** * User ID + * * @var integer */ public $id; /** * Username + * * @var string */ public $name; /** * Avatar URL + * * @var string */ public $avatar; - + /** * Date and time user joined + * * @var \DateTime */ public $joinDate; /** * User's associated SteamID + * * @var string */ public $steamID64; /** * Group ID of user + * * @var integer */ public $groupID; /** * Human readable group name + * * @var string */ public $groupName; /** * If user is an in-game admin + * * @var boolean */ public $inGameAdmin; /** * Player constructor. + * * @param ResponseInterface $response + * * @throws PlayerNotFoundException */ public function __construct(ResponseInterface $response) { $json = json_decode((string)$response->getBody(), true, 512, JSON_BIGINT_AS_STRING); - + if ($json['error']) { throw new PlayerNotFoundException($json['response']); } - $this->id = $json['response']['id']; - $this->name = $json['response']['name']; - $this->avatar = $json['response']['avatar']; - $this->joinDate = $json['response']['joinDate']; - $this->steamID64 = $json['response']['steamID64']; - $this->groupID = $json['response']['groupID']; - $this->groupName = $json['response']['groupName']; - $this->inGameAdmin = $json['response']['permissions']['isGameAdmin']; + $this->id = $json['response']['id']; + $this->name = $json['response']['name']; + $this->avatar = $json['response']['avatar']; + $this->joinDate = $json['response']['joinDate']; + $this->steamID64 = $json['response']['steamID64']; + $this->groupID = $json['response']['groupID']; + $this->groupName = $json['response']['groupName']; + $this->inGameAdmin = $json['response']['permissions']['isGameAdmin']; } } diff --git a/src/Types/Server.php b/src/Types/Server.php index 4f6db5e..ff35fd3 100644 --- a/src/Types/Server.php +++ b/src/Types/Server.php @@ -7,102 +7,119 @@ class Server /** * Game server ID + * * @var int */ public $id; /** * Game + * * @var string */ public $game; /** * IP or Hostname of server + * * @var string */ public $ip; /** * Port + * * @var int */ public $port; /** * Game server name + * * @var string */ public $name; /** * Game server short name + * * @var string */ public $shortName; /** * Online status + * * @var bool */ public $online; /** * Current player count + * * @var int */ public $players; /** * Current queue count + * * @var int */ public $queue; /** * Max player count + * * @var int */ public $maxPlayers; /** * Speed limiter + * * @var bool */ public $speedLimiter; /** * Collisions + * * @var bool */ public $collisions; /** * Will cars be available for all players + * * @var bool */ public $carsForPlayers; /** * Will police cars be available for all players + * * @var bool */ public $policeCarsForPlayers; /** * "Away from keyboard" status + * * @var bool */ public $afkEnabled; /** * Sync delay (tick rate) + * * @var bool */ public $syncDelay; /** * Server constructor. + * * @param $server */ public function __construct($server) diff --git a/src/Types/Servers.php b/src/Types/Servers.php index 6f22656..9e6300e 100644 --- a/src/Types/Servers.php +++ b/src/Types/Servers.php @@ -8,21 +8,25 @@ class Servers implements \Iterator, \ArrayAccess { - /** - * Iterator position - * @var int - */ - private $position = 0; - /** * Array of servers + * * @var array */ public $servers; + /** + * Iterator position + * + * @var int + */ + private $position = 0; + /** * Servers constructor. + * * @param ResponseInterface $response + * * @throws APIErrorException */ public function __construct(ResponseInterface $response) diff --git a/src/Types/Version.php b/src/Types/Version.php index 4a708d4..8d4c569 100644 --- a/src/Types/Version.php +++ b/src/Types/Version.php @@ -21,29 +21,33 @@ class Version public $support; + /** + * Version constructor. + * + * @param \Psr\Http\Message\ResponseInterface $response + */ public function __construct(ResponseInterface $response) { $json = json_decode((string)$response->getBody(), true, 512, JSON_BIGINT_AS_STRING); - - $this->version = new \stdClass(); - $this->version->human = $json['name']; - $this->version->stage = $json['stage']; + $this->version = new \stdClass(); + $this->version->human = $json['name']; + $this->version->stage = $json['stage']; $this->version->nummeric = $json['numeric']; - $this->checksum = new \stdClass(); - $this->checksum->atsmp = new \stdClass(); - $this->checksum->atsmp->dll = $json['atsmp_checksum']['dll']; - $this->checksum->atsmp->adb = $json['atsmp_checksum']['adb']; - $this->checksum->ets2mp = new \stdClass(); + $this->checksum = new \stdClass(); + $this->checksum->atsmp = new \stdClass(); + $this->checksum->atsmp->dll = $json['atsmp_checksum']['dll']; + $this->checksum->atsmp->adb = $json['atsmp_checksum']['adb']; + $this->checksum->ets2mp = new \stdClass(); $this->checksum->ets2mp->dll = $json['atsmp_checksum']['dll']; $this->checksum->ets2mp->adb = $json['atsmp_checksum']['adb']; $this->released = new Carbon($json['time'], 'UTC'); - $this->support = new \stdClass(); + $this->support = new \stdClass(); $this->support->ets2 = $json['supported_game_version']; - $this->support->ats = $json['supported_ats_game_version']; + $this->support->ats = $json['supported_ats_game_version']; } } From 50e816486947fe2e1284a7ad4fe5e69bdc4c24e5 Mon Sep 17 00:00:00 2001 From: CJMAXiK Date: Tue, 11 Apr 2017 10:49:47 +1000 Subject: [PATCH 02/11] Delete `examples` folder --- Tests/ClientTest.php | 26 +++++++++++++++++++------- examples/example_bans.php | 18 ------------------ examples/example_game_time.php | 13 ------------- examples/example_servers.php | 21 --------------------- examples/example_user.php | 13 ------------- examples/example_version.php | 12 ------------ 6 files changed, 19 insertions(+), 84 deletions(-) delete mode 100644 examples/example_bans.php delete mode 100644 examples/example_game_time.php delete mode 100644 examples/example_servers.php delete mode 100644 examples/example_user.php delete mode 100644 examples/example_version.php diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index 81c5c3c..7fe9099 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -4,16 +4,9 @@ use Carbon\Carbon; use TruckersMP\API\APIClient; - -use GuzzleHttp\Client as GuzzleClient; -use Http\Adapter\Guzzle6\Client as GuzzleAdapter; - -use TruckersMP\Types\Version; use TruckersMP\Types\Ban; use TruckersMP\Types\Bans; use TruckersMP\Types\Player; -use TruckersMP\Types\Servers; -use TruckersMP\Types\Server; class ClientTest extends \PHPUnit_Framework_TestCase { @@ -22,6 +15,9 @@ class ClientTest extends \PHPUnit_Framework_TestCase private $client; + /** + * ClientTest constructor. + */ public function __construct() { parent::__construct(); @@ -29,6 +25,10 @@ public function __construct() $this->client = new APIClient(); } + /** + * @throws \Exception + * @throws \Http\Client\Exception + */ public function testPlayer() { $player = $this->client->player($this->testAccount); // Special test account that *should* remain static @@ -41,6 +41,10 @@ public function testPlayer() $this->assertInstanceOf(Player::class, $player); } + /** + * @throws \Exception + * @throws \Http\Client\Exception + */ public function testPlayerBans() { $bans = $this->client->bans($this->testAccount); @@ -54,12 +58,20 @@ public function testPlayerBans() } + /** + * @throws \Exception + * @throws \Http\Client\Exception + */ public function testServers() { $servers = $this->client->servers(); $this->assertEquals($servers[0]->name, 'Europe 1'); } + /** + * @throws \Exception + * @throws \Http\Client\Exception + */ public function testVersion() { $version = $this->client->version(); diff --git a/examples/example_bans.php b/examples/example_bans.php deleted file mode 100644 index 540fbf3..0000000 --- a/examples/example_bans.php +++ /dev/null @@ -1,18 +0,0 @@ -bans(50); - - -echo "

Ban reasons:

"; -foreach ($bans as $ban) -{ - printf('

Banned by: %s
', $ban->adminName); - printf('Reason: %s
', $ban->reason); - printf('At: %s
', $ban->created); - printf('Until: %s

', $ban->expires); -} - diff --git a/examples/example_game_time.php b/examples/example_game_time.php deleted file mode 100644 index 9491fae..0000000 --- a/examples/example_game_time.php +++ /dev/null @@ -1,13 +0,0 @@ -game_time(); //Use the class variable to access one of the class functions. - -echo "

Year-Month-Day Hours:Minutes

"; -echo "

Current Game time: " . $time['years'] . "-" . $time['months'] . "-" . $time['days'] . " " . $time['hours'] . ":" . $time['minutes'] . "

"; - -echo "
"; -var_dump($time); //Dumping all of the information so you can see what is available. diff --git a/examples/example_servers.php b/examples/example_servers.php deleted file mode 100644 index 26115ee..0000000 --- a/examples/example_servers.php +++ /dev/null @@ -1,21 +0,0 @@ -servers(); //Use the class variable to access one of the class functions. - -echo "

Servers:

"; -foreach ($servers['response'] as $v) -{ //Foreach to loop through all servers supplied by the API and print their statuses. - if ($v['online'] == TRUE) { - $online = "Online"; - } else { - $online = "Offline"; - } - echo "

" . $v['game'] . " " . $v['name'] . ":" . $online . "(" . $v['players'] . "/" . $v['maxplayers'] . ")" ."

"; -} - -echo "
"; -var_dump($servers); //Dumping all of the information so you can see what is available. diff --git a/examples/example_user.php b/examples/example_user.php deleted file mode 100644 index 4fede46..0000000 --- a/examples/example_user.php +++ /dev/null @@ -1,13 +0,0 @@ -player(1); //Use the class variable to access one of the class functions, loading the player with ID 1. Also works with SteamID64. - -echo "Player 1 is " . $thisplayer['response']['name'] . '
'; //Write the information about Player 1 to the document. -echo "(S)He is a " . $thisplayer['response']['groupName'] . '
'; - -echo "
"; -var_dump($thisplayer); //Dumping all of the information so you can see what is available. diff --git a/examples/example_version.php b/examples/example_version.php deleted file mode 100644 index db4d9e6..0000000 --- a/examples/example_version.php +++ /dev/null @@ -1,12 +0,0 @@ -version(); //Use the class variable to access one of the class functions. - -echo "

TruckersMP Version: " . $version['name'] . "

"; - -echo "
"; -var_dump($version); //Dumping all of the information so you can see what is available. From c59643beb62ba417c7bac8b008925b5ef80d2447 Mon Sep 17 00:00:00 2001 From: Max Mekenya Date: Tue, 11 Apr 2017 00:50:15 +0000 Subject: [PATCH 03/11] Apply fixes from StyleCI --- Tests/ClientTest.php | 2 - src/API/APIClient.php | 28 +++++----- src/API/Request.php | 13 +++-- src/Exceptions/APIErrorException.php | 3 +- src/Exceptions/PlayerNotFoundException.php | 3 +- src/Types/Ban.php | 20 ++++--- src/Types/Bans.php | 8 ++- src/Types/GameTime.php | 14 +++-- src/Types/Player.php | 40 +++++++------- src/Types/Server.php | 63 +++++++++++----------- src/Types/Servers.php | 9 ++-- src/Types/Version.php | 25 +++++---- 12 files changed, 108 insertions(+), 120 deletions(-) diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index 7fe9099..4255478 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -10,7 +10,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase { - private $testAccount = 585204; private $client; @@ -55,7 +54,6 @@ public function testPlayerBans() $this->assertInstanceOf(Bans::class, $bans); $this->assertInstanceOf(Ban::class, $bans[0]); - } /** diff --git a/src/API/APIClient.php b/src/API/APIClient.php index d96d2a7..278b95f 100644 --- a/src/API/APIClient.php +++ b/src/API/APIClient.php @@ -33,26 +33,26 @@ public function __construct( $apiEndpoint = 'api.truckersmp.com', $version = 'v2', $secure = true - ) - { + ) { $scheme = $secure ? 'https' : 'http'; - $url = $scheme . '://' . $apiEndpoint . '/' . $version . '/'; + $url = $scheme.'://'.$apiEndpoint.'/'.$version.'/'; $this->request = new Request($url, $config); } /** - * Fetch player information + * Fetch player information. * - * @param integer $id + * @param int $id * - * @return \TruckersMP\Types\Player * @throws \Exception * @throws \Http\Client\Exception + * + * @return \TruckersMP\Types\Player */ public function player($id) { - $result = $this->request->execute('player/' . $id); + $result = $this->request->execute('player/'.$id); return new Player($result); } @@ -60,21 +60,23 @@ public function player($id) /** * @param $id * - * @return \TruckersMP\Types\Bans * @throws \Exception * @throws \Http\Client\Exception + * + * @return \TruckersMP\Types\Bans */ public function bans($id) { - $result = $this->request->execute('bans/' . $id); + $result = $this->request->execute('bans/'.$id); return new Bans($result); } /** - * @return \TruckersMP\Types\Servers * @throws \Exception * @throws \Http\Client\Exception + * + * @return \TruckersMP\Types\Servers */ public function servers() { @@ -84,9 +86,10 @@ public function servers() } /** - * @return \TruckersMP\Types\GameTime * @throws \Exception * @throws \Http\Client\Exception + * + * @return \TruckersMP\Types\GameTime */ public function gameTime() { @@ -96,9 +99,10 @@ public function gameTime() } /** - * @return \TruckersMP\Types\Version * @throws \Exception * @throws \Http\Client\Exception + * + * @return \TruckersMP\Types\Version */ public function version() { diff --git a/src/API/Request.php b/src/API/Request.php index c0481b2..5264bb5 100644 --- a/src/API/Request.php +++ b/src/API/Request.php @@ -2,7 +2,7 @@ /** * Created by PhpStorm. * Date: 11.04.2017 - * Time: 09:52 + * Time: 09:52. */ namespace TruckersMP\API; @@ -31,16 +31,15 @@ class Request /** * Request constructor. * - * @param $apiEndpoint - * - * @param array $config + * @param $apiEndpoint + * @param array $config */ public function __construct($apiEndpoint, $config = []) { $this->message = new GuzzleMessageFactory(); $this->apiEndpoint = $apiEndpoint; - $this->adapter = new GuzzleAdapter(new GuzzleClient($config)); + $this->adapter = new GuzzleAdapter(new GuzzleClient($config)); } /** @@ -50,8 +49,8 @@ public function __construct($apiEndpoint, $config = []) */ public function execute($uri) { - $request = $this->message->createRequest('GET', $this->apiEndpoint . $uri); + $request = $this->message->createRequest('GET', $this->apiEndpoint.$uri); return $this->adapter->sendRequest($request); } -} \ No newline at end of file +} diff --git a/src/Exceptions/APIErrorException.php b/src/Exceptions/APIErrorException.php index acd204f..28749af 100644 --- a/src/Exceptions/APIErrorException.php +++ b/src/Exceptions/APIErrorException.php @@ -3,12 +3,11 @@ * Created by PhpStorm. * User: thor * Date: 24.06.16 - * Time: 14:06 + * Time: 14:06. */ namespace TruckersMP\Exceptions; class APIErrorException extends \Exception { - } diff --git a/src/Exceptions/PlayerNotFoundException.php b/src/Exceptions/PlayerNotFoundException.php index 129cb2c..b545c22 100644 --- a/src/Exceptions/PlayerNotFoundException.php +++ b/src/Exceptions/PlayerNotFoundException.php @@ -3,12 +3,11 @@ * Created by PhpStorm. * User: thor * Date: 24.06.16 - * Time: 11:19 + * Time: 11:19. */ namespace TruckersMP\Exceptions; class PlayerNotFoundException extends \Exception { - } diff --git a/src/Types/Ban.php b/src/Types/Ban.php index 9cd341d..3bb639c 100644 --- a/src/Types/Ban.php +++ b/src/Types/Ban.php @@ -1,6 +1,5 @@ expires = new Carbon($ban['expiration'], 'UTC'); $this->created = new Carbon($ban['timeAdded'], 'UTC'); - $this->active = $ban['active']; + $this->active = $ban['active']; - $this->reason = $ban['reason']; + $this->reason = $ban['reason']; $this->adminName = $ban['adminName']; - $this->adminID = $ban['adminID']; - + $this->adminID = $ban['adminID']; } } diff --git a/src/Types/Bans.php b/src/Types/Bans.php index 8c9080b..e548855 100644 --- a/src/Types/Bans.php +++ b/src/Types/Bans.php @@ -1,6 +1,5 @@ position = 0; - $json = json_decode((string)$response->getBody(), true, 512, JSON_BIGINT_AS_STRING); + $json = json_decode((string) $response->getBody(), true, 512, JSON_BIGINT_AS_STRING); if ($json['error'] && ($json['descriptor'] == 'No player ID submitted' || diff --git a/src/Types/GameTime.php b/src/Types/GameTime.php index 5a7737f..2374986 100644 --- a/src/Types/GameTime.php +++ b/src/Types/GameTime.php @@ -3,7 +3,7 @@ * Created by PhpStorm. * User: thor * Date: 12.07.16 - * Time: 09:55 + * Time: 09:55. */ namespace TruckersMP\Types; @@ -13,7 +13,6 @@ class GameTime { - public $time; /** @@ -25,7 +24,7 @@ class GameTime */ public function __construct(ResponseInterface $response) { - $json = json_decode((string)$response->getBody(), true, 512, JSON_BIGINT_AS_STRING); + $json = json_decode((string) $response->getBody(), true, 512, JSON_BIGINT_AS_STRING); if ($json['error']) { // TODO: actually throw a usable error @@ -33,19 +32,18 @@ public function __construct(ResponseInterface $response) } $load['minutes'] = $json['game_time']; - $load['hours'] = $load['minutes'] / 60; + $load['hours'] = $load['minutes'] / 60; $load['minutes'] = $load['minutes'] % 60; - $load['days'] = $load['hours'] / 24; + $load['days'] = $load['hours'] / 24; $load['hours'] = $load['hours'] % 24; $load['months'] = $load['days'] / 30; - $load['days'] = $load['days'] % 30; + $load['days'] = $load['days'] % 30; - $load['years'] = intval($load['months'] / 12); + $load['years'] = intval($load['months'] / 12); $load['months'] = $load['months'] % 12; $this->time = Carbon::create($load['years'], $load['months'], $load['days'], $load['hours'], $load['minutes']); - } } diff --git a/src/Types/Player.php b/src/Types/Player.php index 81e35b6..c1cd9b5 100644 --- a/src/Types/Player.php +++ b/src/Types/Player.php @@ -1,6 +1,5 @@ getBody(), true, 512, JSON_BIGINT_AS_STRING); + $json = json_decode((string) $response->getBody(), true, 512, JSON_BIGINT_AS_STRING); if ($json['error']) { throw new PlayerNotFoundException($json['response']); } - $this->id = $json['response']['id']; - $this->name = $json['response']['name']; - $this->avatar = $json['response']['avatar']; - $this->joinDate = $json['response']['joinDate']; - $this->steamID64 = $json['response']['steamID64']; - $this->groupID = $json['response']['groupID']; - $this->groupName = $json['response']['groupName']; + $this->id = $json['response']['id']; + $this->name = $json['response']['name']; + $this->avatar = $json['response']['avatar']; + $this->joinDate = $json['response']['joinDate']; + $this->steamID64 = $json['response']['steamID64']; + $this->groupID = $json['response']['groupID']; + $this->groupName = $json['response']['groupName']; $this->inGameAdmin = $json['response']['permissions']['isGameAdmin']; } } diff --git a/src/Types/Server.php b/src/Types/Server.php index ff35fd3..12be736 100644 --- a/src/Types/Server.php +++ b/src/Types/Server.php @@ -4,114 +4,113 @@ class Server { - /** - * Game server ID + * Game server ID. * * @var int */ public $id; /** - * Game + * Game. * * @var string */ public $game; /** - * IP or Hostname of server + * IP or Hostname of server. * * @var string */ public $ip; /** - * Port + * Port. * * @var int */ public $port; /** - * Game server name + * Game server name. * * @var string */ public $name; /** - * Game server short name + * Game server short name. * * @var string */ public $shortName; /** - * Online status + * Online status. * * @var bool */ public $online; /** - * Current player count + * Current player count. * * @var int */ public $players; /** - * Current queue count + * Current queue count. * * @var int */ public $queue; /** - * Max player count + * Max player count. * * @var int */ public $maxPlayers; /** - * Speed limiter + * Speed limiter. * * @var bool */ public $speedLimiter; /** - * Collisions + * Collisions. * * @var bool */ public $collisions; /** - * Will cars be available for all players + * Will cars be available for all players. * * @var bool */ public $carsForPlayers; /** - * Will police cars be available for all players + * Will police cars be available for all players. * * @var bool */ public $policeCarsForPlayers; /** - * "Away from keyboard" status + * "Away from keyboard" status. * * @var bool */ public $afkEnabled; /** - * Sync delay (tick rate) + * Sync delay (tick rate). * * @var bool */ @@ -124,21 +123,21 @@ class Server */ public function __construct($server) { - $this->id = intval($server['id']); - $this->game = $server['game']; - $this->ip = $server['ip']; - $this->port = intval($server['port']); - $this->name = $server['name']; - $this->shortName = $server['shortname']; - $this->online = boolval($server['online']); - $this->players = intval($server['players']); - $this->queue = intval($server['queue']); - $this->maxPlayers = intval($server['maxplayers']); - $this->speedLimiter = boolval($server['speedlimiter']); - $this->collisions = boolval($server['collisions']); - $this->carsForPlayers = boolval($server['carsforplayers']); + $this->id = intval($server['id']); + $this->game = $server['game']; + $this->ip = $server['ip']; + $this->port = intval($server['port']); + $this->name = $server['name']; + $this->shortName = $server['shortname']; + $this->online = boolval($server['online']); + $this->players = intval($server['players']); + $this->queue = intval($server['queue']); + $this->maxPlayers = intval($server['maxplayers']); + $this->speedLimiter = boolval($server['speedlimiter']); + $this->collisions = boolval($server['collisions']); + $this->carsForPlayers = boolval($server['carsforplayers']); $this->policeCarsForPlayers = boolval($server['policecarsforplayers']); - $this->afkEnabled = boolval($server['afkenabled']); - $this->syncDelay = intval($server['syncdelay']); + $this->afkEnabled = boolval($server['afkenabled']); + $this->syncDelay = intval($server['syncdelay']); } } diff --git a/src/Types/Servers.php b/src/Types/Servers.php index 9e6300e..743db6b 100644 --- a/src/Types/Servers.php +++ b/src/Types/Servers.php @@ -7,16 +7,15 @@ class Servers implements \Iterator, \ArrayAccess { - /** - * Array of servers + * Array of servers. * * @var array */ public $servers; /** - * Iterator position + * Iterator position. * * @var int */ @@ -33,9 +32,9 @@ public function __construct(ResponseInterface $response) { $this->position = 0; - $json = json_decode((string)$response->getBody(), true, 512, JSON_BIGINT_AS_STRING); + $json = json_decode((string) $response->getBody(), true, 512, JSON_BIGINT_AS_STRING); - if ($json['error'] == "true" && $json['descriptor'] == 'Unable to fetch servers') { + if ($json['error'] == 'true' && $json['descriptor'] == 'Unable to fetch servers') { throw new APIErrorException($json['descriptor']); } diff --git a/src/Types/Version.php b/src/Types/Version.php index 8d4c569..41e2cc1 100644 --- a/src/Types/Version.php +++ b/src/Types/Version.php @@ -3,7 +3,7 @@ * Created by PhpStorm. * User: thor * Date: 27.06.16 - * Time: 09:46 + * Time: 09:46. */ namespace TruckersMP\Types; @@ -28,26 +28,25 @@ class Version */ public function __construct(ResponseInterface $response) { - $json = json_decode((string)$response->getBody(), true, 512, JSON_BIGINT_AS_STRING); + $json = json_decode((string) $response->getBody(), true, 512, JSON_BIGINT_AS_STRING); - $this->version = new \stdClass(); - $this->version->human = $json['name']; - $this->version->stage = $json['stage']; + $this->version = new \stdClass(); + $this->version->human = $json['name']; + $this->version->stage = $json['stage']; $this->version->nummeric = $json['numeric']; - $this->checksum = new \stdClass(); - $this->checksum->atsmp = new \stdClass(); - $this->checksum->atsmp->dll = $json['atsmp_checksum']['dll']; - $this->checksum->atsmp->adb = $json['atsmp_checksum']['adb']; - $this->checksum->ets2mp = new \stdClass(); + $this->checksum = new \stdClass(); + $this->checksum->atsmp = new \stdClass(); + $this->checksum->atsmp->dll = $json['atsmp_checksum']['dll']; + $this->checksum->atsmp->adb = $json['atsmp_checksum']['adb']; + $this->checksum->ets2mp = new \stdClass(); $this->checksum->ets2mp->dll = $json['atsmp_checksum']['dll']; $this->checksum->ets2mp->adb = $json['atsmp_checksum']['adb']; $this->released = new Carbon($json['time'], 'UTC'); - $this->support = new \stdClass(); + $this->support = new \stdClass(); $this->support->ets2 = $json['supported_game_version']; - $this->support->ats = $json['supported_ats_game_version']; - + $this->support->ats = $json['supported_ats_game_version']; } } From a0960d34cf77cedd6f879a40de14b2be7395fd89 Mon Sep 17 00:00:00 2001 From: CJMAXiK Date: Tue, 11 Apr 2017 14:01:27 +1000 Subject: [PATCH 04/11] File caching --- .gitignore | 3 +- Tests/ClientTest.php | 35 +- composer.json | 4 +- composer.lock | 738 +++++++++++++++++++++++++++-------------- readme.md | 15 +- src/API/APIClient.php | 71 +++- src/API/Request.php | 15 +- src/Types/Ban.php | 6 +- src/Types/Bans.php | 18 +- src/Types/GameTime.php | 19 +- src/Types/Player.php | 29 +- src/Types/Server.php | 34 +- src/Types/Servers.php | 12 +- src/Types/Version.php | 43 ++- 14 files changed, 674 insertions(+), 368 deletions(-) diff --git a/.gitignore b/.gitignore index e0f7956..34a600c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ ### Composer ### composer.phar /vendor/ -/.idea +/.idea/ +/Tests/cache/ diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index 4255478..5cf1e0a 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -16,24 +16,43 @@ class ClientTest extends \PHPUnit_Framework_TestCase /** * ClientTest constructor. + * */ public function __construct() { parent::__construct(); - $this->client = new APIClient(); + $cachePath = __DIR__ . '/cache/'; + $this->client = new APIClient($cachePath); } /** * @throws \Exception * @throws \Http\Client\Exception */ - public function testPlayer() + public function testColdPlayerCache() { - $player = $this->client->player($this->testAccount); // Special test account that *should* remain static + $this->getPlayer(); + } - $this->assertEquals($player->name, 'tuxytestaccount'); + /** + * @throws \Exception + * @throws \Http\Client\Exception + */ + public function testHotPlayerCache() + { + $this->getPlayer(); + } + + /** + * @throws \Exception + * @throws \Http\Client\Exception + */ + public function getPlayer() + { + $player = $this->client->player($this->testAccount); + $this->assertEquals($player->name, 'tuxytestaccount'); $this->assertEquals($player->groupID, 1); $this->assertEquals($player->groupName, 'Player'); @@ -63,6 +82,7 @@ public function testPlayerBans() public function testServers() { $servers = $this->client->servers(); + $this->assertEquals($servers[0]->name, 'Europe 1'); } @@ -95,4 +115,11 @@ public function testGameTime() $this->assertNotEmpty($time); } + + public function testCache() + { + $cache = $this->client->clearCache(); + + $this->assertTrue($cache); + } } diff --git a/composer.json b/composer.json index 3a3238f..24a335c 100644 --- a/composer.json +++ b/composer.json @@ -26,9 +26,9 @@ "require": { "php": ">=5.6.0", "nesbot/carbon": "^1.21", - "php-http/client-implementation": "^1.0", "php-http/message": "^1.2", - "guzzlehttp/psr7": "^1.3" + "guzzlehttp/psr7": "^1.3", + "phpFastCache/phpFastCache": "^5.0" }, "require-dev": { "phpunit/phpunit": "^5.3", diff --git a/composer.lock b/composer.lock index 3943a75..8f42498 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "2c1d4698425cf2ca8f19291e44fd8693", + "content-hash": "77da861a1797d508d71a3beaf73575be", "packages": [ { "name": "clue/stream-filter", @@ -55,119 +55,6 @@ ], "time": "2015-11-08T23:41:30+00:00" }, - { - "name": "guzzlehttp/guzzle", - "version": "6.2.3", - "source": { - "type": "git", - "url": "https://github.com/guzzle/guzzle.git", - "reference": "8d6c6cc55186db87b7dc5009827429ba4e9dc006" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/guzzle/guzzle/zipball/8d6c6cc55186db87b7dc5009827429ba4e9dc006", - "reference": "8d6c6cc55186db87b7dc5009827429ba4e9dc006", - "shasum": "" - }, - "require": { - "guzzlehttp/promises": "^1.0", - "guzzlehttp/psr7": "^1.4", - "php": ">=5.5" - }, - "require-dev": { - "ext-curl": "*", - "phpunit/phpunit": "^4.0", - "psr/log": "^1.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "6.2-dev" - } - }, - "autoload": { - "files": [ - "src/functions_include.php" - ], - "psr-4": { - "GuzzleHttp\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Michael Dowling", - "email": "mtdowling@gmail.com", - "homepage": "https://github.com/mtdowling" - } - ], - "description": "Guzzle is a PHP HTTP client library", - "homepage": "http://guzzlephp.org/", - "keywords": [ - "client", - "curl", - "framework", - "http", - "http client", - "rest", - "web service" - ], - "time": "2017-02-28T22:50:30+00:00" - }, - { - "name": "guzzlehttp/promises", - "version": "v1.3.1", - "source": { - "type": "git", - "url": "https://github.com/guzzle/promises.git", - "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", - "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", - "shasum": "" - }, - "require": { - "php": ">=5.5.0" - }, - "require-dev": { - "phpunit/phpunit": "^4.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.4-dev" - } - }, - "autoload": { - "psr-4": { - "GuzzleHttp\\Promise\\": "src/" - }, - "files": [ - "src/functions_include.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Michael Dowling", - "email": "mtdowling@gmail.com", - "homepage": "https://github.com/mtdowling" - } - ], - "description": "Guzzle promises library", - "keywords": [ - "promise" - ], - "time": "2016-12-20T10:07:11+00:00" - }, { "name": "guzzlehttp/psr7", "version": "1.4.2", @@ -286,122 +173,6 @@ ], "time": "2017-01-16T07:55:07+00:00" }, - { - "name": "php-http/guzzle6-adapter", - "version": "v1.1.1", - "source": { - "type": "git", - "url": "https://github.com/php-http/guzzle6-adapter.git", - "reference": "a56941f9dc6110409cfcddc91546ee97039277ab" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-http/guzzle6-adapter/zipball/a56941f9dc6110409cfcddc91546ee97039277ab", - "reference": "a56941f9dc6110409cfcddc91546ee97039277ab", - "shasum": "" - }, - "require": { - "guzzlehttp/guzzle": "^6.0", - "php": ">=5.5.0", - "php-http/httplug": "^1.0" - }, - "provide": { - "php-http/async-client-implementation": "1.0", - "php-http/client-implementation": "1.0" - }, - "require-dev": { - "ext-curl": "*", - "php-http/adapter-integration-tests": "^0.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.2-dev" - } - }, - "autoload": { - "psr-4": { - "Http\\Adapter\\Guzzle6\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Márk Sági-Kazár", - "email": "mark.sagikazar@gmail.com" - }, - { - "name": "David de Boer", - "email": "david@ddeboer.nl" - } - ], - "description": "Guzzle 6 HTTP Adapter", - "homepage": "http://httplug.io", - "keywords": [ - "Guzzle", - "http" - ], - "time": "2016-05-10T06:13:32+00:00" - }, - { - "name": "php-http/httplug", - "version": "v1.1.0", - "source": { - "type": "git", - "url": "https://github.com/php-http/httplug.git", - "reference": "1c6381726c18579c4ca2ef1ec1498fdae8bdf018" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-http/httplug/zipball/1c6381726c18579c4ca2ef1ec1498fdae8bdf018", - "reference": "1c6381726c18579c4ca2ef1ec1498fdae8bdf018", - "shasum": "" - }, - "require": { - "php": ">=5.4", - "php-http/promise": "^1.0", - "psr/http-message": "^1.0" - }, - "require-dev": { - "henrikbjorn/phpspec-code-coverage": "^1.0", - "phpspec/phpspec": "^2.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.1-dev" - } - }, - "autoload": { - "psr-4": { - "Http\\Client\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Eric GELOEN", - "email": "geloen.eric@gmail.com" - }, - { - "name": "Márk Sági-Kazár", - "email": "mark.sagikazar@gmail.com" - } - ], - "description": "HTTPlug, the HTTP client abstraction for PHP", - "homepage": "http://httplug.io", - "keywords": [ - "client", - "http" - ], - "time": "2016-08-31T08:30:17+00:00" - }, { "name": "php-http/message", "version": "1.5.0", @@ -522,32 +293,40 @@ "time": "2015-12-19T14:08:53+00:00" }, { - "name": "php-http/promise", - "version": "v1.0.0", + "name": "phpfastcache/phpfastcache", + "version": "5.0.15", "source": { "type": "git", - "url": "https://github.com/php-http/promise.git", - "reference": "dc494cdc9d7160b9a09bd5573272195242ce7980" + "url": "https://github.com/PHPSocialNetwork/phpfastcache.git", + "reference": "f9c09668b313dd53a6417be42e4af02649914a89" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-http/promise/zipball/dc494cdc9d7160b9a09bd5573272195242ce7980", - "reference": "dc494cdc9d7160b9a09bd5573272195242ce7980", + "url": "https://api.github.com/repos/PHPSocialNetwork/phpfastcache/zipball/f9c09668b313dd53a6417be42e4af02649914a89", + "reference": "f9c09668b313dd53a6417be42e4af02649914a89", "shasum": "" }, - "require-dev": { - "henrikbjorn/phpspec-code-coverage": "^1.0", - "phpspec/phpspec": "^2.4" + "require": { + "ext-json": "*", + "php": ">=5.5.0", + "phpfastcache/phpssdb": "~1.0.0", + "predis/predis": "~1.1.0", + "psr/cache": "~1.0.0" }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.1-dev" - } + "suggest": { + "ext-apc": "*", + "ext-intl": "*", + "ext-mbstring": "*", + "ext-memcache": "*", + "ext-memcached": "*", + "ext-predis": "*", + "ext-redis": "*", + "ext-sqlite": "*" }, + "type": "library", "autoload": { "psr-4": { - "Http\\Promise\\": "src/" + "phpFastCache\\": "src/phpFastCache/" } }, "notification-url": "https://packagist.org/downloads/", @@ -556,20 +335,194 @@ ], "authors": [ { - "name": "Márk Sági-Kazár", - "email": "mark.sagikazar@gmail.com" + "name": "Khoa Bui", + "email": "khoaofgod@gmail.com", + "homepage": "http://www.phpfastcache.com", + "role": "Developer" }, { - "name": "Joel Wurtz", - "email": "joel.wurtz@gmail.com" + "name": "Georges.L", + "email": "contact@geolim4.com", + "homepage": "https://github.com/Geolim4", + "role": "Developer" } ], - "description": "Promise used for asynchronous HTTP requests", - "homepage": "http://httplug.io", + "description": "PHP Cache Class - Reduce your database call using cache system. PhpFastCache handles a lot of drivers such as Apc(u), CouchBase, Mongodb, Files, (P)redis, Leveldb, Memcache(d), Ssdb, Sqlite, Wincache, Xcache, Zend Data Cache.", + "homepage": "http://www.phpfastcache.com", "keywords": [ - "promise" + "LevelDb", + "apc", + "apcu", + "cache", + "cache class", + "caching", + "cookie", + "couchbase", + "files cache", + "memcache", + "memcached", + "mongodb", + "mysql cache", + "pdo cache", + "php cache", + "predis", + "redis", + "ssdb", + "wincache", + "xcache", + "zend", + "zend data cache", + "zend disk cache", + "zend memory cache", + "zend server" + ], + "time": "2017-02-04T01:09:40+00:00" + }, + { + "name": "phpfastcache/phpssdb", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/PHPSocialNetwork/phpssdb.git", + "reference": "6755b00200d28ce59cfabde2274d41b045acae98" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPSocialNetwork/phpssdb/zipball/6755b00200d28ce59cfabde2274d41b045acae98", + "reference": "6755b00200d28ce59cfabde2274d41b045acae98", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "phpssdb\\": "src/phpssdb/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" ], - "time": "2016-01-26T13:27:02+00:00" + "authors": [ + { + "name": "Khoa Bui", + "email": "khoaofgod@gmail.com", + "homepage": "http://www.phpfastcache.com", + "role": "Developer" + }, + { + "name": "Georges.L", + "email": "contact@geolim4.com", + "homepage": "https://github.com/Geolim4", + "role": "Developer" + } + ], + "description": "PHP SSDB Driver for phpFastCache", + "homepage": "http://www.phpfastcache.com", + "keywords": [ + "cache", + "driver", + "ssdb" + ], + "time": "2016-07-02T12:58:46+00:00" + }, + { + "name": "predis/predis", + "version": "v1.1.1", + "source": { + "type": "git", + "url": "https://github.com/nrk/predis.git", + "reference": "f0210e38881631afeafb56ab43405a92cafd9fd1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nrk/predis/zipball/f0210e38881631afeafb56ab43405a92cafd9fd1", + "reference": "f0210e38881631afeafb56ab43405a92cafd9fd1", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "require-dev": { + "phpunit/phpunit": "~4.8" + }, + "suggest": { + "ext-curl": "Allows access to Webdis when paired with phpiredis", + "ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol" + }, + "type": "library", + "autoload": { + "psr-4": { + "Predis\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Daniele Alessandri", + "email": "suppakilla@gmail.com", + "homepage": "http://clorophilla.net" + } + ], + "description": "Flexible and feature-complete Redis client for PHP and HHVM", + "homepage": "http://github.com/nrk/predis", + "keywords": [ + "nosql", + "predis", + "redis" + ], + "time": "2016-06-16T16:22:20+00:00" + }, + { + "name": "psr/cache", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/cache.git", + "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/cache/zipball/d11b50ad223250cf17b86e38383413f5a6764bf8", + "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Cache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for caching libraries", + "keywords": [ + "cache", + "psr", + "psr-6" + ], + "time": "2016-08-06T20:24:11+00:00" }, { "name": "psr/http-message", @@ -800,6 +753,119 @@ ], "time": "2015-06-14T21:17:01+00:00" }, + { + "name": "guzzlehttp/guzzle", + "version": "6.2.3", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "8d6c6cc55186db87b7dc5009827429ba4e9dc006" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/8d6c6cc55186db87b7dc5009827429ba4e9dc006", + "reference": "8d6c6cc55186db87b7dc5009827429ba4e9dc006", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.4", + "php": ">=5.5" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "^4.0", + "psr/log": "^1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.2-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2017-02-28T22:50:30+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-12-20T10:07:11+00:00" + }, { "name": "myclabs/deep-copy", "version": "1.6.0", @@ -842,6 +908,172 @@ ], "time": "2017-01-26T22:05:40+00:00" }, + { + "name": "php-http/guzzle6-adapter", + "version": "v1.1.1", + "source": { + "type": "git", + "url": "https://github.com/php-http/guzzle6-adapter.git", + "reference": "a56941f9dc6110409cfcddc91546ee97039277ab" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-http/guzzle6-adapter/zipball/a56941f9dc6110409cfcddc91546ee97039277ab", + "reference": "a56941f9dc6110409cfcddc91546ee97039277ab", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^6.0", + "php": ">=5.5.0", + "php-http/httplug": "^1.0" + }, + "provide": { + "php-http/async-client-implementation": "1.0", + "php-http/client-implementation": "1.0" + }, + "require-dev": { + "ext-curl": "*", + "php-http/adapter-integration-tests": "^0.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2-dev" + } + }, + "autoload": { + "psr-4": { + "Http\\Adapter\\Guzzle6\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com" + }, + { + "name": "David de Boer", + "email": "david@ddeboer.nl" + } + ], + "description": "Guzzle 6 HTTP Adapter", + "homepage": "http://httplug.io", + "keywords": [ + "Guzzle", + "http" + ], + "time": "2016-05-10T06:13:32+00:00" + }, + { + "name": "php-http/httplug", + "version": "v1.1.0", + "source": { + "type": "git", + "url": "https://github.com/php-http/httplug.git", + "reference": "1c6381726c18579c4ca2ef1ec1498fdae8bdf018" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-http/httplug/zipball/1c6381726c18579c4ca2ef1ec1498fdae8bdf018", + "reference": "1c6381726c18579c4ca2ef1ec1498fdae8bdf018", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "php-http/promise": "^1.0", + "psr/http-message": "^1.0" + }, + "require-dev": { + "henrikbjorn/phpspec-code-coverage": "^1.0", + "phpspec/phpspec": "^2.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + } + }, + "autoload": { + "psr-4": { + "Http\\Client\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Eric GELOEN", + "email": "geloen.eric@gmail.com" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com" + } + ], + "description": "HTTPlug, the HTTP client abstraction for PHP", + "homepage": "http://httplug.io", + "keywords": [ + "client", + "http" + ], + "time": "2016-08-31T08:30:17+00:00" + }, + { + "name": "php-http/promise", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-http/promise.git", + "reference": "dc494cdc9d7160b9a09bd5573272195242ce7980" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-http/promise/zipball/dc494cdc9d7160b9a09bd5573272195242ce7980", + "reference": "dc494cdc9d7160b9a09bd5573272195242ce7980", + "shasum": "" + }, + "require-dev": { + "henrikbjorn/phpspec-code-coverage": "^1.0", + "phpspec/phpspec": "^2.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + } + }, + "autoload": { + "psr-4": { + "Http\\Promise\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com" + }, + { + "name": "Joel Wurtz", + "email": "joel.wurtz@gmail.com" + } + ], + "description": "Promise used for asynchronous HTTP requests", + "homepage": "http://httplug.io", + "keywords": [ + "promise" + ], + "time": "2016-01-26T13:27:02+00:00" + }, { "name": "phpdocumentor/reflection-common", "version": "1.0", diff --git a/readme.md b/readme.md index 3ed2935..f7f3eaf 100644 --- a/readme.md +++ b/readme.md @@ -28,14 +28,15 @@ After doing either of the above, execute the command `composer install`. ## Usage -Please refer to our full [API documentation](https://stats.truckersmp.com/api). +Please note, that library uses File cache to take care about responsible API usage. ```php player(50); @@ -53,14 +54,18 @@ echo $player->groupName; ## Configuration +APIClient has Cache and Guzzle configuration. + We use [Guzzle](https://github.com/guzzle/guzzle) for gathering data from API endpoint. If you want to change Guzzle [configuration](http://guzzlephp.org/), you can pass config array on Client initialization. ```php +request = new Request($url, $config); + + if ($cache !== false) { + $this->cache = CacheManager::getInstance('files', [ + 'path' => $cache, + ]); + } else { + $this->cache = false; + } } /** @@ -52,11 +72,28 @@ public function __construct( */ public function player($id) { - $result = $this->request->execute('player/'.$id); + $result = $this->execute('player/' . $id, 10 * 60); return new Player($result); } + public function execute($uri, $ttl) + { + if ($this->cache !== false) { + $cached = $this->cache->getItem(urlencode($uri)); + + if (is_null($cached->get())) { + $request = $this->request->execute($uri); + $cached->set($request)->expiresAfter($ttl); + $this->cache->save($cached); + } + + return $cached->get(); + } + + return $this->request->execute($uri); + } + /** * @param $id * @@ -67,7 +104,8 @@ public function player($id) */ public function bans($id) { - $result = $this->request->execute('bans/'.$id); + $result = $this->execute('bans/' . $id, 10 * 60); + var_dump($result); return new Bans($result); } @@ -80,7 +118,7 @@ public function bans($id) */ public function servers() { - $result = $this->request->execute('servers'); + $result = $this->execute('servers', 1 * 60); return new Servers($result); } @@ -93,7 +131,7 @@ public function servers() */ public function gameTime() { - $result = $this->request->execute('game_time'); + $result = $this->execute('game_time', 1 * 60); return new GameTime($result); } @@ -106,8 +144,19 @@ public function gameTime() */ public function version() { - $result = $this->request->execute('version'); + $result = $this->execute('version', 60 * 60); return new Version($result); } + + public function clearCache() + { + if ($this->cache !== false) { + $this->cache->clear(); + + return true; + } + + return false; + } } diff --git a/src/API/Request.php b/src/API/Request.php index 5264bb5..059bfa4 100644 --- a/src/API/Request.php +++ b/src/API/Request.php @@ -11,6 +11,7 @@ use Http\Adapter\Guzzle6\Client as GuzzleAdapter; use Http\Message\MessageFactory\GuzzleMessageFactory; + class Request { /** @@ -28,29 +29,33 @@ class Request */ private $adapter; + /** * Request constructor. * * @param $apiEndpoint * @param array $config + * */ - public function __construct($apiEndpoint, $config = []) + public function __construct($apiEndpoint, $config) { $this->message = new GuzzleMessageFactory(); $this->apiEndpoint = $apiEndpoint; - $this->adapter = new GuzzleAdapter(new GuzzleClient($config)); + $this->adapter = new GuzzleAdapter(new GuzzleClient($config)); + } /** - * @param $uri + * @param string $uri URI of API method * * @return \Psr\Http\Message\ResponseInterface */ public function execute($uri) { - $request = $this->message->createRequest('GET', $this->apiEndpoint.$uri); + $request = $this->message->createRequest('GET', $this->apiEndpoint . $uri); + $result = $this->adapter->sendRequest($request); - return $this->adapter->sendRequest($request); + return json_decode((string) $result->getBody(), true, 512, JSON_BIGINT_AS_STRING); } } diff --git a/src/Types/Ban.php b/src/Types/Ban.php index 3bb639c..30c23ad 100644 --- a/src/Types/Ban.php +++ b/src/Types/Ban.php @@ -57,10 +57,10 @@ public function __construct($ban) { $this->expires = new Carbon($ban['expiration'], 'UTC'); $this->created = new Carbon($ban['timeAdded'], 'UTC'); - $this->active = $ban['active']; + $this->active = $ban['active']; - $this->reason = $ban['reason']; + $this->reason = $ban['reason']; $this->adminName = $ban['adminName']; - $this->adminID = $ban['adminID']; + $this->adminID = $ban['adminID']; } } diff --git a/src/Types/Bans.php b/src/Types/Bans.php index e548855..c64576f 100644 --- a/src/Types/Bans.php +++ b/src/Types/Bans.php @@ -23,24 +23,22 @@ class Bans implements \Iterator, \ArrayAccess /** * Bans constructor. * - * @param ResponseInterface $response + * @param array $response * - * @throws PlayerNotFoundException + * @throws \TruckersMP\Exceptions\PlayerNotFoundException */ - public function __construct(ResponseInterface $response) + public function __construct(array $response) { $this->position = 0; - $json = json_decode((string) $response->getBody(), true, 512, JSON_BIGINT_AS_STRING); - - if ($json['error'] && - ($json['descriptor'] == 'No player ID submitted' || - $json['descriptor'] == 'Invalid user ID') + if ($response['error'] && + ($response['descriptor'] == 'No player ID submitted' || + $response['descriptor'] == 'Invalid user ID') ) { - throw new PlayerNotFoundException($json['descriptor']); + throw new PlayerNotFoundException($response['descriptor']); } - foreach ($json['response'] as $k => $ban) { + foreach ($response['response'] as $k => $ban) { $this->bans[$k] = new Ban($ban); } } diff --git a/src/Types/GameTime.php b/src/Types/GameTime.php index 2374986..46a5c38 100644 --- a/src/Types/GameTime.php +++ b/src/Types/GameTime.php @@ -9,7 +9,6 @@ namespace TruckersMP\Types; use Carbon\Carbon; -use Psr\Http\Message\ResponseInterface; class GameTime { @@ -18,30 +17,28 @@ class GameTime /** * GameTime constructor. * - * @param \Psr\Http\Message\ResponseInterface $response + * @param array $response * * @throws \Exception */ - public function __construct(ResponseInterface $response) + public function __construct(array $response) { - $json = json_decode((string) $response->getBody(), true, 512, JSON_BIGINT_AS_STRING); - - if ($json['error']) { + if ($response['error']) { // TODO: actually throw a usable error throw new \Exception('API Error'); } - $load['minutes'] = $json['game_time']; + $load['minutes'] = $response['game_time']; - $load['hours'] = $load['minutes'] / 60; + $load['hours'] = $load['minutes'] / 60; $load['minutes'] = $load['minutes'] % 60; - $load['days'] = $load['hours'] / 24; + $load['days'] = $load['hours'] / 24; $load['hours'] = $load['hours'] % 24; $load['months'] = $load['days'] / 30; - $load['days'] = $load['days'] % 30; + $load['days'] = $load['days'] % 30; - $load['years'] = intval($load['months'] / 12); + $load['years'] = intval($load['months'] / 12); $load['months'] = $load['months'] % 12; $this->time = Carbon::create($load['years'], $load['months'], $load['days'], $load['hours'], $load['minutes']); diff --git a/src/Types/Player.php b/src/Types/Player.php index c1cd9b5..ac3e4cf 100644 --- a/src/Types/Player.php +++ b/src/Types/Player.php @@ -2,7 +2,6 @@ namespace TruckersMP\Types; -use Psr\Http\Message\ResponseInterface; use TruckersMP\Exceptions\PlayerNotFoundException; class Player @@ -66,25 +65,23 @@ class Player /** * Player constructor. * - * @param ResponseInterface $response + * @param array $response * - * @throws PlayerNotFoundException + * @throws \TruckersMP\Exceptions\PlayerNotFoundException */ - public function __construct(ResponseInterface $response) + public function __construct(array $response) { - $json = json_decode((string) $response->getBody(), true, 512, JSON_BIGINT_AS_STRING); - - if ($json['error']) { - throw new PlayerNotFoundException($json['response']); + if ($response['error']) { + throw new PlayerNotFoundException($response['response']); } - $this->id = $json['response']['id']; - $this->name = $json['response']['name']; - $this->avatar = $json['response']['avatar']; - $this->joinDate = $json['response']['joinDate']; - $this->steamID64 = $json['response']['steamID64']; - $this->groupID = $json['response']['groupID']; - $this->groupName = $json['response']['groupName']; - $this->inGameAdmin = $json['response']['permissions']['isGameAdmin']; + $this->id = $response['response']['id']; + $this->name = $response['response']['name']; + $this->avatar = $response['response']['avatar']; + $this->joinDate = $response['response']['joinDate']; + $this->steamID64 = $response['response']['steamID64']; + $this->groupID = $response['response']['groupID']; + $this->groupName = $response['response']['groupName']; + $this->inGameAdmin = $response['response']['permissions']['isGameAdmin']; } } diff --git a/src/Types/Server.php b/src/Types/Server.php index 12be736..5a1a0b2 100644 --- a/src/Types/Server.php +++ b/src/Types/Server.php @@ -119,25 +119,25 @@ class Server /** * Server constructor. * - * @param $server + * @param array $server */ - public function __construct($server) + public function __construct(array $server) { - $this->id = intval($server['id']); - $this->game = $server['game']; - $this->ip = $server['ip']; - $this->port = intval($server['port']); - $this->name = $server['name']; - $this->shortName = $server['shortname']; - $this->online = boolval($server['online']); - $this->players = intval($server['players']); - $this->queue = intval($server['queue']); - $this->maxPlayers = intval($server['maxplayers']); - $this->speedLimiter = boolval($server['speedlimiter']); - $this->collisions = boolval($server['collisions']); - $this->carsForPlayers = boolval($server['carsforplayers']); + $this->id = intval($server['id']); + $this->game = $server['game']; + $this->ip = $server['ip']; + $this->port = intval($server['port']); + $this->name = $server['name']; + $this->shortName = $server['shortname']; + $this->online = boolval($server['online']); + $this->players = intval($server['players']); + $this->queue = intval($server['queue']); + $this->maxPlayers = intval($server['maxplayers']); + $this->speedLimiter = boolval($server['speedlimiter']); + $this->collisions = boolval($server['collisions']); + $this->carsForPlayers = boolval($server['carsforplayers']); $this->policeCarsForPlayers = boolval($server['policecarsforplayers']); - $this->afkEnabled = boolval($server['afkenabled']); - $this->syncDelay = intval($server['syncdelay']); + $this->afkEnabled = boolval($server['afkenabled']); + $this->syncDelay = intval($server['syncdelay']); } } diff --git a/src/Types/Servers.php b/src/Types/Servers.php index 743db6b..00c8c90 100644 --- a/src/Types/Servers.php +++ b/src/Types/Servers.php @@ -24,21 +24,19 @@ class Servers implements \Iterator, \ArrayAccess /** * Servers constructor. * - * @param ResponseInterface $response + * @param array $response * * @throws APIErrorException */ - public function __construct(ResponseInterface $response) + public function __construct(array $response) { $this->position = 0; - $json = json_decode((string) $response->getBody(), true, 512, JSON_BIGINT_AS_STRING); - - if ($json['error'] == 'true' && $json['descriptor'] == 'Unable to fetch servers') { - throw new APIErrorException($json['descriptor']); + if ($response['error'] == 'true' && $response['descriptor'] == 'Unable to fetch servers') { + throw new APIErrorException($response['descriptor']); } - foreach ($json['response'] as $k => $server) { + foreach ($response['response'] as $k => $server) { $this->servers[$k] = new Server($server); } } diff --git a/src/Types/Version.php b/src/Types/Version.php index 41e2cc1..9765a2b 100644 --- a/src/Types/Version.php +++ b/src/Types/Version.php @@ -9,7 +9,6 @@ namespace TruckersMP\Types; use Carbon\Carbon; -use Psr\Http\Message\ResponseInterface; class Version { @@ -24,29 +23,27 @@ class Version /** * Version constructor. * - * @param \Psr\Http\Message\ResponseInterface $response + * @param array $response */ - public function __construct(ResponseInterface $response) + public function __construct(array $response) { - $json = json_decode((string) $response->getBody(), true, 512, JSON_BIGINT_AS_STRING); - - $this->version = new \stdClass(); - $this->version->human = $json['name']; - $this->version->stage = $json['stage']; - $this->version->nummeric = $json['numeric']; - - $this->checksum = new \stdClass(); - $this->checksum->atsmp = new \stdClass(); - $this->checksum->atsmp->dll = $json['atsmp_checksum']['dll']; - $this->checksum->atsmp->adb = $json['atsmp_checksum']['adb']; - $this->checksum->ets2mp = new \stdClass(); - $this->checksum->ets2mp->dll = $json['atsmp_checksum']['dll']; - $this->checksum->ets2mp->adb = $json['atsmp_checksum']['adb']; - - $this->released = new Carbon($json['time'], 'UTC'); - - $this->support = new \stdClass(); - $this->support->ets2 = $json['supported_game_version']; - $this->support->ats = $json['supported_ats_game_version']; + $this->version = new \stdClass(); + $this->version->human = $response['name']; + $this->version->stage = $response['stage']; + $this->version->nummeric = $response['numeric']; + + $this->checksum = new \stdClass(); + $this->checksum->atsmp = new \stdClass(); + $this->checksum->atsmp->dll = $response['atsmp_checksum']['dll']; + $this->checksum->atsmp->adb = $response['atsmp_checksum']['adb']; + $this->checksum->ets2mp = new \stdClass(); + $this->checksum->ets2mp->dll = $response['atsmp_checksum']['dll']; + $this->checksum->ets2mp->adb = $response['atsmp_checksum']['adb']; + + $this->released = new Carbon($response['time'], 'UTC'); + + $this->support = new \stdClass(); + $this->support->ets2 = $response['supported_game_version']; + $this->support->ats = $response['supported_ats_game_version']; } } From 675f2aad897c62c50e21e4539049989e489d1493 Mon Sep 17 00:00:00 2001 From: CJMAXiK Date: Tue, 11 Apr 2017 14:11:09 +1000 Subject: [PATCH 05/11] Style Fixes --- .travis.yml | 4 ++-- composer.json | 4 ++-- src/API/APIClient.php | 8 +------- src/API/Request.php | 7 ++----- 4 files changed, 7 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index a3caff7..a4a169c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,7 +22,7 @@ before_script: script: - vendor/bin/phpunit - - vendor/bin/phpcs src --standard=PSR2 - - vendor/bin/phpcs Tests --standard=PSR2 + - vendor/bin/phpcs src + - vendor/bin/phpcs Tests diff --git a/composer.json b/composer.json index 24a335c..3655c15 100644 --- a/composer.json +++ b/composer.json @@ -51,8 +51,8 @@ "scripts": { "test": [ "./vendor/bin/phpunit", - "./vendor/bin/phpcs src --standard=PSR2", - "./vendor/bin/phpcs Tests --standard=PSR2" + "./vendor/bin/phpcs src", + "./vendor/bin/phpcs Tests" ] } } diff --git a/src/API/APIClient.php b/src/API/APIClient.php index 0a87712..e1d04ef 100644 --- a/src/API/APIClient.php +++ b/src/API/APIClient.php @@ -38,13 +38,7 @@ class APIClient * @throws \phpFastCache\Exceptions\phpFastCacheDriverCheckException */ - public function __construct( - $cache = false, - $config = [], - $apiEndpoint = 'api.truckersmp.com', - $version = 'v2', - $secure = true - ) + public function __construct($cache = false, $config = [], $apiEndpoint = 'api.truckersmp.com', $version = 'v2', $secure = true) { $scheme = $secure ? 'https' : 'http'; $url = $scheme . '://' . $apiEndpoint . '/' . $version . '/'; diff --git a/src/API/Request.php b/src/API/Request.php index 059bfa4..b875cf0 100644 --- a/src/API/Request.php +++ b/src/API/Request.php @@ -11,7 +11,6 @@ use Http\Adapter\Guzzle6\Client as GuzzleAdapter; use Http\Message\MessageFactory\GuzzleMessageFactory; - class Request { /** @@ -29,7 +28,6 @@ class Request */ private $adapter; - /** * Request constructor. * @@ -43,7 +41,6 @@ public function __construct($apiEndpoint, $config) $this->apiEndpoint = $apiEndpoint; $this->adapter = new GuzzleAdapter(new GuzzleClient($config)); - } /** @@ -54,8 +51,8 @@ public function __construct($apiEndpoint, $config) public function execute($uri) { $request = $this->message->createRequest('GET', $this->apiEndpoint . $uri); - $result = $this->adapter->sendRequest($request); + $result = $this->adapter->sendRequest($request); - return json_decode((string) $result->getBody(), true, 512, JSON_BIGINT_AS_STRING); + return json_decode((string)$result->getBody(), true, 512, JSON_BIGINT_AS_STRING); } } From 7aecee3d77e95a14f9b09c56090de9700234c7d1 Mon Sep 17 00:00:00 2001 From: CJMAXiK Date: Tue, 11 Apr 2017 14:12:39 +1000 Subject: [PATCH 06/11] Style Fixes --- .travis.yml | 4 ++-- composer.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index a4a169c..a3caff7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,7 +22,7 @@ before_script: script: - vendor/bin/phpunit - - vendor/bin/phpcs src - - vendor/bin/phpcs Tests + - vendor/bin/phpcs src --standard=PSR2 + - vendor/bin/phpcs Tests --standard=PSR2 diff --git a/composer.json b/composer.json index 3655c15..24a335c 100644 --- a/composer.json +++ b/composer.json @@ -51,8 +51,8 @@ "scripts": { "test": [ "./vendor/bin/phpunit", - "./vendor/bin/phpcs src", - "./vendor/bin/phpcs Tests" + "./vendor/bin/phpcs src --standard=PSR2", + "./vendor/bin/phpcs Tests --standard=PSR2" ] } } From 71c09a6200180167cdb4f4b3f289c04cf1600f9e Mon Sep 17 00:00:00 2001 From: CJMAXiK Date: Tue, 11 Apr 2017 14:25:53 +1000 Subject: [PATCH 07/11] APIClient Style Fixes --- Tests/ClientTest.php | 2 +- src/API/APIClient.php | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index 5cf1e0a..e322562 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -116,7 +116,7 @@ public function testGameTime() $this->assertNotEmpty($time); } - public function testCache() + public function testClearCache() { $cache = $this->client->clearCache(); diff --git a/src/API/APIClient.php b/src/API/APIClient.php index e1d04ef..81ba15d 100644 --- a/src/API/APIClient.php +++ b/src/API/APIClient.php @@ -26,22 +26,24 @@ class APIClient */ private $cache; + const API_ENDPOINT = 'api.truckersmp.com'; + + const API_VERSION = 'v2'; + /** * APIClient constructor. * * @param string|bool $cache * @param array $config - * @param string $apiEndpoint - * @param string $version * @param bool $secure * * @throws \phpFastCache\Exceptions\phpFastCacheDriverCheckException */ - public function __construct($cache = false, $config = [], $apiEndpoint = 'api.truckersmp.com', $version = 'v2', $secure = true) + public function __construct($cache = false, $config = [], $secure = true) { $scheme = $secure ? 'https' : 'http'; - $url = $scheme . '://' . $apiEndpoint . '/' . $version . '/'; + $url = $scheme . '://' . self::API_ENDPOINT . '/' . self::API_VERSION . '/'; $this->request = new Request($url, $config); From b7714a1d952c1085014b10567014e25e06fb7b39 Mon Sep 17 00:00:00 2001 From: CJMAXiK Date: Tue, 11 Apr 2017 17:19:37 +1000 Subject: [PATCH 08/11] Get rid of caching, make some examples instead --- Tests/ClientTest.php | 23 +--- composer.json | 3 +- composer.lock | 234 +------------------------------- examples/cache/Laravel.php | 33 +++++ examples/cache/phpFastCache.php | 36 +++++ readme.md | 18 ++- src/API/APIClient.php | 53 ++------ 7 files changed, 92 insertions(+), 308 deletions(-) create mode 100644 examples/cache/Laravel.php create mode 100644 examples/cache/phpFastCache.php diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index e322562..c9dc32c 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -22,33 +22,14 @@ public function __construct() { parent::__construct(); - $cachePath = __DIR__ . '/cache/'; - $this->client = new APIClient($cachePath); + $this->client = new APIClient(); } /** * @throws \Exception * @throws \Http\Client\Exception */ - public function testColdPlayerCache() - { - $this->getPlayer(); - } - - /** - * @throws \Exception - * @throws \Http\Client\Exception - */ - public function testHotPlayerCache() - { - $this->getPlayer(); - } - - /** - * @throws \Exception - * @throws \Http\Client\Exception - */ - public function getPlayer() + public function testPlayer() { $player = $this->client->player($this->testAccount); diff --git a/composer.json b/composer.json index 24a335c..2f6d7a8 100644 --- a/composer.json +++ b/composer.json @@ -27,8 +27,7 @@ "php": ">=5.6.0", "nesbot/carbon": "^1.21", "php-http/message": "^1.2", - "guzzlehttp/psr7": "^1.3", - "phpFastCache/phpFastCache": "^5.0" + "guzzlehttp/psr7": "^1.3" }, "require-dev": { "phpunit/phpunit": "^5.3", diff --git a/composer.lock b/composer.lock index 8f42498..6875e2b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "77da861a1797d508d71a3beaf73575be", + "content-hash": "b1dd8f3b08c2fbe389ccc6ed1f4f8f96", "packages": [ { "name": "clue/stream-filter", @@ -292,238 +292,6 @@ ], "time": "2015-12-19T14:08:53+00:00" }, - { - "name": "phpfastcache/phpfastcache", - "version": "5.0.15", - "source": { - "type": "git", - "url": "https://github.com/PHPSocialNetwork/phpfastcache.git", - "reference": "f9c09668b313dd53a6417be42e4af02649914a89" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/PHPSocialNetwork/phpfastcache/zipball/f9c09668b313dd53a6417be42e4af02649914a89", - "reference": "f9c09668b313dd53a6417be42e4af02649914a89", - "shasum": "" - }, - "require": { - "ext-json": "*", - "php": ">=5.5.0", - "phpfastcache/phpssdb": "~1.0.0", - "predis/predis": "~1.1.0", - "psr/cache": "~1.0.0" - }, - "suggest": { - "ext-apc": "*", - "ext-intl": "*", - "ext-mbstring": "*", - "ext-memcache": "*", - "ext-memcached": "*", - "ext-predis": "*", - "ext-redis": "*", - "ext-sqlite": "*" - }, - "type": "library", - "autoload": { - "psr-4": { - "phpFastCache\\": "src/phpFastCache/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Khoa Bui", - "email": "khoaofgod@gmail.com", - "homepage": "http://www.phpfastcache.com", - "role": "Developer" - }, - { - "name": "Georges.L", - "email": "contact@geolim4.com", - "homepage": "https://github.com/Geolim4", - "role": "Developer" - } - ], - "description": "PHP Cache Class - Reduce your database call using cache system. PhpFastCache handles a lot of drivers such as Apc(u), CouchBase, Mongodb, Files, (P)redis, Leveldb, Memcache(d), Ssdb, Sqlite, Wincache, Xcache, Zend Data Cache.", - "homepage": "http://www.phpfastcache.com", - "keywords": [ - "LevelDb", - "apc", - "apcu", - "cache", - "cache class", - "caching", - "cookie", - "couchbase", - "files cache", - "memcache", - "memcached", - "mongodb", - "mysql cache", - "pdo cache", - "php cache", - "predis", - "redis", - "ssdb", - "wincache", - "xcache", - "zend", - "zend data cache", - "zend disk cache", - "zend memory cache", - "zend server" - ], - "time": "2017-02-04T01:09:40+00:00" - }, - { - "name": "phpfastcache/phpssdb", - "version": "1.0.0", - "source": { - "type": "git", - "url": "https://github.com/PHPSocialNetwork/phpssdb.git", - "reference": "6755b00200d28ce59cfabde2274d41b045acae98" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/PHPSocialNetwork/phpssdb/zipball/6755b00200d28ce59cfabde2274d41b045acae98", - "reference": "6755b00200d28ce59cfabde2274d41b045acae98", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "type": "library", - "autoload": { - "psr-4": { - "phpssdb\\": "src/phpssdb/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Khoa Bui", - "email": "khoaofgod@gmail.com", - "homepage": "http://www.phpfastcache.com", - "role": "Developer" - }, - { - "name": "Georges.L", - "email": "contact@geolim4.com", - "homepage": "https://github.com/Geolim4", - "role": "Developer" - } - ], - "description": "PHP SSDB Driver for phpFastCache", - "homepage": "http://www.phpfastcache.com", - "keywords": [ - "cache", - "driver", - "ssdb" - ], - "time": "2016-07-02T12:58:46+00:00" - }, - { - "name": "predis/predis", - "version": "v1.1.1", - "source": { - "type": "git", - "url": "https://github.com/nrk/predis.git", - "reference": "f0210e38881631afeafb56ab43405a92cafd9fd1" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/nrk/predis/zipball/f0210e38881631afeafb56ab43405a92cafd9fd1", - "reference": "f0210e38881631afeafb56ab43405a92cafd9fd1", - "shasum": "" - }, - "require": { - "php": ">=5.3.9" - }, - "require-dev": { - "phpunit/phpunit": "~4.8" - }, - "suggest": { - "ext-curl": "Allows access to Webdis when paired with phpiredis", - "ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol" - }, - "type": "library", - "autoload": { - "psr-4": { - "Predis\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Daniele Alessandri", - "email": "suppakilla@gmail.com", - "homepage": "http://clorophilla.net" - } - ], - "description": "Flexible and feature-complete Redis client for PHP and HHVM", - "homepage": "http://github.com/nrk/predis", - "keywords": [ - "nosql", - "predis", - "redis" - ], - "time": "2016-06-16T16:22:20+00:00" - }, - { - "name": "psr/cache", - "version": "1.0.1", - "source": { - "type": "git", - "url": "https://github.com/php-fig/cache.git", - "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-fig/cache/zipball/d11b50ad223250cf17b86e38383413f5a6764bf8", - "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8", - "shasum": "" - }, - "require": { - "php": ">=5.3.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Psr\\Cache\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" - } - ], - "description": "Common interface for caching libraries", - "keywords": [ - "cache", - "psr", - "psr-6" - ], - "time": "2016-08-06T20:24:11+00:00" - }, { "name": "psr/http-message", "version": "1.0.1", diff --git a/examples/cache/Laravel.php b/examples/cache/Laravel.php new file mode 100644 index 0000000..a88bb1a --- /dev/null +++ b/examples/cache/Laravel.php @@ -0,0 +1,33 @@ +input('needle'); + + /** + * Caching search for that needle for 10 minutes + */ + $tmp = Cache::remember('users.player.' . (string)$needle, 10, function () use ($needle, $client) { + return $client->player($needle); + }); + + return view('search', compact($tmp)); + } +} diff --git a/examples/cache/phpFastCache.php b/examples/cache/phpFastCache.php new file mode 100644 index 0000000..08e59c3 --- /dev/null +++ b/examples/cache/phpFastCache.php @@ -0,0 +1,36 @@ + '/path/to/your/cache/folder', + ]); + +// In your class, function, you can call the Cache +$InstanceCache = CacheManager::getInstance('files'); + +$client = new APIClient(); + +/** + * Try to get $products from Caching First + * product_page is "identity keyword"; + */ +$key = "player_" . $player_id; +$CachedRequest = $InstanceCache->getItem($key); + +if (is_null($CachedRequest->get())) { + $request = $client->player($player_id); + + $CachedRequest->set($request)->expiresAfter(5);//in seconds, also accepts Datetime + $InstanceCache->save($CachedRequest); // Save the cache item just like you do with doctrine and entities +} + +var_dump($CachedRequest->get()); diff --git a/readme.md b/readme.md index f7f3eaf..5fd9729 100644 --- a/readme.md +++ b/readme.md @@ -28,15 +28,15 @@ After doing either of the above, execute the command `composer install`. ## Usage -Please note, that library uses File cache to take care about responsible API usage. + +> **Please note: this examples doesn't use caching. You should cache your requests in order to use API responsibly. Some examples can be found in `examples\cache` folder.** ```php player(50); @@ -54,20 +54,18 @@ echo $player->groupName; ## Configuration -APIClient has Cache and Guzzle configuration. - We use [Guzzle](https://github.com/guzzle/guzzle) for gathering data from API endpoint. If you want to change Guzzle [configuration](http://guzzlephp.org/), you can pass config array on Client initialization. ```php request = new Request($url, $config); - - if ($cache !== false) { - $this->cache = CacheManager::getInstance('files', [ - 'path' => $cache, - ]); - } else { - $this->cache = false; - } } /** @@ -68,28 +55,11 @@ public function __construct($cache = false, $config = [], $secure = true) */ public function player($id) { - $result = $this->execute('player/' . $id, 10 * 60); + $result = $this->request->execute('player/' . $id, 10 * 60); return new Player($result); } - public function execute($uri, $ttl) - { - if ($this->cache !== false) { - $cached = $this->cache->getItem(urlencode($uri)); - - if (is_null($cached->get())) { - $request = $this->request->execute($uri); - $cached->set($request)->expiresAfter($ttl); - $this->cache->save($cached); - } - - return $cached->get(); - } - - return $this->request->execute($uri); - } - /** * @param $id * @@ -100,8 +70,7 @@ public function execute($uri, $ttl) */ public function bans($id) { - $result = $this->execute('bans/' . $id, 10 * 60); - var_dump($result); + $result = $this->request->execute('bans/' . $id, 10 * 60); return new Bans($result); } @@ -114,7 +83,7 @@ public function bans($id) */ public function servers() { - $result = $this->execute('servers', 1 * 60); + $result = $this->request->execute('servers', 1 * 60); return new Servers($result); } @@ -127,7 +96,7 @@ public function servers() */ public function gameTime() { - $result = $this->execute('game_time', 1 * 60); + $result = $this->request->execute('game_time', 1 * 60); return new GameTime($result); } @@ -140,7 +109,7 @@ public function gameTime() */ public function version() { - $result = $this->execute('version', 60 * 60); + $result = $this->request->execute('version', 60 * 60); return new Version($result); } @@ -148,7 +117,7 @@ public function version() public function clearCache() { if ($this->cache !== false) { - $this->cache->clear(); + $this->cache->dropCache(); return true; } From a02d682c9cbc437e26e33ddfc8be6ce00dfab936 Mon Sep 17 00:00:00 2001 From: CJMAXiK Date: Tue, 11 Apr 2017 17:32:12 +1000 Subject: [PATCH 09/11] Fixes --- Tests/ClientTest.php | 7 ------- readme.md | 22 +++++++++++++++++----- src/API/APIClient.php | 32 ++++++++------------------------ src/API/Request.php | 2 +- 4 files changed, 26 insertions(+), 37 deletions(-) diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index c9dc32c..686ea9f 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -96,11 +96,4 @@ public function testGameTime() $this->assertNotEmpty($time); } - - public function testClearCache() - { - $cache = $this->client->clearCache(); - - $this->assertTrue($cache); - } } diff --git a/readme.md b/readme.md index 5fd9729..f65cbdb 100644 --- a/readme.md +++ b/readme.md @@ -2,9 +2,9 @@ [![Build Status](https://travis-ci.org/TruckersMP/API-Client.svg?branch=master)](https://travis-ci.org/TruckersMP/API-Client) -This is a PHP library created to simplify development using the [TruckersMP](http://truckersmp.com/) API. This client library provides functions to connect and gather data from each API endpoint, and returns the data as an array. +This is a PHP library created to simplify development using the [TruckersMP](http://truckersmp.com/) API. This client library provides functions to connect and gather data from each API endpoint, and returns the data as an object. -If you want to use API without library (or if you have advanced knowledge of PHP), you can use it [directly](https://stats.truckersmp.com/api). +> If you want to use API without library (or if you have advanced knowledge of PHP), check out [API documentation](https://stats.truckersmp.com/api). ## Warning! @@ -28,7 +28,6 @@ After doing either of the above, execute the command `composer install`. ## Usage - > **Please note: this examples doesn't use caching. You should cache your requests in order to use API responsibly. Some examples can be found in `examples\cache` folder.** ```php @@ -52,6 +51,18 @@ echo $player->groupName; ``` +## Methods + +`player()` - find player by TruckersMP ID or Steam ID + +`bans()` - view bans by TruckersMP ID + +`servers()` - servers list + +`gameTime()` - gameserver time (uses [Carbon](http://carbon.nesbot.com/docs/)) + +`version()`- TruckersMP version info + ## Configuration We use [Guzzle](https://github.com/guzzle/guzzle) for gathering data from API endpoint. If you want to change Guzzle [configuration](http://guzzlephp.org/), you can pass config array on Client initialization. @@ -66,6 +77,7 @@ $client = new APIClient([ All other settings you can find in `APIClient.php` constructor. -## Tests -To run tests, use `composer test` +## Need Help? + +If you have any questions about library usage, you can create new issue or make a topic on [our forum](https://forum.truckersmp.com/index.php?/forum/198-developer-portal/). diff --git a/src/API/APIClient.php b/src/API/APIClient.php index 8259a8f..b351292 100644 --- a/src/API/APIClient.php +++ b/src/API/APIClient.php @@ -7,7 +7,6 @@ namespace TruckersMP\API; -use Desarrolla2\Cache\Cache; use TruckersMP\Types\Bans; use TruckersMP\Types\GameTime; use TruckersMP\Types\Player; @@ -18,21 +17,17 @@ class APIClient { const API_ENDPOINT = 'api.truckersmp.com'; const API_VERSION = 'v2'; + /** * @var \TruckersMP\API\Request */ private $request; - /** - * @var \TruckersMP\API\FileCache|bool - */ - private $cache; - /** * APIClient constructor. * - * @param array $config - * @param bool $secure + * @param array $config + * @param bool $secure */ public function __construct($config = [], $secure = true) @@ -55,7 +50,7 @@ public function __construct($config = [], $secure = true) */ public function player($id) { - $result = $this->request->execute('player/' . $id, 10 * 60); + $result = $this->request->execute('player/' . $id); return new Player($result); } @@ -70,7 +65,7 @@ public function player($id) */ public function bans($id) { - $result = $this->request->execute('bans/' . $id, 10 * 60); + $result = $this->request->execute('bans/' . $id); return new Bans($result); } @@ -83,7 +78,7 @@ public function bans($id) */ public function servers() { - $result = $this->request->execute('servers', 1 * 60); + $result = $this->request->execute('servers'); return new Servers($result); } @@ -96,7 +91,7 @@ public function servers() */ public function gameTime() { - $result = $this->request->execute('game_time', 1 * 60); + $result = $this->request->execute('game_time'); return new GameTime($result); } @@ -109,19 +104,8 @@ public function gameTime() */ public function version() { - $result = $this->request->execute('version', 60 * 60); + $result = $this->request->execute('version'); return new Version($result); } - - public function clearCache() - { - if ($this->cache !== false) { - $this->cache->dropCache(); - - return true; - } - - return false; - } } diff --git a/src/API/Request.php b/src/API/Request.php index b875cf0..d0737c4 100644 --- a/src/API/Request.php +++ b/src/API/Request.php @@ -46,7 +46,7 @@ public function __construct($apiEndpoint, $config) /** * @param string $uri URI of API method * - * @return \Psr\Http\Message\ResponseInterface + * @return array */ public function execute($uri) { From 7ce03e2893f80d7f6f93f3e7be5a41fe48c94be2 Mon Sep 17 00:00:00 2001 From: Max Mekenya Date: Wed, 12 Apr 2017 18:46:52 +1000 Subject: [PATCH 10/11] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2f6d7a8..799e8aa 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "truckersmp/api-client", + "name": "cjmaxik/api-client", "description": "TruckersMP API Library helps connecting to and using the TruckersMP API.", "homepage": "https://truckersmp.com/", "license": "MIT", From 718b4cb59b3fc71c4782e7f1cbf3bfe740cb56c8 Mon Sep 17 00:00:00 2001 From: Max Mekenya Date: Wed, 12 Apr 2017 19:07:20 +1000 Subject: [PATCH 11/11] Revert name --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 799e8aa..2f6d7a8 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "cjmaxik/api-client", + "name": "truckersmp/api-client", "description": "TruckersMP API Library helps connecting to and using the TruckersMP API.", "homepage": "https://truckersmp.com/", "license": "MIT",