comparison vendor/psy/psysh/src/VersionUpdater/GitHubChecker.php @ 0:c75dbcec494b

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