Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
6
 * (c) 2012-2022 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(): bool
26
    {
27
        // version_compare doesn't handle semver completely;
28
        // strip pre-release and build metadata before comparing
29
        $version = \preg_replace('/[+-]\w+/', '', Shell::VERSION);
30
 
31
        return \version_compare($version, $this->getLatest(), '>=');
32
    }
33
 
34
    /**
35
     * @return string
36
     */
37
    public function getLatest(): string
38
    {
39
        if (!isset($this->latest)) {
40
            $this->setLatest($this->getVersionFromTag());
41
        }
42
 
43
        return $this->latest;
44
    }
45
 
46
    /**
47
     * @param string $version
48
     */
49
    public function setLatest(string $version)
50
    {
51
        $this->latest = $version;
52
    }
53
 
54
    /**
55
     * @return string|null
56
     */
57
    private function getVersionFromTag()
58
    {
59
        $contents = $this->fetchLatestRelease();
60
        if (!$contents || !isset($contents->tag_name)) {
61
            throw new \InvalidArgumentException('Unable to check for updates');
62
        }
63
        $this->setLatest($contents->tag_name);
64
 
65
        return $this->getLatest();
66
    }
67
 
68
    /**
69
     * Set to public to make testing easier.
70
     *
71
     * @return mixed
72
     */
73
    public function fetchLatestRelease()
74
    {
75
        $context = \stream_context_create([
76
            'http' => [
77
                'user_agent' => 'PsySH/'.Shell::VERSION,
78
                'timeout'    => 1.0,
79
            ],
80
        ]);
81
 
82
        \set_error_handler(function () {
83
            // Just ignore all errors with this. The checker will throw an exception
84
            // if it doesn't work :)
85
        });
86
 
87
        $result = @\file_get_contents(self::URL, false, $context);
88
 
89
        \restore_error_handler();
90
 
91
        return \json_decode($result);
92
    }
93
}