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
 
16
class Installer
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $installLocation;
22
 
23
    /**
24
     * @var string
25
     */
26
    protected $tempDirectory;
27
 
28
    public function __construct(string $tempDirectory = null)
29
    {
30
        $this->tempDirectory = $tempDirectory ?: \sys_get_temp_dir();
31
        $this->installLocation = \Phar::running(false);
32
    }
33
 
34
    /**
35
     * Public to allow the Downloader to use the temporary directory if it's been set.
36
     *
37
     * @return string
38
     */
39
    public function getTempDirectory(): string
40
    {
41
        return $this->tempDirectory;
42
    }
43
 
44
    /**
45
     * Verify the currently installed PsySH phar is writable so it can be replaced.
46
     *
47
     * @return bool
48
     */
49
    public function isInstallLocationWritable(): bool
50
    {
51
        return \is_writable($this->installLocation);
52
    }
53
 
54
    /**
55
     * Verify the temporary directory is writable so downloads and backups can be saved there.
56
     *
57
     * @return bool
58
     */
59
    public function isTempDirectoryWritable(): bool
60
    {
61
        return \is_writable($this->tempDirectory);
62
    }
63
 
64
    /**
65
     * Verifies the downloaded archive can be extracted with \PharData.
66
     *
67
     * @param string $sourceArchive
68
     *
69
     * @return bool
70
     */
71
    public function isValidSource(string $sourceArchive): bool
72
    {
73
        if (!\class_exists('\PharData')) {
74
            return false;
75
        }
76
        $pharArchive = new \PharData($sourceArchive);
77
 
78
        return $pharArchive->valid();
79
    }
80
 
81
    /**
82
     * Extract the "psysh" phar from the archive and move it, replacing the currently installed phar.
83
     *
84
     * @param string $sourceArchive
85
     *
86
     * @return bool
87
     */
88
    public function install(string $sourceArchive): bool
89
    {
90
        $pharArchive = new \PharData($sourceArchive);
91
        $outputDirectory = \tempnam($this->tempDirectory, 'psysh-');
92
 
93
        // remove the temp file, and replace it with a sub-directory
94
        if (!\unlink($outputDirectory) || !\mkdir($outputDirectory, 0700)) {
95
            return false;
96
        }
97
 
98
        $pharArchive->extractTo($outputDirectory, ['psysh'], true);
99
 
100
        $renamed = \rename($outputDirectory.'/psysh', $this->installLocation);
101
 
102
        // Remove the sub-directory created to extract the psysh binary/phar
103
        \rmdir($outputDirectory);
104
 
105
        return $renamed;
106
    }
107
 
108
    /**
109
     * Create a backup of the currently installed PsySH phar in the temporary directory with a version number postfix.
110
     *
111
     * @param string $version
112
     *
113
     * @return bool
114
     */
115
    public function createBackup(string $version): bool
116
    {
117
        $backupFilename = $this->getBackupFilename($version);
118
 
119
        if (\file_exists($backupFilename) && !\is_writable($backupFilename)) {
120
            return false;
121
        }
122
 
123
        return \rename($this->installLocation, $backupFilename);
124
    }
125
 
126
    /**
127
     * Restore the backup file to the original PsySH install location.
128
     *
129
     * @param string $version
130
     *
131
     * @throws ErrorException If the backup file could not be found
132
     *
133
     * @return bool
134
     */
135
    public function restoreFromBackup(string $version): bool
136
    {
137
        $backupFilename = $this->getBackupFilename($version);
138
 
139
        if (!\file_exists($backupFilename)) {
140
            throw new ErrorException("Cannot restore from backup. File not found! [{$backupFilename}]");
141
        }
142
 
143
        return \rename($backupFilename, $this->installLocation);
144
    }
145
 
146
    /**
147
     * Get the full path for the backup target file location.
148
     *
149
     * @param string $version
150
     *
151
     * @return string
152
     */
153
    public function getBackupFilename(string $version): string
154
    {
155
        $installFilename = \basename($this->installLocation);
156
 
157
        return \sprintf('%s/%s.%s', $this->tempDirectory, $installFilename, $version);
158
    }
159
}