4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\VersionUpdater;
16 class GitHubChecker implements Checker
18 const URL = 'https://api.github.com/repos/bobthecow/psysh/releases/latest';
25 public function isLatest()
27 return version_compare(Shell::VERSION, $this->getLatest(), '>=');
33 public function getLatest()
35 if (!isset($this->latest)) {
36 $this->setLatest($this->getVersionFromTag());
43 * @param string $version
45 public function setLatest($version)
47 $this->latest = $version;
53 private function getVersionFromTag()
55 $contents = $this->fetchLatestRelease();
56 if (!$contents || !isset($contents->tag_name)) {
57 throw new \InvalidArgumentException('Unable to check for updates');
59 $this->setLatest($contents->tag_name);
61 return $this->getLatest();
65 * Set to public to make testing easier.
69 public function fetchLatestRelease()
71 $context = stream_context_create(array('http' => array('user_agent' => 'PsySH/' . Shell::VERSION)));
73 return json_decode(@file_get_contents(self::URL, false, $context));