Skip to content

Commit 5e88397

Browse files
committed
feat(notification): Show desktop notification on completion
1 parent ef4c362 commit 5e88397

File tree

4 files changed

+169
-108
lines changed

4 files changed

+169
-108
lines changed

app/Commands/PrefixCommand.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use App\Support\Validator;
1717
use Illuminate\Support\Str;
1818
use LaravelZero\Framework\Commands\Command;
19+
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notification;
20+
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notifier;
1921
use Symfony\Component\Console\Input\InputInterface;
2022
use Symfony\Component\Console\Output\OutputInterface;
2123

@@ -146,16 +148,19 @@ private function renderOutput($state, $start)
146148
switch ($state) {
147149
case 'success':
148150
$this->info('PHP-Prefixer: project prefixed successfully');
151+
$this->notify('PHP-Prefixer CLI', 'Project prefixed successfully');
149152
$this->info($formattedProcessingTime);
150153

151154
return 0;
152155
case 'cancelled':
153156
$this->error('PHP-Prefixer: project prefixing cancelled');
157+
$this->notify('PHP-Prefixer CLI', 'Project prefixing cancelled');
154158
$this->info($formattedProcessingTime);
155159

156160
return 1;
157161
case 'failed':
158162
$this->error('PHP-Prefixer: Project prefixing failed');
163+
$this->notify('PHP-Prefixer CLI', 'Project prefixing failed');
159164
$this->info($formattedProcessingTime);
160165

161166
return 1;
@@ -166,4 +171,44 @@ private function renderOutput($state, $start)
166171

167172
return 1;
168173
}
174+
175+
private function notify($title, $body)
176+
{
177+
$notifier = app(Notifier::class);
178+
179+
$notification = app(Notification::class)
180+
->setTitle($title)
181+
->setBody($body);
182+
183+
$notification = $this->setIcon($notification);
184+
$notifier->send($notification);
185+
}
186+
187+
private function setIcon($notification)
188+
{
189+
$icon = 'config/PHPPrefixer.png';
190+
191+
return $notification->setIcon($this->extractIcon($icon));
192+
}
193+
194+
private function extractIcon($icon)
195+
{
196+
$pharPath = \Phar::running(false);
197+
198+
if (empty($pharPath)) {
199+
return $icon;
200+
}
201+
202+
$phar = new \Phar($pharPath);
203+
$tmpDir = sys_get_temp_dir();
204+
$tmpIcon = $tmpDir.'/'.$icon;
205+
206+
if (file_exists($tmpIcon)) {
207+
return $tmpIcon;
208+
}
209+
210+
$phar->extractTo($tmpDir, $icon);
211+
212+
return $tmpIcon;
213+
}
169214
}

composer.json

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
11
{
22
"name": "php-prefixer/php-prefixer-cli",
3+
"type": "project",
34
"description": "A command-line for the PHP-Prefixer service.",
4-
"keywords": ["php-prefixer", "php", "prefixer", "service", "rest", "cli", "functions", "namespace", "prefix", "namespaces-php"],
5+
"keywords": [
6+
"php-prefixer",
7+
"php",
8+
"prefixer",
9+
"service",
10+
"rest",
11+
"cli",
12+
"functions",
13+
"namespace",
14+
"prefix",
15+
"namespaces-php"
16+
],
517
"homepage": "http://php-prefixer.com/",
6-
"type": "project",
718
"license": "MIT",
8-
"support": {
9-
"issues": "https://github.com/php-prefixer/php-prefixer-cli/issues",
10-
"source": "https://github.com/php-prefixer/php-prefixer-cli"
11-
},
1219
"authors": [
1320
{
1421
"name": "Aníbal Sánchez, Desarrollos Inteligentes Virtuales, SL.",
1522
"email": "team@php-prefixer.com"
1623
}
1724
],
1825
"require": {
19-
"php": "^7.3|^8.0",
26+
"php": "^7.3 || ^8.0",
2027
"guzzlehttp/guzzle": "^7.0.1",
2128
"http-interop/http-factory-guzzle": "^1.0",
2229
"knplabs/github-api": "^3.0",
2330
"laminas/laminas-text": "^2.8",
2431
"laravel-zero/framework": "^8.7",
25-
"nelexa/zip": "^4.0"
32+
"nelexa/zip": "^4.0",
33+
"nunomaduro/laravel-desktop-notifier": "^2.5"
2634
},
2735
"require-dev": {
2836
"mockery/mockery": "^1.4.2",
2937
"phpunit/phpunit": "^9.5"
3038
},
39+
"config": {
40+
"optimize-autoloader": true,
41+
"preferred-install": "dist",
42+
"sort-packages": true
43+
},
3144
"autoload": {
3245
"psr-4": {
3346
"App\\": "app/"
@@ -38,12 +51,13 @@
3851
"Tests\\": "tests/"
3952
}
4053
},
41-
"config": {
42-
"preferred-install": "dist",
43-
"sort-packages": true,
44-
"optimize-autoloader": true
45-
},
4654
"minimum-stability": "dev",
4755
"prefer-stable": true,
48-
"bin": ["application"]
56+
"bin": [
57+
"application"
58+
],
59+
"support": {
60+
"issues": "https://github.com/php-prefixer/php-prefixer-cli/issues",
61+
"source": "https://github.com/php-prefixer/php-prefixer-cli"
62+
}
4963
}

0 commit comments

Comments
 (0)