16
16
17
17
namespace phpMyFAQ \Setup ;
18
18
19
+ use JsonException ;
19
20
use Monolog \Level ;
20
21
use phpMyFAQ \Configuration ;
21
22
use phpMyFAQ \Enums \DownloadHostType ;
31
32
32
33
class Upgrade extends Setup
33
34
{
35
+ public const GITHUB_PATH = 'thorsten/phpMyFAQ/releases/download/development-nightly-%s/ ' ;
36
+ private const GITHUB_FILENAME = 'phpMyFAQ-nightly-%s.zip ' ;
37
+ private const PHPMYFAQ_FILENAME = 'phpMyFAQ-%s.zip ' ;
38
+
39
+ private $ isNightly = false ;
40
+
34
41
public function __construct (protected System $ system , private readonly Configuration $ configuration )
35
42
{
36
43
parent ::__construct ($ this ->system );
@@ -49,14 +56,14 @@ public function checkFilesystem(): bool
49
56
}
50
57
}
51
58
if (
52
- is_dir (PMF_CONTENT_DIR . '\user\attachments ' ) && is_dir (PMF_CONTENT_DIR . '\user\images ' ) && is_dir (
53
- PMF_CONTENT_DIR . '\core\data '
54
- ) && is_dir (PMF_ROOT_DIR . '\assets\themes ' )
59
+ is_dir (PMF_CONTENT_DIR . '\user\attachments ' ) &&
60
+ is_dir (PMF_CONTENT_DIR . '\user\images ' ) &&
61
+ is_dir (PMF_CONTENT_DIR . '\core\data ' ) &&
62
+ is_dir (PMF_ROOT_DIR . '\assets\themes ' )
55
63
) {
56
64
if (
57
- is_file (PMF_CONTENT_DIR . '\core\config\constants.php ' ) && is_file (
58
- PMF_CONTENT_DIR . '\core\config\database.php '
59
- )
65
+ is_file (PMF_CONTENT_DIR . '\core\config\constants.php ' ) &&
66
+ is_file (PMF_CONTENT_DIR . '\core\config\database.php ' )
60
67
) {
61
68
if ($ this ->configuration ->isElasticsearchActive ()) {
62
69
if (!is_file (PMF_CONTENT_DIR . '\core\config\elasticsearch.php ' )) {
@@ -92,8 +99,7 @@ public function checkFilesystem(): bool
92
99
*/
93
100
public function downloadPackage (string $ version ): string |bool
94
101
{
95
- $ zipFile = 'phpMyFAQ- ' . $ version . '.zip ' ;
96
- $ url = DownloadHostType::PHPMYFAQ ->value . $ zipFile ;
102
+ $ url = $ this ->getDownloadHost () . $ this ->getPath () . $ this ->getFilename ($ version );
97
103
98
104
$ client = HttpClient::create ();
99
105
@@ -106,9 +112,9 @@ public function downloadPackage(string $version): string|bool
106
112
107
113
$ package = $ response ->getContent ();
108
114
109
- file_put_contents (PMF_CONTENT_DIR . '/upgrades/ ' . $ zipFile , $ package );
115
+ file_put_contents (PMF_CONTENT_DIR . '/upgrades/ ' . $ this -> getFilename ( $ version ) , $ package );
110
116
111
- return PMF_CONTENT_DIR . '/upgrades/ ' . $ zipFile ;
117
+ return PMF_CONTENT_DIR . '/upgrades/ ' . $ this -> getFilename ( $ version ) ;
112
118
} catch (
113
119
TransportExceptionInterface |
114
120
ClientExceptionInterface |
@@ -124,10 +130,10 @@ public function downloadPackage(string $version): string|bool
124
130
/**
125
131
* Method to verify the downloaded phpMyFAQ package
126
132
*
127
- * @return bool
128
- * @throws TransportExceptionInterface|ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|JsonException
129
133
* @param string $path | Path to zip file
130
134
* @param string $version | Version to verify
135
+ * @return bool
136
+ * @throws TransportExceptionInterface|ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|JsonException
131
137
*/
132
138
public function verifyPackage (string $ path , string $ version ): bool
133
139
{
@@ -161,7 +167,6 @@ public function verifyPackage(string $path, string $version): bool
161
167
*
162
168
* @return bool
163
169
* @param string $path | Path of the package
164
- * @throws ZipException
165
170
*/
166
171
public function unpackPackage (string $ path ): bool
167
172
{
@@ -187,7 +192,6 @@ public function unpackPackage(string $path): bool
187
192
*
188
193
* @param string $backupName | Name of the created backup
189
194
* @return bool
190
- * @throws ZipException
191
195
*/
192
196
public function createTemporaryBackup (string $ backupName ): bool
193
197
{
@@ -240,4 +244,60 @@ public function restoreTemporaryBackup()
240
244
public function installPackage ()
241
245
{
242
246
}
247
+
248
+ /**
249
+ * Returns the host for download packages, so either github.com or download.phpmyfaq.de
250
+ * @return string
251
+ */
252
+ public function getDownloadHost (): string
253
+ {
254
+ if ($ this ->isNightly ()) {
255
+ return DownloadHostType::GITHUB ->value ;
256
+ }
257
+
258
+ return DownloadHostType::PHPMYFAQ ->value ;
259
+ }
260
+
261
+ /**
262
+ * Returns the path to the download package, it's an empty string for development and production releases
263
+ * @return string
264
+ */
265
+ public function getPath (): string
266
+ {
267
+ if ($ this ->isNightly ()) {
268
+ return sprintf (self ::GITHUB_PATH , date ('Y-m-d ' , strtotime ('-1 days ' )));
269
+ }
270
+
271
+ return '' ;
272
+ }
273
+
274
+ /**
275
+ * Returns the filename of the download package
276
+ * @param string $version
277
+ * @return string
278
+ */
279
+ public function getFilename (string $ version ): string
280
+ {
281
+ if ($ this ->isNightly ()) {
282
+ return sprintf (self ::GITHUB_FILENAME , date ('Y-m-d ' , strtotime ('-1 days ' )));
283
+ }
284
+
285
+ return sprintf (self ::PHPMYFAQ_FILENAME , $ version );
286
+ }
287
+
288
+ /**
289
+ * @return bool
290
+ */
291
+ public function isNightly (): bool
292
+ {
293
+ return $ this ->isNightly ;
294
+ }
295
+
296
+ /**
297
+ * @param bool $isNightly
298
+ */
299
+ public function setIsNightly (bool $ isNightly ): void
300
+ {
301
+ $ this ->isNightly = $ isNightly ;
302
+ }
243
303
}
0 commit comments