22
33namespace KoderZi \PhpGitHubUpdater ;
44
5+ use ZipArchive ;
6+
57final class Updater
68{
79 private $ username ;
@@ -12,6 +14,7 @@ final class Updater
1214 private $ zip_url ;
1315 private $ admin ;
1416 private $ mailer ;
17+ private $ dir ;
1518 private $ log = [];
1619
1720 /**
@@ -25,27 +28,31 @@ final class Updater
2528 * @param string $mailer The email address that the email will be sent from.
2629 * @return void
2730 */
28- public function __construct (string $ username ,string $ repository ,string $ token ,string $ version ,string $ admin ,string $ mailer )
31+ public function __construct (string $ username , string $ repository , string $ token , string $ version , string $ admin , string $ mailer )
2932 {
3033 $ this ->username = $ username ;
3134 $ this ->repository = $ repository ;
3235 $ this ->token = $ token ;
3336 $ this ->version = $ version ;
3437 $ this ->admin = $ admin ;
3538 $ this ->mailer = $ mailer ;
39+ $ this ->dir = getcwd ();
3640
3741 if (!$ this ->Update ()) {
3842 $ this ->Mail ();
3943 }
4044 $ this ->Log ();
45+ if (class_exists ('Composer\Autoload\ClassLoader ' )) {
46+ exec ('composer install -d ' . getcwd ());
47+ }
4148 }
4249
4350 private function Log ()
4451 {
4552 $ log = implode ("\n" , array_map (function ($ entry ) {
4653 return "{$ entry [0 ]}: {$ entry [1 ]}" ;
4754 }, $ this ->log ));
48- file_put_contents (__DIR__ . "/update/log/ " . date ("Y-m-d H:i:s " ) . ".txt " , $ log );
55+ file_put_contents ($ this -> dir . "/update/log/ " . date ("Y-m-d H:i:s " ) . ".txt " , $ log );
4956 }
5057
5158 private function Mail ()
@@ -105,7 +112,7 @@ private function Lock()
105112 return false ;
106113 }
107114 for ($ i = 0 ; $ i < 3 ; $ i ++) {
108- if (!file_exists (__DIR__ . '/update.lock ' ) && file_put_contents (__DIR__ . '/update.lock ' , '' ) !== false ) {
115+ if (!file_exists ($ this -> dir . '/update.lock ' ) && file_put_contents ($ this -> dir . '/update.lock ' , '' ) !== false ) {
109116 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Update lock acquired. " ];
110117 return true ;
111118 }
@@ -121,10 +128,10 @@ private function Unlock()
121128 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup process failed. " ];
122129 }
123130 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Releasing update lock. " ];
124- if (!file_exists (__DIR__ . '/update.lock ' )) {
131+ if (!file_exists ($ this -> dir . '/update.lock ' )) {
125132 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Update lock unavailable. " ];
126133 }
127- if ($ this ->Delete (__DIR__ . '/update.lock ' )) {
134+ if ($ this ->Delete ($ this -> dir . '/update.lock ' )) {
128135 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Update lock released. " ];
129136 }
130137 return ;
@@ -147,9 +154,9 @@ private function Folder()
147154 private function CreateFolder (string $ FolderName , string $ FolderPath = '' )
148155 {
149156 if ($ FolderPath == '' ) {
150- $ download_path = __DIR__ . '/ ' . $ FolderName ;
157+ $ download_path = $ this -> dir . '/ ' . $ FolderName ;
151158 } else {
152- $ download_path = __DIR__ . '/ ' . trim ($ FolderPath , '/ ' ) . '/ ' . $ FolderName ;
159+ $ download_path = $ this -> dir . '/ ' . trim ($ FolderPath , '/ ' ) . '/ ' . $ FolderName ;
153160 }
154161 if (true !== is_dir ($ download_path )) {
155162 $ FolderName = ucfirst ($ FolderName );
@@ -222,7 +229,7 @@ private function Download($url = null)
222229 return false ;
223230 }
224231 } else {
225- $ download_file = __DIR__ . "/update/update.zip " ;
232+ $ download_file = $ this -> dir . "/update/update.zip " ;
226233 if (file_exists ($ download_file )) {
227234 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Deleting existing zip file. $ download_file " ];
228235 if (!$ this ->Delete ($ download_file )) {
@@ -267,8 +274,8 @@ private function Download($url = null)
267274
268275 private function Extract ()
269276 {
270- $ download_file = __DIR__ . "/update/update.zip " ;
271- $ extract_path = __DIR__ . "/update/extract " ;
277+ $ download_file = $ this -> dir . "/update/update.zip " ;
278+ $ extract_path = $ this -> dir . "/update/extract " ;
272279 if (file_exists ($ extract_path )) {
273280 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Deleting existing extract folder. $ extract_path " ];
274281 if (!$ this ->Delete ($ extract_path )) {
@@ -298,23 +305,23 @@ private function Extract()
298305 private function Upgrade ()
299306 {
300307 sleep (10 );
301- $ plugin_paths = $ this ->MapPath (__DIR__ , ['path ' => [__DIR__ . '/.git ' , __DIR__ . '/update ' , __DIR__ . '/update.lock ' ], 'filename ' => ['.gitignore ' ]]);
308+ $ plugin_paths = $ this ->MapPath ($ this -> dir , ['path ' => [$ this -> dir . '/.git ' , $ this -> dir . '/update ' , $ this -> dir . '/update.lock ' , $ this -> dir . ' /vendor ' ], 'filename ' => ['.gitignore ' ]]);
302309 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Plugin list: \n" . json_encode ($ plugin_paths , JSON_PRETTY_PRINT )];
303310
304311 $ plugin_relative_paths = array_map (function ($ plugin_path ) {
305- return substr_replace ($ plugin_path , '' , 0 , strlen (__DIR__ ));
312+ return substr_replace ($ plugin_path , '' , 0 , strlen ($ this -> dir ));
306313 }, $ plugin_paths );
307314
308- $ upgrade_paths = $ this ->MapPath (__DIR__ . "/update/extract/tmp_ {$ this ->repository }" , ['path ' => [__DIR__ . '/.git ' , __DIR__ . '/update.lock ' ], 'filename ' => ['.gitignore ' ]]);
315+ $ upgrade_paths = $ this ->MapPath ($ this -> dir . "/update/extract/tmp_ {$ this ->repository }" , ['path ' => [$ this -> dir . '/.git ' , $ this -> dir . '/update.lock ' , $ this -> dir . ' /vendor ' ], 'filename ' => ['.gitignore ' ]]);
309316 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Upgrade list: \n" . json_encode ($ upgrade_paths , JSON_PRETTY_PRINT )];
310317
311318 $ upgrade_relative_paths = array_map (function ($ upgrade_path ) {
312- return substr_replace ($ upgrade_path , '' , 0 , strlen (__DIR__ . "/update/extract/tmp_ {$ this ->repository }" ));
319+ return substr_replace ($ upgrade_path , '' , 0 , strlen ($ this -> dir . "/update/extract/tmp_ {$ this ->repository }" ));
313320 }, $ upgrade_paths );
314321
315322 foreach ($ upgrade_relative_paths as $ upgrade_relative_path ) {
316- $ upgrade_path = __DIR__ . "/update/extract/tmp_ {$ this ->repository }$ upgrade_relative_path " ;
317- $ plugin_path = __DIR__ . "$ upgrade_relative_path " ;
323+ $ upgrade_path = $ this -> dir . "/update/extract/tmp_ {$ this ->repository }$ upgrade_relative_path " ;
324+ $ plugin_path = $ this -> dir . "$ upgrade_relative_path " ;
318325 if (is_dir ($ upgrade_path )) {
319326 if (!is_dir ($ plugin_path )) {
320327 if (mkdir ($ plugin_path , 0700 , true )) {
@@ -349,7 +356,7 @@ private function Upgrade()
349356 $ delete_relative_paths = array_values (array_diff ($ plugin_relative_paths , $ upgrade_relative_paths ));
350357
351358 foreach ($ delete_relative_paths as $ delete_relative_path ) {
352- if (is_dir (__DIR__ . $ delete_relative_path )) {
359+ if (is_dir ($ this -> dir . $ delete_relative_path )) {
353360 foreach ($ delete_relative_paths as $ _delete_relative_key => $ _delete_relative_path ) {
354361 if ($ delete_relative_path != $ _delete_relative_path && substr ($ _delete_relative_path , 0 , strlen ($ delete_relative_path )) == $ delete_relative_path ) {
355362 unset($ delete_relative_paths [$ _delete_relative_key ]);
@@ -359,7 +366,7 @@ private function Upgrade()
359366 }
360367
361368 $ delete_paths = array_values (array_map (function ($ delete_relative_path ) {
362- return __DIR__ . $ delete_relative_path ;
369+ return $ this -> dir . $ delete_relative_path ;
363370 }, $ delete_relative_paths ));
364371 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Delete list: \n" . json_encode ($ delete_paths , JSON_PRETTY_PRINT )];
365372
@@ -376,12 +383,12 @@ private function Upgrade()
376383
377384 private function CleanUp ()
378385 {
379- if (file_exists (__DIR__ . "/update/extract " ) && !$ this ->Delete (__DIR__ . "/update/extract " )) {
380- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup failed. " . __DIR__ . "/update/extract " ];
386+ if (file_exists ($ this -> dir . "/update/extract " ) && !$ this ->Delete ($ this -> dir . "/update/extract " )) {
387+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup failed. " . $ this -> dir . "/update/extract " ];
381388 return false ;
382389 };
383- if (file_exists (__DIR__ . "/update/update.zip " ) && !$ this ->Delete (__DIR__ . "/update/update.zip " )) {
384- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup failed. " . __DIR__ . "/update/update.zip " ];
390+ if (file_exists ($ this -> dir . "/update/update.zip " ) && !$ this ->Delete ($ this -> dir . "/update/update.zip " )) {
391+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup failed. " . $ this -> dir . "/update/update.zip " ];
385392 return false ;
386393 };
387394 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup completed. " ];
@@ -392,7 +399,7 @@ private function Update()
392399 {
393400 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Update started. " ];
394401 if (!$ this ->Lock ()) {
395- if (file_exists (__DIR__ . "/update.lock " )) {
402+ if (file_exists ($ this -> dir . "/update.lock " )) {
396403 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Update already running. Update terminated. " ];
397404 return true ;
398405 }
0 commit comments