1 | <?php |
||
10 | class VapidKeysGenerateCommand extends Command |
||
11 | { |
||
12 | use ConfirmableTrait; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $signature = 'webpush:vapid |
||
18 | {--show : Display the keys instead of modifying files} |
||
19 | {--force : Force the operation to run when in production}'; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $description = 'Generate VAPID keys.'; |
||
25 | |||
26 | /** |
||
27 | * Execute the console command. |
||
28 | * |
||
29 | * @return mixed |
||
30 | */ |
||
31 | 3 | public function handle() |
|
48 | |||
49 | /** |
||
50 | * Set the keys in the environment file. |
||
51 | * |
||
52 | * @param array $keys |
||
53 | * @return bool |
||
54 | */ |
||
55 | 1 | protected function setKeysInEnvironmentFile($keys) |
|
67 | |||
68 | /** |
||
69 | * Write a new environment file with the given keys. |
||
70 | * |
||
71 | * @param array $keys |
||
72 | * @return void |
||
73 | */ |
||
74 | 1 | protected function writeNewEnvironmentFileWith($keys) |
|
75 | { |
||
76 | 1 | $contents = file_get_contents($this->laravel->environmentFilePath()); |
|
77 | |||
78 | 1 | if (! Str::contains($contents, 'VAPID_PUBLIC_KEY')) { |
|
79 | 1 | $contents .= PHP_EOL.'VAPID_PUBLIC_KEY='; |
|
80 | } |
||
81 | |||
82 | 1 | if (! Str::contains($contents, 'VAPID_PRIVATE_KEY')) { |
|
83 | 1 | $contents .= PHP_EOL.'VAPID_PRIVATE_KEY='; |
|
84 | } |
||
85 | |||
86 | 1 | $contents = preg_replace( |
|
87 | [ |
||
88 | 1 | $this->keyReplacementPattern('VAPID_PUBLIC_KEY'), |
|
89 | 1 | $this->keyReplacementPattern('VAPID_PRIVATE_KEY'), |
|
90 | ], |
||
91 | [ |
||
92 | 1 | 'VAPID_PUBLIC_KEY='.$keys['publicKey'], |
|
93 | 1 | 'VAPID_PRIVATE_KEY='.$keys['privateKey'], |
|
94 | ], |
||
95 | $contents |
||
96 | ); |
||
97 | |||
98 | 1 | file_put_contents($this->laravel->environmentFilePath(), $contents); |
|
99 | 1 | } |
|
100 | |||
101 | /** |
||
102 | * Get a regex pattern that will match env $keyName with any key. |
||
103 | * |
||
104 | * @param string $keyName |
||
105 | * @return string |
||
106 | */ |
||
107 | 1 | protected function keyReplacementPattern($keyName) |
|
121 | } |
||
122 |