@@ -15,20 +15,24 @@ final class Updater
1515 private $ admin ;
1616 private $ mailer ;
1717 private $ dir ;
18+ private $ exclude = [];
1819 private $ log = [];
1920
2021 /**
21- * Constructs a new instance of the class and starts the update process for the provided version .
22+ * Constructor for the class. Initializes the object properties and installs composer .
2223 *
23- * @param string $username Your GitHub username.
24- * @param string $repository The name of your GitHub repository.
25- * @param string $token The personal access token you generated earlier.
26- * @param string $version The current version number of your project.
27- * @param string $admin The email address of the admin who will receive an email in case of update failure.
28- * @param string $mailer The email address that the email will be sent from.
24+ * @param string $username The username used for the API calls.
25+ * @param string $repository The repository used for the API calls.
26+ * @param string $token The API token used for authentication.
27+ * @param string $version The API version to use.
28+ * @param string $admin The email address of the administrator.
29+ * @param string $mailer The email address used for sending emails.
30+ * @param array $exclude An array of directories or files to exclude from the update.
31+ * The array must have the format ['path' => [], 'filename' => []]
32+ * @throws Some_Exception_Class If the update process fails.
2933 * @return void
3034 */
31- public function __construct (string $ username , string $ repository , string $ token , string $ version , string $ admin , string $ mailer )
35+ public function __construct (string $ username , string $ repository , string $ token , string $ version , string $ admin , string $ mailer, array $ exclude = [ ' path ' => [], ' filename ' => []] )
3236 {
3337 $ this ->username = $ username ;
3438 $ this ->repository = $ repository ;
@@ -38,6 +42,15 @@ public function __construct(string $username, string $repository, string $token,
3842 $ this ->mailer = $ mailer ;
3943 $ this ->dir = getcwd ();
4044
45+ if (!isset ($ exclude ['path ' ])) {
46+ $ exclude ['path ' ] = [];
47+ }
48+ if (!isset ($ exclude ['filename ' ])) {
49+ $ exclude ['filename ' ] = [];
50+ }
51+
52+ $ this ->exclude = $ exclude ;
53+
4154 if (!$ this ->Update ()) {
4255 $ this ->Mail ();
4356 }
@@ -57,7 +70,7 @@ private function Log()
5770
5871 private function Mail ()
5972 {
60- $ subject = "Plugin Update Failed: {$ this ->repository } - Action Required " ;
73+ $ subject = "Github Updater Failed: {$ this ->repository } - Action Required " ;
6174 $ headers = "From: < {$ this ->mailer }> \r\n" ;
6275 $ headers .= "MIME-Version: 1.0 \r\n" ;
6376 $ headers .= "Content-Type: text/html; charset=UTF-8 \r\n" ;
@@ -66,7 +79,7 @@ private function Mail()
6679 <html>
6780 <head>
6881 <meta charset="utf-8">
69- <title>Plugin Update Failed: [PLUGIN NAME] - Action Required</title>
82+ <title>Github Updater Failed: {{ REPOSITORY }} - Action Required</title>
7083 <style>
7184 table {
7285 border-collapse: collapse;
@@ -82,7 +95,7 @@ private function Mail()
8295 </head>
8396 <body>
8497 <p>Dear Admin,</p>
85- <p>We regret to inform you that the latest update for the [PLUGIN NAME] plugin has failed. Please take appropriate action to resolve the issue.</p>
98+ <p>We regret to inform you that the latest update for the {{ REPOSITORY }} repository has failed. Please take appropriate action to resolve the issue.</p>
8699 <p>Update Logs:</p>
87100 <table>
88101 ' . implode ("" , array_map (function ($ entry ) {
@@ -93,7 +106,7 @@ private function Mail()
93106 <p>Best regards</p>
94107 </body>
95108 </html> ' ;
96- $ html_message = str_replace ("[PLUGIN NAME] " , $ this ->repository , $ html_message );
109+ $ html_message = str_replace ("{{ REPOSITORY }} " , $ this ->repository , $ html_message );
97110 if (mail ($ this ->admin , $ subject , $ html_message , $ headers )) {
98111 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Email sent to {$ this ->admin }" ];
99112 } else {
@@ -305,55 +318,77 @@ private function Extract()
305318 private function Upgrade ()
306319 {
307320 sleep (10 );
308- $ plugin_paths = $ this ->MapPath ($ this ->dir , ['path ' => [$ this ->dir . '/.git ' , $ this ->dir . '/update ' , $ this ->dir . '/update.lock ' , $ this ->dir .'/vendor ' , $ this ->dir .'/composer.phar ' ], 'filename ' => ['.gitignore ' ]]);
309- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Plugin list: \n" . json_encode ($ plugin_paths , JSON_PRETTY_PRINT )];
321+ // how to merge array?
322+ $ app_exclude = [];
323+ $ app_exclude ['path ' ] = [$ this ->dir . '/.git ' , $ this ->dir . '/update ' , $ this ->dir . '/update.lock ' , $ this ->dir .'/vendor ' , $ this ->dir .'/composer.phar ' ];
324+ $ app_exclude ['path ' ] = array_merge ($ app_exclude ['path ' ], $ this ->exclude ['path ' ]);
325+ $ app_exclude ['path ' ] = array_unique ($ app_exclude_paths );
326+
327+ $ app_exclude ['filename ' ] = ['.gitignore ' ];
328+ $ app_exclude ['filename ' ] = array_merge ($ app_exclude ['filename ' ], $ this ->exclude ['filename ' ]);
329+ $ app_exclude ['filename ' ] = array_unique ($ app_exclude ['filename ' ]);
330+
331+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "App exclude: \n" . json_encode ($ app_exclude , JSON_PRETTY_PRINT )];
332+
333+ $ app_paths = $ this ->MapPath ($ this ->dir , $ app_exclude );
334+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "App lists: \n" . json_encode ($ app_paths , JSON_PRETTY_PRINT )];
335+
336+ $ app_relative_paths = array_map (function ($ app_path ) {
337+ return substr_replace ($ app_path , '' , 0 , strlen ($ this ->dir ));
338+ }, $ app_paths );
339+
340+ $ upgrade_exclude = [];
341+ $ upgrade_exclude ['path ' ] = [$ this ->dir . '/.git ' , $ this ->dir . '/update.lock ' , $ this ->dir .'/vendor ' , $ this ->dir .'/composer.phar ' ];
342+ $ upgrade_exclude ['path ' ] = array_merge ($ upgrade_exclude ['path ' ], $ this ->exclude ['path ' ]);
343+ $ upgrade_exclude ['path ' ] = array_unique ($ upgrade_exclude ['path ' ]);
310344
311- $ plugin_relative_paths = array_map (function ($ plugin_path ) {
312- return substr_replace ($ plugin_path , '' , 0 , strlen ($ this ->dir ));
313- }, $ plugin_paths );
345+ $ upgrade_exclude ['filename ' ] = ['.gitkeep ' ];
346+ $ upgrade_exclude ['filename ' ] = array_merge ($ upgrade_exclude ['filename ' ], $ this ->exclude ['filename ' ]);
347+ $ upgrade_exclude ['filename ' ] = array_unique ($ upgrade_exclude ['filename ' ]);
348+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Upgrade exclude: \n" . json_encode ($ upgrade_exclude , JSON_PRETTY_PRINT )];
314349
315- $ upgrade_paths = $ this ->MapPath ($ this ->dir . "/update/extract/tmp_ {$ this ->repository }" , [ ' path ' => [ $ this -> dir . ' /.git ' , $ this -> dir . ' /update.lock ' , $ this -> dir . ' /vendor ' , $ this -> dir . ' /composer.phar ' ], ' filename ' => [ ' .gitignore ' ]] );
316- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Upgrade list : \n" . json_encode ($ upgrade_paths , JSON_PRETTY_PRINT )];
350+ $ upgrade_paths = $ this ->MapPath ($ this ->dir . "/update/extract/tmp_ {$ this ->repository }" , $ upgrade_exclude );
351+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Upgrade lists : \n" . json_encode ($ upgrade_paths , JSON_PRETTY_PRINT )];
317352
318353 $ upgrade_relative_paths = array_map (function ($ upgrade_path ) {
319354 return substr_replace ($ upgrade_path , '' , 0 , strlen ($ this ->dir . "/update/extract/tmp_ {$ this ->repository }" ));
320355 }, $ upgrade_paths );
321356
322357 foreach ($ upgrade_relative_paths as $ upgrade_relative_path ) {
323358 $ upgrade_path = $ this ->dir . "/update/extract/tmp_ {$ this ->repository }$ upgrade_relative_path " ;
324- $ plugin_path = $ this ->dir . "$ upgrade_relative_path " ;
359+ $ app_path = $ this ->dir . "$ upgrade_relative_path " ;
325360 if (is_dir ($ upgrade_path )) {
326- if (!is_dir ($ plugin_path )) {
327- if (mkdir ($ plugin_path , 0700 , true )) {
328- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Folder created. $ plugin_path " ];
361+ if (!is_dir ($ app_path )) {
362+ if (mkdir ($ app_path , 0700 , true )) {
363+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Folder created. $ app_path " ];
329364 } else {
330- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Folder cannot be created. $ plugin_path " ];
365+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Folder cannot be created. $ app_path " ];
331366 return false ;
332367 }
333368 }
334369 } elseif (is_file ($ upgrade_path )) {
335- if (is_file ($ plugin_path )) {
370+ if (is_file ($ app_path )) {
336371 $ upgrade_content = file_get_contents ($ upgrade_path , true );
337372 if ($ upgrade_content === false ) {
338373 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to retrieve update content. $ upgrade_path " ];
339374 return false ;
340375 };
341- $ content_size = file_put_contents ($ plugin_path , $ upgrade_content );
376+ $ content_size = file_put_contents ($ app_path , $ upgrade_content );
342377 if ($ content_size === false ) {
343- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to write update content. $ plugin_path " ];
378+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to write update content. $ app_path " ];
344379 return false ;
345380 };
346- $ this ->log [] = [date ("Y-m-d H:i:s " ), "$ content_size bytes written from $ upgrade_path to $ plugin_path " ];
381+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "$ content_size bytes written from $ upgrade_path to $ app_path " ];
347382 } else {
348- if (!copy ($ upgrade_path , $ plugin_path )) {
349- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to copy update content. $ plugin_path " ];
383+ if (!copy ($ upgrade_path , $ app_path )) {
384+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to copy update content. $ app_path " ];
350385 return false ;
351386 };
352387 }
353388 }
354389 }
355390
356- $ delete_relative_paths = array_values (array_diff ($ plugin_relative_paths , $ upgrade_relative_paths ));
391+ $ delete_relative_paths = array_values (array_diff ($ app_relative_paths , $ upgrade_relative_paths ));
357392
358393 foreach ($ delete_relative_paths as $ delete_relative_path ) {
359394 if (is_dir ($ this ->dir . $ delete_relative_path )) {
@@ -368,7 +403,7 @@ private function Upgrade()
368403 $ delete_paths = array_values (array_map (function ($ delete_relative_path ) {
369404 return $ this ->dir . $ delete_relative_path ;
370405 }, $ delete_relative_paths ));
371- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Delete list : \n" . json_encode ($ delete_paths , JSON_PRETTY_PRINT )];
406+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Delete lists : \n" . json_encode ($ delete_paths , JSON_PRETTY_PRINT )];
372407
373408 foreach ($ delete_paths as $ delete_path ) {
374409 if (!$ this ->Delete ($ delete_path )) {
0 commit comments