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