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\Exception\ErrorException;
15
use Psy\Shell;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
 
19
/**
20
 * Self update command.
21
 *
22
 * If a new version is available, this command will download it and replace the currently installed version
23
 */
24
class SelfUpdate
25
{
26
    const URL_PREFIX = 'https://github.com/bobthecow/psysh/releases/download';
27
    const SUCCESS = 0;
28
    const FAILURE = 1;
29
 
30
    /** @var Checker */
31
    private $checker;
32
 
33
    /** @var Installer */
34
    private $installer;
35
 
36
    /** @var Downloader */
37
    private $downloader;
38
 
39
    public function __construct(Checker $checker, Installer $installer)
40
    {
41
        $this->checker = $checker;
42
        $this->installer = $installer;
43
    }
44
 
45
    /**
46
     * Allow the downloader to be injected for testing.
47
     *
48
     * @param Downloader $downloader
49
     *
50
     * @return void
51
     */
52
    public function setDownloader(Downloader $downloader)
53
    {
54
        $this->downloader = $downloader;
55
    }
56
 
57
    /**
58
     * Get the currently set Downloader or create one based on the capabilities of the php environment.
59
     *
60
     * @return Downloader
61
     *
62
     * @throws ErrorException if a downloader cannot be created for the php environment
63
     */
64
    private function getDownloader(): Downloader
65
    {
66
        if (!isset($this->downloader)) {
67
            return Downloader\Factory::getDownloader();
68
        }
69
 
70
        return $this->downloader;
71
    }
72
 
73
    /**
74
     * Build the download URL for the latest release.
75
     *
76
     * The file name used in the URL will include the flavour postfix extracted from the current version
77
     * if it's present
78
     *
79
     * @param string $latestVersion
80
     *
81
     * @return string
82
     */
83
    private function getAssetUrl(string $latestVersion): string
84
    {
85
        $versionPostfix = '';
86
        if (\strpos(Shell::VERSION, '+')) {
87
            $versionPostfix = '-'.\substr(Shell::VERSION, \strpos(Shell::VERSION, '+') + 1);
88
        }
89
        $downloadFilename = \sprintf('psysh-%s%s.tar.gz', $latestVersion, $versionPostfix);
90
 
91
        // check if latest release data contains an asset matching the filename?
92
 
93
        return \sprintf('%s/%s/%s', self::URL_PREFIX, $latestVersion, $downloadFilename);
94
    }
95
 
96
    /**
97
     * Execute the self-update process.
98
     *
99
     * @param InputInterface  $input
100
     * @param OutputInterface $output
101
     *
102
     * @throws ErrorException if the current version is not restored when installation fails
103
     *
104
     * @return int
105
     */
106
    public function run(InputInterface $input, OutputInterface $output): int
107
    {
108
        $currentVersion = Shell::VERSION;
109
 
110
        // already have the latest version?
111
        if ($this->checker->isLatest()) {
112
            // current version is latest version...
113
            $output->writeln('<info>Current version is up-to-date.</info>');
114
 
115
            return self::SUCCESS;
116
        }
117
 
118
        // can overwrite current version?
119
        if (!$this->installer->isInstallLocationWritable()) {
120
            $output->writeln('<error>Installed version is not writable.</error>');
121
 
122
            return self::FAILURE;
123
        }
124
        // can download to, and create a backup in the temp directory?
125
        if (!$this->installer->isTempDirectoryWritable()) {
126
            $output->writeln('<error>Temporary directory is not writable.</error>');
127
 
128
            return self::FAILURE;
129
        }
130
 
131
        $latestVersion = $this->checker->getLatest();
132
        $downloadUrl = $this->getAssetUrl($latestVersion);
133
 
134
        $output->write("Downloading PsySH $latestVersion ...");
135
 
136
        try {
137
            $downloader = $this->getDownloader();
138
            $downloader->setTempDir($this->installer->getTempDirectory());
139
            $downloaded = $downloader->download($downloadUrl);
140
        } catch (ErrorException $e) {
141
            $output->write(' <error>Failed.</error>');
142
            $output->writeln(\sprintf('<error>%s</error>', $e->getMessage()));
143
 
144
            return self::FAILURE;
145
        }
146
 
147
        if (!$downloaded) {
148
            $output->writeln('<error>Download failed.</error>');
149
            $downloader->cleanup();
150
 
151
            return self::FAILURE;
152
        } else {
153
            $output->write(' <info>OK</info>'.\PHP_EOL);
154
        }
155
 
156
        $downloadedFile = $downloader->getFilename();
157
 
158
        if (!$this->installer->isValidSource($downloadedFile)) {
159
            $downloader->cleanup();
160
            $output->writeln('<error>Downloaded file is not a valid archive.</error>');
161
 
162
            return self::FAILURE;
163
        }
164
 
165
        // create backup as bin.old-version in the temporary directory
166
        $backupCreated = $this->installer->createBackup($currentVersion);
167
        if (!$backupCreated) {
168
            $downloader->cleanup();
169
            $output->writeln('<error>Failed to create a backup of the current version.</error>');
170
 
171
            return self::FAILURE;
172
        } elseif ($input->getOption('verbose')) {
173
            $backupFilename = $this->installer->getBackupFilename($currentVersion);
174
            $output->writeln('Created backup of current version: '.$backupFilename);
175
        }
176
 
177
        if (!$this->installer->install($downloadedFile)) {
178
            $this->installer->restoreFromBackup($currentVersion);
179
            $downloader->cleanup();
180
            $output->writeln("<error>Failed to install new PsySH version $latestVersion.</error>");
181
 
182
            return self::FAILURE;
183
        }
184
 
185
        // Remove the downloaded archive file from the temporary directory
186
        $downloader->cleanup();
187
 
188
        $output->writeln("Updated PsySH from $currentVersion to <info>$latestVersion</info>");
189
 
190
        return self::SUCCESS;
191
    }
192
}