Skip to content

Commit eebefce

Browse files
committed
feat: add Facade
1 parent 551312b commit eebefce

File tree

5 files changed

+100
-23
lines changed

5 files changed

+100
-23
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,35 @@ You can update your PWA manifest file by running:
8181
php artisan erag:update-manifest
8282
```
8383

84+
## Facade Usage 🎯
85+
86+
You can easily update the manifest dynamically at runtime using the provided `PWA` Facade.
87+
88+
```php
89+
use EragLaravelPwa\Facades\PWA;
90+
91+
PWA::update([
92+
'name' => 'Laravel Apps',
93+
'short_name' => 'LA',
94+
'background_color' => '#6777ef',
95+
'display' => 'fullscreen',
96+
'description' => 'A Progressive Web Application setup for Laravel projects.',
97+
'theme_color' => '#6777ef',
98+
'icons' => [
99+
[
100+
'src' => 'logo.png',
101+
'sizes' => '512x512',
102+
'type' => 'image/png',
103+
],
104+
],
105+
]);
106+
```
107+
108+
- This will immediately update your `manifest.json` file in the public directory.
109+
- You can call this from a controller, job, or anywhere inside your Laravel project where you need to update PWA settings dynamically.
110+
111+
✅ Make sure `public/` folder is writable to allow manifest updates!
112+
84113
This command updates the `manifest.json` file located in the public directory of your Laravel project.
85114

86115
## Usage 🛠️

src/Commands/PWACommand.php

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace EragLaravelPwa\Commands;
44

5+
use EragLaravelPwa\Services\PWAService;
56
use Illuminate\Console\Command;
67
use Illuminate\Support\Facades\Config;
7-
use Illuminate\Support\Facades\File;
88

99
class PWACommand extends Command
1010
{
11+
public function __construct(protected PWAService $pwaService)
12+
{
13+
parent::__construct();
14+
}
15+
1116
/**
1217
* The name and signature of the console command.
1318
*
@@ -53,31 +58,12 @@ public function handle(): void
5358
return;
5459
}
5560

56-
unset($manifest['start_url']);
57-
$icons = $manifest['icons'];
58-
unset($manifest['icons']);
59-
60-
$arrayMergeManifest = array_merge($manifest, ['start_url' => '/'], ['icons' => $icons]);
61-
62-
$jsonData = json_encode($arrayMergeManifest, JSON_PRETTY_PRINT);
63-
if ($jsonData === false) {
64-
$this->error('Failed to encode manifest array to JSON. Aborting operation.');
65-
66-
return;
67-
}
68-
69-
$jsonData = str_replace('\/', '/', $jsonData);
70-
71-
$filePath = public_path('manifest.json');
72-
if (! File::isWritable(public_path())) {
73-
$this->error('Public directory is not writable. Check file permissions.');
74-
75-
return;
61+
if ($this->pwaService->createOrUpdate($manifest)) {
62+
$this->info('Manifest JSON updated successfully ✔');
7663
}
7764

78-
File::put($filePath, $jsonData);
65+
$this->info('Failed to write the manifest file.');
7966

80-
$this->info('Manifest JSON updated successfully ✔');
8167
} catch (\Exception $e) {
8268
// Catch any errors and display an error message
8369
$this->error('An error occurred while updating the manifest: '.$e->getMessage());

src/EragLaravelPwaServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,10 @@ public function boot(): void
5959
Blade::directive('RegisterServiceWorkerScript', function () {
6060
return '<?php echo app(\\EragLaravelPwa\\Services\\PWAService::class)->registerServiceWorkerScript(); ?>';
6161
});
62+
63+
if (class_exists('Illuminate\Foundation\AliasLoader')) {
64+
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
65+
$loader->alias('PWA', \EragLaravelPwa\Facades\PWA::class);
66+
}
6267
}
6368
}

src/Facades/PWA.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace EragLaravelPwa\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class PWA extends Facade
8+
{
9+
protected static function getFacadeAccessor(): string
10+
{
11+
return \EragLaravelPwa\Services\PWAService::class;
12+
}
13+
}

src/Services/PWAService.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace EragLaravelPwa\Services;
44

5+
use Illuminate\Support\Facades\File;
6+
57
class PWAService
68
{
79
public function HeadTag(): string
@@ -92,4 +94,46 @@ private static function getInstallAppHtml(bool $installButton, string $icon): st
9294

9395
return '';
9496
}
97+
98+
public function createOrUpdate(array $manifest): bool
99+
{
100+
unset($manifest['start_url']);
101+
$icons = $manifest['icons'];
102+
unset($manifest['icons']);
103+
104+
$arrayMergeManifest = array_merge($manifest, ['start_url' => '/'], ['icons' => $icons]);
105+
106+
$jsonData = json_encode($arrayMergeManifest, JSON_PRETTY_PRINT);
107+
if ($jsonData === false) {
108+
$this->error('Failed to encode manifest array to JSON. Aborting operation.');
109+
110+
return false;
111+
}
112+
113+
$jsonData = str_replace('\/', '/', $jsonData);
114+
115+
$filePath = public_path('manifest.json');
116+
if (! File::isWritable(public_path())) {
117+
$this->error('Public directory is not writable. Check file permissions.');
118+
119+
return false;
120+
}
121+
122+
File::put($filePath, $jsonData);
123+
124+
return true;
125+
}
126+
127+
public function update(array $manifestData): bool
128+
{
129+
try {
130+
if ($this->createOrUpdate($manifestData)) {
131+
return true;
132+
}
133+
134+
return false;
135+
} catch (\Exception $e) {
136+
return false;
137+
}
138+
}
95139
}

0 commit comments

Comments
 (0)