Subversion-Projekte lars-tiefland.laravel_shop

Revision

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