1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\WebPush; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Minishlink\WebPush\WebPush; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
|
9
|
|
|
class WebPushServiceProvider extends ServiceProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Register the application services. |
13
|
|
|
* |
14
|
|
|
* @return void |
15
|
|
|
*/ |
16
|
14 |
|
public function register() |
17
|
|
|
{ |
18
|
14 |
|
$this->commands([VapidKeysGenerateCommand::class]); |
19
|
|
|
|
20
|
14 |
|
$this->mergeConfigFrom(__DIR__.'/../config/webpush.php', 'webpush'); |
21
|
14 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Bootstrap the application services. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
14 |
|
public function boot() |
29
|
|
|
{ |
30
|
14 |
|
$this->app->when(WebPushChannel::class) |
31
|
14 |
|
->needs(WebPush::class) |
32
|
14 |
|
->give(function () { |
33
|
|
|
$webPush = new WebPush($this->webPushConfig()); |
34
|
|
|
$webPush->setReuseVAPIDHeaders(true); |
35
|
|
|
|
36
|
|
|
return $webPush; |
37
|
14 |
|
}); |
38
|
|
|
|
39
|
14 |
|
if ($this->app->runningInConsole()) { |
40
|
14 |
|
$this->definePublishing(); |
41
|
|
|
} |
42
|
14 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
protected function webPushConfig() |
48
|
|
|
{ |
49
|
|
|
$config = []; |
50
|
|
|
$webpush = config('webpush'); |
51
|
|
|
$publicKey = $webpush['vapid']['public_key']; |
52
|
|
|
$privateKey = $webpush['vapid']['private_key']; |
53
|
|
|
|
54
|
|
|
if (! empty($webpush['gcm']['key'])) { |
55
|
|
|
$config['GCM'] = $webpush['gcm']['key']; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (empty($publicKey) || empty($privateKey)) { |
59
|
|
|
return $config; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$config['VAPID'] = compact('publicKey', 'privateKey'); |
63
|
|
|
$config['VAPID']['subject'] = $webpush['vapid']['subject']; |
64
|
|
|
|
65
|
|
|
if (empty($config['VAPID']['subject'])) { |
66
|
|
|
$config['VAPID']['subject'] = url('/'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (! empty($webpush['vapid']['pem_file'])) { |
70
|
|
|
$config['VAPID']['pemFile'] = $webpush['vapid']['pem_file']; |
71
|
|
|
|
72
|
|
|
if (Str::startsWith($config['VAPID']['pemFile'], 'storage')) { |
73
|
|
|
$config['VAPID']['pemFile'] = base_path($config['VAPID']['pemFile']); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $config; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Define the publishable migrations and resources. |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
14 |
|
protected function definePublishing() |
86
|
|
|
{ |
87
|
14 |
|
$this->publishes([ |
88
|
14 |
|
__DIR__.'/../config/webpush.php' => config_path('webpush.php'), |
89
|
14 |
|
], 'config'); |
90
|
|
|
|
91
|
14 |
|
if (! class_exists('CreatePushSubscriptionsTable')) { |
92
|
1 |
|
$timestamp = date('Y_m_d_His', time()); |
93
|
|
|
|
94
|
1 |
|
$this->publishes([ |
95
|
1 |
|
__DIR__.'/../migrations/create_push_subscriptions_table.php.stub' => $this->app->databasePath().'/migrations/'.$timestamp.'_create_push_subscriptions_table.php', |
96
|
1 |
|
], 'migrations'); |
97
|
|
|
} |
98
|
14 |
|
} |
99
|
|
|
} |
100
|
|
|
|