annotate vendor/psy/psysh/src/VersionUpdater/GitHubChecker.php @ 4:a9cd425dd02b

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