PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
UpdateCheckTask.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\updater;
25
28use function is_array;
29use function is_string;
30use function json_decode;
31
33 private const TLS_KEY_UPDATER = "updater";
34
35 private string $error = "Unknown error";
36
37 public function __construct(
38 UpdateChecker $updater,
39 private string $endpoint,
40 private string $channel
41 ){
42 $this->storeLocal(self::TLS_KEY_UPDATER, $updater);
43 }
44
45 public function onRun() : void{
46 $error = "";
47 $response = Internet::getURL($this->endpoint . "?channel=" . $this->channel, 4, [], $error);
48 $this->error = $error;
49
50 if($response !== null){
51 $response = json_decode($response->getBody(), true);
52 if(is_array($response)){
53 if(isset($response["error"]) && is_string($response["error"])){
54 $this->error = $response["error"];
55 }else{
56 $mapper = new \JsonMapper();
57 $mapper->bExceptionOnMissingData = true;
58 $mapper->bStrictObjectTypeChecking = true;
59 $mapper->bEnforceMapType = false;
60 try{
62 $responseObj = $mapper->map($response, new UpdateInfo());
63 $this->setResult($responseObj);
64 }catch(\JsonMapper_Exception $e){
65 $this->error = "Invalid JSON response data: " . $e->getMessage();
66 }
67 }
68 }else{
69 $this->error = "Invalid response data";
70 }
71 }
72 }
73
74 public function onCompletion() : void{
76 $updater = $this->fetchLocal(self::TLS_KEY_UPDATER);
77 if($this->hasResult()){
79 $response = $this->getResult();
80 $updater->checkUpdateCallback($response);
81 }else{
82 $updater->checkUpdateError($this->error);
83 }
84 }
85}
storeLocal(string $key, mixed $complexData)
Definition: AsyncTask.php:214
checkUpdateCallback(UpdateInfo $updateInfo)
static getURL(string $page, int $timeout=10, array $extraHeaders=[], &$err=null)
Definition: Internet.php:147