Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\VersionUpdater;
|
Chris@13
|
13
|
Chris@13
|
14 use Psy\Shell;
|
Chris@13
|
15
|
Chris@13
|
16 class GitHubChecker implements Checker
|
Chris@13
|
17 {
|
Chris@13
|
18 const URL = 'https://api.github.com/repos/bobthecow/psysh/releases/latest';
|
Chris@13
|
19
|
Chris@13
|
20 private $latest;
|
Chris@13
|
21
|
Chris@13
|
22 /**
|
Chris@13
|
23 * @return bool
|
Chris@13
|
24 */
|
Chris@13
|
25 public function isLatest()
|
Chris@13
|
26 {
|
Chris@17
|
27 return \version_compare(Shell::VERSION, $this->getLatest(), '>=');
|
Chris@13
|
28 }
|
Chris@13
|
29
|
Chris@13
|
30 /**
|
Chris@13
|
31 * @return string
|
Chris@13
|
32 */
|
Chris@13
|
33 public function getLatest()
|
Chris@13
|
34 {
|
Chris@13
|
35 if (!isset($this->latest)) {
|
Chris@13
|
36 $this->setLatest($this->getVersionFromTag());
|
Chris@13
|
37 }
|
Chris@13
|
38
|
Chris@13
|
39 return $this->latest;
|
Chris@13
|
40 }
|
Chris@13
|
41
|
Chris@13
|
42 /**
|
Chris@13
|
43 * @param string $version
|
Chris@13
|
44 */
|
Chris@13
|
45 public function setLatest($version)
|
Chris@13
|
46 {
|
Chris@13
|
47 $this->latest = $version;
|
Chris@13
|
48 }
|
Chris@13
|
49
|
Chris@13
|
50 /**
|
Chris@13
|
51 * @return string|null
|
Chris@13
|
52 */
|
Chris@13
|
53 private function getVersionFromTag()
|
Chris@13
|
54 {
|
Chris@13
|
55 $contents = $this->fetchLatestRelease();
|
Chris@13
|
56 if (!$contents || !isset($contents->tag_name)) {
|
Chris@13
|
57 throw new \InvalidArgumentException('Unable to check for updates');
|
Chris@13
|
58 }
|
Chris@13
|
59 $this->setLatest($contents->tag_name);
|
Chris@13
|
60
|
Chris@13
|
61 return $this->getLatest();
|
Chris@13
|
62 }
|
Chris@13
|
63
|
Chris@13
|
64 /**
|
Chris@13
|
65 * Set to public to make testing easier.
|
Chris@13
|
66 *
|
Chris@13
|
67 * @return mixed
|
Chris@13
|
68 */
|
Chris@13
|
69 public function fetchLatestRelease()
|
Chris@13
|
70 {
|
Chris@17
|
71 $context = \stream_context_create([
|
Chris@13
|
72 'http' => [
|
Chris@13
|
73 'user_agent' => 'PsySH/' . Shell::VERSION,
|
Chris@13
|
74 'timeout' => 3,
|
Chris@13
|
75 ],
|
Chris@13
|
76 ]);
|
Chris@13
|
77
|
Chris@17
|
78 \set_error_handler(function () {
|
Chris@13
|
79 // Just ignore all errors with this. The checker will throw an exception
|
Chris@13
|
80 // if it doesn't work :)
|
Chris@13
|
81 });
|
Chris@13
|
82
|
Chris@17
|
83 $result = @\file_get_contents(self::URL, false, $context);
|
Chris@13
|
84
|
Chris@17
|
85 \restore_error_handler();
|
Chris@13
|
86
|
Chris@17
|
87 return \json_decode($result);
|
Chris@13
|
88 }
|
Chris@13
|
89 }
|