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