Skip to content

Commit cea62a9

Browse files
authored
add new function to update manually
1 parent 724bda2 commit cea62a9

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ To initialize the Updater class and start the update process, follow these steps
6464
array|null $releaseExclusions = ['path' => [], 'filename' => []],
6565
bool $clear = true,
6666
string $dir = ""
67+
bool $autoUpdate = true
6768
);
6869
```
6970

@@ -77,6 +78,7 @@ To initialize the Updater class and start the update process, follow these steps
7778
> `$releaseExclusions` (Optional) An array of directories or files in the release to exclude from the update.<br>
7879
> `$clear` (Optional) Clear the downloaded file after the update has completed if set to true.<br>
7980
> `$dir` (Optional) Set the directory of the update. Default to current working dir.
81+
> `$autoUpdate` (Optional) Whether or not to automatically update the project. Defaults to true.
8082
8183
> The exclusions array keys:
8284
@@ -92,22 +94,33 @@ To initialize the Updater class and start the update process, follow these steps
9294
]
9395
```
9496

95-
If a new release is available, the class will update your project automatically.
97+
To check the release version, use the following code:
98+
99+
```
100+
$update->release();
101+
```
102+
103+
If a new release is available, the class will update your project automatically.
104+
To update manually, set $autoUpdate to false and use the following code to start update:
105+
106+
```
107+
$update->update();
108+
```
96109

97110
To check the status of the update, use the following code:
98-
111+
99112
```
100113
$update->status();
101114
```
102115

103116
The update status can have the following int values:
104117

105-
> `Updater::STARTED` (100): Indicates that the update has started.<br>
118+
> `Updater::INIT` (100): Indicates that update class has been initialized.<br>
106119
> `Updater::UPDATED` (200): Indicates that the update was successful.<br>
107120
> `Updater::LATEST` (204): Indicates that the project is already up to date.<br>
108121
> `Updater::ERROR` (500): Indicates that the update failed.<br>
109-
> `Updater::BUSY` (504): Indicates that an update process is already in progress.<br>
122+
> `Updater::BUSY` (504): Indicates that an update process is in progress.<br>
110123
111124
## Conclusion
112125

113-
The GitHub Release Updater is a simple and efficient way to keep your project up-to-date with the latest releases on GitHub. It is easy to use and can save you a lot of time and effort. If you have any questions or issues, please feel free to contact us.
126+
The GitHub Release Updater is a simple and efficient way to keep your project up-to-date with the latest releases on GitHub. It is easy to use and can save you a lot of time and effort. If you have any questions or issues, please feel free create an issue.

src/Updater.php

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
final class Updater
88
{
9-
const STARTED = 100;
9+
const INIT = 100;
1010
const UPDATED = 200;
1111
const LATEST = 204;
1212
const ERROR = 500;
@@ -23,12 +23,12 @@ final class Updater
2323
private $dir;
2424
private $exclude = [];
2525
private $log = [];
26-
private $status;
26+
private $status = self::ERROR;
2727
private $clear;
2828
private $archive_relative_paths;
2929

3030
/**
31-
* Constructs a new instance of the class and starts the update process for the provided version.
31+
* Constructs a new instance of the updater class.
3232
*
3333
* @param string $username Your GitHub username.
3434
* @param string $repository The name of your GitHub repository.
@@ -42,12 +42,13 @@ final class Updater
4242
* @param array|null $releaseExclusions (Optional) An array of directories or files in the release to exclude from the update. The array keys:
4343
* 'path' => an array of release excluded paths
4444
* 'filename' => an array of release excluded filenames
45+
* @param bool $clear (Optional) Whether or not to clear the downloaded file. Defaults to true.
46+
* @param string $dir (Optional) The directory where the update will occur. Defaults to current working directory.
47+
* @param bool $autoUpdate (Optional) Whether or not to automatically update the project. Defaults to true.
4548
* @return void
4649
*/
47-
public function __construct(string $username, string $repository, string $token, string $version, string|null $admin = '', string|null $mailer = '', array|null $sourceExclusions = ['path' => [], 'filename' => []], array|null $releaseExclusions = ['path' => [], 'filename' => []], bool $clear = true, $dir = "")
50+
public function __construct(string $username, string $repository, string $token, string $version, string|null $admin = '', string|null $mailer = '', array|null $sourceExclusions = ['path' => [], 'filename' => []], array|null $releaseExclusions = ['path' => [], 'filename' => []], bool $clear = true, $dir = "", $autoUpdate = true)
4851
{
49-
$this->status = $this::STARTED;
50-
5152
if ($admin == null) {
5253
$this->admin = '';
5354
}
@@ -63,6 +64,7 @@ public function __construct(string $username, string $repository, string $token,
6364
if (!isset($sourceExclusions['path'])) {
6465
$sourceExclusions['path'] = [];
6566
}
67+
6668
if (!isset($sourceExclusions['filename'])) {
6769
$sourceExclusions['filename'] = [];
6870
}
@@ -93,22 +95,18 @@ public function __construct(string $username, string $repository, string $token,
9395
$this->dir = getcwd();
9496
}
9597

96-
$update = $this->Install();
98+
$this->status = $this::INIT;
9799

98-
if ($update == $this::ERROR) {
99-
if ($this->admin != '' && $this->mailer != '') {
100-
$this->Mail();
101-
}
100+
if ($autoUpdate) {
101+
$this->update();
102102
}
103-
$this->Log();
104-
$this->status = $update;
105103
}
106104

107105
/**
108106
* Retrieves the status of the updater.
109107
*
110108
* @return int One of the following status codes:
111-
* - `STARTED` (100): Indicates that the update has started.
109+
* - `INIT` (100): Indicates that update class has been initialized.
112110
* - `UPDATED` (200): Indicates that the update was successful.
113111
* - `LATEST` (204): Indicates that the project is already up to date.
114112
* - `ERROR` (500): Indicates that the update failed.
@@ -119,6 +117,38 @@ public function status()
119117
return $this->status;
120118
}
121119

120+
/**
121+
* Retrieves the release version.
122+
*
123+
* @return mixed The release version if the information retrieved successfully, otherwise false.
124+
*/
125+
public function release()
126+
{
127+
if ($this->Download()) {
128+
return $this->release;
129+
} else {
130+
return false;
131+
}
132+
}
133+
134+
/**
135+
* Executes update.
136+
*
137+
* @return void
138+
*/
139+
public function update()
140+
{
141+
$update = $this->Install();
142+
143+
if ($update == $this::ERROR) {
144+
if ($this->admin != '' && $this->mailer != '') {
145+
$this->Mail();
146+
}
147+
}
148+
$this->Log();
149+
$this->status = $update;
150+
}
151+
122152
private function Log()
123153
{
124154
$log = implode("\n", array_map(function ($entry) {

0 commit comments

Comments
 (0)