@@ -25,6 +25,7 @@ final class Updater
25
25
private $ log = [];
26
26
private $ status ;
27
27
private $ clear ;
28
+ private $ archive_relative_paths ;
28
29
29
30
/**
30
31
* Constructs a new instance of the class and starts the update process for the provided version.
@@ -197,6 +198,7 @@ private function Unlock()
197
198
{
198
199
if (!$ this ->CleanUp ()) {
199
200
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup process failed. " ];
201
+ $ this ->status = $ this ::ERROR ;
200
202
}
201
203
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Releasing update lock. " ];
202
204
if (!file_exists ($ this ->dir . '/update.lock ' )) {
@@ -360,7 +362,7 @@ private function Extract()
360
362
if ($ zip ->extractTo ($ extract_path )) {
361
363
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Extraction completed. $ extract_path " ];
362
364
$ zip ->close ();
363
- rename (glob ($ extract_path . '/* ' )[0 ], $ extract_path . '/tmp_ ' . $ this ->repository );
365
+ rename (glob ($ extract_path . '/* ' )[0 ], $ extract_path . '/ ' . $ this ->repository );
364
366
return true ;
365
367
} else {
366
368
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Extraction failed. $ extract_path " ];
@@ -403,15 +405,17 @@ private function Upgrade()
403
405
$ release_exclude ['filename ' ] = array_unique ($ release_exclude ['filename ' ]);
404
406
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Upgrade exclude: \n" . json_encode ($ release_exclude , JSON_PRETTY_PRINT )];
405
407
406
- $ release_paths = $ this ->MapPath ($ this ->dir . "/update/extract/tmp_ {$ this ->repository }" , $ release_exclude );
408
+ $ release_paths = $ this ->MapPath ($ this ->dir . "/update/extract/ {$ this ->repository }" , $ release_exclude );
407
409
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Upgrade lists: \n" . json_encode ($ release_paths , JSON_PRETTY_PRINT )];
408
410
409
411
$ release_relative_paths = array_map (function ($ release_path ) {
410
- return substr_replace ($ release_path , '' , 0 , strlen ($ this ->dir . "/update/extract/tmp_ {$ this ->repository }" ));
412
+ return substr_replace ($ release_path , '' , 0 , strlen ($ this ->dir . "/update/extract/ {$ this ->repository }" ));
411
413
}, $ release_paths );
412
414
415
+ $ this ->archive_relative_paths = $ release_relative_paths ;
416
+
413
417
foreach ($ release_relative_paths as $ release_relative_path ) {
414
- $ release_path = $ this ->dir . "/update/extract/tmp_ {$ this ->repository }$ release_relative_path " ;
418
+ $ release_path = $ this ->dir . "/update/extract/ {$ this ->repository }$ release_relative_path " ;
415
419
$ source_path = $ this ->dir . "$ release_relative_path " ;
416
420
if (is_dir ($ release_path )) {
417
421
if (!is_dir ($ source_path )) {
@@ -474,18 +478,59 @@ private function Upgrade()
474
478
475
479
private function CleanUp ()
476
480
{
481
+ $ cleaned = true ;
482
+ if (file_exists ($ this ->dir . "/update/ " . $ this ->repository . ".zip " ) && !$ this ->Delete ($ this ->dir . "/update/ " . $ this ->repository . ".zip " )) {
483
+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to delete downloaded zip file. " . $ this ->dir . "/update/ " . $ this ->repository . ".zip " ];
484
+ $ cleaned = false ;
485
+ };
486
+ if (!$ this ->clear && $ this ->status !== $ this ::ERROR ) {
487
+ $ archived = true ;
488
+ $ zip = new ZipArchive ;
489
+ $ zip_file = $ this ->dir . "/update/ {$ this ->repository }.zip " ;
490
+ if ($ zip ->open ($ zip_file , ZipArchive::CREATE ) !== true ) {
491
+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to create archive file. $ zip_file " ];
492
+ $ cleaned = false ;
493
+ $ archived = false ;
494
+ }
495
+ if ($ zip ->addEmptyDir ($ this ->repository ) === false ) {
496
+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to create directory in archive file. {$ this ->repository }" ];
497
+ $ cleaned = false ;
498
+ $ archived = false ;
499
+ }
500
+ foreach ($ this ->archive_relative_paths as $ archive_relative_path ) {
501
+ $ release_path = $ this ->dir . "/update/extract/ {$ this ->repository }$ archive_relative_path " ;
502
+ if (is_dir ($ release_path )) {
503
+ if ($ zip ->addEmptyDir ($ this ->repository . $ archive_relative_path ) === false ) {
504
+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to create directory in archive file. " . $ this ->repository . $ archive_relative_path ];
505
+ $ cleaned = false ;
506
+ $ archived = false ;
507
+ }
508
+ } elseif (is_file ($ release_path )) {
509
+ if ($ zip ->addFile ($ release_path , $ this ->repository . $ archive_relative_path ) === false ) {
510
+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to add file to archive file. " . $ this ->repository . $ archive_relative_path ];
511
+ $ cleaned = false ;
512
+ $ archived = false ;
513
+ };
514
+ }
515
+ }
516
+ $ zip ->close ();
517
+ if (!$ archived ) {
518
+ if (file_exists ($ zip_file ) && !$ this ->Delete ($ zip_file )) {
519
+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to delete archive file. $ zip_file " ];
520
+ $ cleaned = false ;
521
+ };
522
+ }
523
+ }
477
524
if (file_exists ($ this ->dir . "/update/extract " ) && !$ this ->Delete ($ this ->dir . "/update/extract " )) {
478
- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup failed . " . $ this ->dir . "/update/extract " ];
479
- return false ;
525
+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to delete extracted folder . " . $ this ->dir . "/update/extract " ];
526
+ $ cleaned = false ;
480
527
};
481
- if ($ this ->clear ) {
482
- if (file_exists ($ this ->dir . "/update/ " . $ this ->repository . ".zip " ) && !$ this ->Delete ($ this ->dir . "/update/ " . $ this ->repository . ".zip " )) {
483
- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup failed. " . $ this ->dir . "/update/ " . $ this ->repository . ".zip " ];
484
- return false ;
485
- };
528
+ if ($ cleaned ) {
529
+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup completed. " ];
530
+ } else {
531
+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup completed with errors. " ];
486
532
}
487
- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup completed. " ];
488
- return true ;
533
+ return $ cleaned ;
489
534
}
490
535
491
536
private function Install ()
@@ -494,51 +539,60 @@ private function Install()
494
539
if (!$ this ->Lock ()) {
495
540
if (file_exists ($ this ->dir . "/update.lock " )) {
496
541
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Update already running. Update terminated. " ];
497
- return $ this ::BUSY ;
542
+ $ this ->status = $ this ::BUSY ;
543
+ return $ this ->status ;
498
544
}
499
545
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Update lock aquiring failed. Update terminated. " ];
546
+ $ this ->status = $ this ::ERROR ;
500
547
$ this ->Unlock ();
501
- return $ this :: ERROR ;
548
+ return $ this -> status ;
502
549
}
503
550
if (!$ this ->Folder ()) {
504
551
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Folder creation process failed. Update terminated. " ];
552
+ $ this ->status = $ this ::ERROR ;
505
553
$ this ->Unlock ();
506
- return $ this :: ERROR ;
554
+ return $ this -> status ;
507
555
}
508
556
if (!$ this ->Version ()) {
509
557
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Version check process failed. Update terminated. " ];
558
+ $ this ->status = $ this ::ERROR ;
510
559
$ this ->Unlock ();
511
- return $ this :: ERROR ;
560
+ return $ this -> status ;
512
561
}
513
562
if (!version_compare ($ this ->release , $ this ->version , '> ' )) {
514
563
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Version already up to date (Release {$ this ->release }). Update terminated. " ];
564
+ $ this ->status = $ this ::LATEST ;
515
565
$ this ->Unlock ();
516
- return $ this :: LATEST ;
566
+ return $ this -> status ;
517
567
}
518
568
$ download_try = 0 ;
519
569
while (!$ this ->Download ($ this ->zip_url )) {
520
570
$ download_try ++;
521
571
if ($ download_try > 3 ) {
522
572
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Download process failed. Update terminated. " ];
573
+ $ this ->status = $ this ::ERROR ;
523
574
$ this ->Unlock ();
524
- return $ this :: ERROR ;
575
+ return $ this -> status ;
525
576
}
526
577
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Unable to retrieve download. Retry in 5 seconds. " ];
527
578
sleep (5 );
528
579
}
529
580
if (!$ this ->Extract ()) {
530
581
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Extraction process failed. Update terminated. " ];
582
+ $ this ->status = $ this ::ERROR ;
531
583
$ this ->Unlock ();
532
- return $ this :: ERROR ;
584
+ return $ this -> status ;
533
585
}
534
586
if (!$ this ->Upgrade ()) {
535
587
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Upgrade process failed. Update terminated. " ];
588
+ $ this ->status = $ this ::ERROR ;
536
589
$ this ->Unlock ();
537
- return $ this :: ERROR ;
590
+ return $ this -> status ;
538
591
}
539
592
$ this ->log [] = [date ("Y-m-d H:i:s " ), "Update completed. " ];
593
+ $ this ->status = $ this ::UPDATED ;
540
594
$ this ->Unlock ();
541
- return $ this :: UPDATED ;
595
+ return $ this -> status ;
542
596
}
543
597
544
598
private function MapPath ($ path , $ exclude = ['path ' => [], 'filename ' => []], &$ list = [])
0 commit comments