1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\WebPush; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Minishlink\WebPush\WebPush; |
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
|
|
|
->give(function () { |
33
|
|
|
$webPush = new WebPush($this->webPushConfig(), [], 30, $this->clientOptionsConfig()); |
34
|
|
|
$webPush->setReuseVAPIDHeaders(true); |
35
|
|
|
|
36
|
|
|
return $webPush; |
37
|
14 |
|
}); |
38
|
|
|
|
39
|
14 |
|
$this->app->when(WebPushChannel::class) |
40
|
14 |
|
->needs(ReportHandlerInterface::class) |
41
|
14 |
|
->give(ReportHandler::class); |
42
|
|
|
|
43
|
14 |
|
if ($this->app->runningInConsole()) { |
44
|
14 |
|
$this->definePublishing(); |
45
|
|
|
} |
46
|
14 |
|
} |
47
|
|
|
/** |
48
|
|
|
* Get Guzzle Request options from config file and |
49
|
|
|
* pass to WebPush. |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
protected function clientOptionsConfig() |
53
|
|
|
{ |
54
|
|
|
$webpush = config('webpush'); |
55
|
|
|
return $webpush['client_options']; |
56
|
|
|
} |
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
protected function webPushConfig() |
61
|
|
|
{ |
62
|
|
|
$config = []; |
63
|
|
|
$webpush = config('webpush'); |
64
|
|
|
$publicKey = $webpush['vapid']['public_key']; |
65
|
|
|
$privateKey = $webpush['vapid']['private_key']; |
66
|
|
|
|
67
|
|
|
if (! empty($webpush['gcm']['key'])) { |
68
|
|
|
$config['GCM'] = $webpush['gcm']['key']; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (empty($publicKey) || empty($privateKey)) { |
72
|
|
|
return $config; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$config['VAPID'] = compact('publicKey', 'privateKey'); |
76
|
|
|
$config['VAPID']['subject'] = $webpush['vapid']['subject']; |
77
|
|
|
|
78
|
|
|
if (empty($config['VAPID']['subject'])) { |
79
|
|
|
$config['VAPID']['subject'] = url('/'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (! empty($webpush['vapid']['pem_file'])) { |
83
|
|
|
$config['VAPID']['pemFile'] = $webpush['vapid']['pem_file']; |
84
|
|
|
|
85
|
|
|
if (Str::startsWith($config['VAPID']['pemFile'], 'storage')) { |
86
|
|
|
$config['VAPID']['pemFile'] = base_path($config['VAPID']['pemFile']); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $config; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Define the publishable migrations and resources. |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
14 |
|
protected function definePublishing() |
99
|
|
|
{ |
100
|
14 |
|
$this->publishes([ |
101
|
14 |
|
__DIR__.'/../config/webpush.php' => config_path('webpush.php'), |
102
|
14 |
|
], 'config'); |
103
|
|
|
|
104
|
14 |
|
if (! class_exists('CreatePushSubscriptionsTable')) { |
105
|
1 |
|
$timestamp = date('Y_m_d_His', time()); |
106
|
|
|
|
107
|
1 |
|
$this->publishes([ |
108
|
1 |
|
__DIR__.'/../migrations/create_push_subscriptions_table.php.stub' => database_path("migrations/{$timestamp}_create_push_subscriptions_table.php"), |
109
|
1 |
|
], 'migrations'); |
110
|
|
|
} |
111
|
14 |
|
} |
112
|
|
|
} |
113
|
|
|
|