Skip to content

Commit a19e54f

Browse files
authored
Add AutoUpdater feature with events and facade support (#570)
* Add AutoUpdater feature with events and facade support Introduces the `AutoUpdater` class to manage update processes via the client. Adds events such as `UpdateAvailable`, `UpdateDownloaded`, and others for broadcasting update states. A facade is also provided for convenient usage. * Add DownloadProgress event for real-time update broadcasting This event handles broadcasting download progress updates through a 'nativephp' channel. It includes details such as total size, delta, transferred bytes, percentage completed, and speed. * fix: update property types in DownloadProgress constructor Adjusted the types of total, delta, and bytesPerSecond from float to int to ensure type consistency and accuracy. This change prevents potential precision issues and aligns with expected data formats.
1 parent 949c1ac commit a19e54f

8 files changed

+173
-0
lines changed

src/AutoUpdater.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Native\Laravel;
4+
5+
use Native\Laravel\Client\Client;
6+
7+
class AutoUpdater
8+
{
9+
public function __construct(protected Client $client) {}
10+
11+
public function checkForUpdates(): void
12+
{
13+
$this->client->post('auto-updater/check-for-updates');
14+
}
15+
16+
public function quitAndInstall(): void
17+
{
18+
$this->client->post('auto-updater/quit-and-install');
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\AutoUpdater;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class CheckingForUpdate implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct() {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\AutoUpdater;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class DownloadProgress implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct(public int $total, public int $delta, public int $transferred, public float $percent, public int $bytesPerSecond) {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}

src/Events/AutoUpdater/Error.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\AutoUpdater;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class Error implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct(public string $error) {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\AutoUpdater;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class UpdateAvailable implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct() {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\AutoUpdater;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class UpdateDownloaded implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct(public string $version, public string $downloadedFile, public string $releaseDate, public ?string $releaseNotes, public ?string $releaseName) {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\AutoUpdater;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class UpdateNotAvailable implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function broadcastOn()
16+
{
17+
return [
18+
new Channel('nativephp'),
19+
];
20+
}
21+
}

src/Facades/AutoUpdater.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Native\Laravel\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static void checkForUpdates()
9+
* @method static void quitAndInstall()
10+
*/
11+
class AutoUpdater extends Facade
12+
{
13+
protected static function getFacadeAccessor()
14+
{
15+
return \Native\Laravel\AutoUpdater::class;
16+
}
17+
}

0 commit comments

Comments
 (0)