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\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
     * @throws ErrorException if a downloader cannot be created for the php environment
61
     */
62
    private function getDownloader(): Downloader
63
    {
64
        if (!isset($this->downloader)) {
65
            return Downloader\Factory::getDownloader();
66
        }
67
 
68
        return $this->downloader;
69
    }
70
 
71
    /**
72
     * Build the download URL for the latest release.
73
     *
74
     * The file name used in the URL will include the flavour postfix extracted from the current version
75
     * if it's present
76
     *
77
     * @param string $latestVersion
78
     */
79
    private function getAssetUrl(string $latestVersion): string
80
    {
81
        $versionPostfix = '';
82
        if (\strpos(Shell::VERSION, '+')) {
83
            $versionPostfix = '-'.\substr(Shell::VERSION, \strpos(Shell::VERSION, '+') + 1);
84
        }
85
        $downloadFilename = \sprintf('psysh-%s%s.tar.gz', $latestVersion, $versionPostfix);
86
 
87
        // check if latest release data contains an asset matching the filename?
88
 
89
        return \sprintf('%s/%s/%s', self::URL_PREFIX, $latestVersion, $downloadFilename);
90
    }
91
 
92
    /**
93
     * Execute the self-update process.
94
     *
95
     * @param InputInterface  $input
96
     * @param OutputInterface $output
97
     *
98
     * @throws ErrorException if the current version is not restored when installation fails
99
     */
100
    public function run(InputInterface $input, OutputInterface $output): int
101
    {
102
        $currentVersion = Shell::VERSION;
103
 
104
        // already have the latest version?
105
        if ($this->checker->isLatest()) {
106
            // current version is latest version...
107
            $output->writeln('<info>Current version is up-to-date.</info>');
108
 
109
            return self::SUCCESS;
110
        }
111
 
112
        // can overwrite current version?
113
        if (!$this->installer->isInstallLocationWritable()) {
114
            $output->writeln('<error>Installed version is not writable.</error>');
115
 
116
            return self::FAILURE;
117
        }
118
        // can download to, and create a backup in the temp directory?
119
        if (!$this->installer->isTempDirectoryWritable()) {
120
            $output->writeln('<error>Temporary directory is not writable.</error>');
121
 
122
            return self::FAILURE;
123
        }
124
 
125
        $latestVersion = $this->checker->getLatest();
126
        $downloadUrl = $this->getAssetUrl($latestVersion);
127
 
128
        $output->write("Downloading PsySH $latestVersion ...");
129
 
130
        try {
131
            $downloader = $this->getDownloader();
132
            $downloader->setTempDir($this->installer->getTempDirectory());
133
            $downloaded = $downloader->download($downloadUrl);
134
        } catch (ErrorException $e) {
135
            $output->write(' <error>Failed.</error>');
136
            $output->writeln(\sprintf('<error>%s</error>', $e->getMessage()));
137
 
138
            return self::FAILURE;
139
        }
140
 
141
        if (!$downloaded) {
142
            $output->writeln('<error>Download failed.</error>');
143
            $downloader->cleanup();
144
 
145
            return self::FAILURE;
146
        } else {
147
            $output->write(' <info>OK</info>'.\PHP_EOL);
148
        }
149
 
150
        $downloadedFile = $downloader->getFilename();
151
 
152
        if (!$this->installer->isValidSource($downloadedFile)) {
153
            $downloader->cleanup();
154
            $output->writeln('<error>Downloaded file is not a valid archive.</error>');
155
 
156
            return self::FAILURE;
157
        }
158
 
159
        // create backup as bin.old-version in the temporary directory
160
        $backupCreated = $this->installer->createBackup($currentVersion);
161
        if (!$backupCreated) {
162
            $downloader->cleanup();
163
            $output->writeln('<error>Failed to create a backup of the current version.</error>');
164
 
165
            return self::FAILURE;
166
        } elseif ($input->getOption('verbose')) {
167
            $backupFilename = $this->installer->getBackupFilename($currentVersion);
168
            $output->writeln('Created backup of current version: '.$backupFilename);
169
        }
170
 
171
        if (!$this->installer->install($downloadedFile)) {
172
            $this->installer->restoreFromBackup($currentVersion);
173
            $downloader->cleanup();
174
            $output->writeln("<error>Failed to install new PsySH version $latestVersion.</error>");
175
 
176
            return self::FAILURE;
177
        }
178
 
179
        // Remove the downloaded archive file from the temporary directory
180
        $downloader->cleanup();
181
 
182
        $output->writeln("Updated PsySH from $currentVersion to <info>$latestVersion</info>");
183
 
184
        return self::SUCCESS;
185
    }
186
}