This repository was archived by the owner on May 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNotify.php
231 lines (193 loc) · 6.08 KB
/
Notify.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
namespace Flasher\Cli\Prime;
use Flasher\Cli\Prime\Notifier\BaseNotifier;
use Flasher\Cli\Prime\System\Path;
use Flasher\Prime\Notification\NotificationInterface;
final class Notify extends BaseNotifier
{
/**
* @var NotifyInterface|null
*/
private $notifier;
/**
* @var NotifyInterface[]
*/
private $notifiers = array();
/**
* @var NotifyInterface[]
*/
private $sorted = array();
/**
* @var bool|null
*/
private $isSupported;
/**
* @var string|null
*/
private $title;
/**
* @var array{success?: string, error?: string, info?: string, warning?: string}
*/
private $icons = array();
/**
* @param string|null $title
* @param array{success?: string, error?: string, info?: string, warning?: string}|string $icons
*/
public function __construct($title = 'PHPFlasher', $icons = array())
{
$this->title = $title;
$this->icons = $this->configureIcons($icons);
$this->addNotifier(new Notifier\NotifySendBaseNotifier());
$this->addNotifier(new Notifier\AppleScriptBaseNotifier());
$this->addNotifier(new Notifier\GrowlNotifyBaseNotifier());
$this->addNotifier(new Notifier\KDialogBaseNotifier());
$this->addNotifier(new Notifier\NotifuBaseNotifier());
$this->addNotifier(new Notifier\SnoreToastBaseNotifier());
$this->addNotifier(new Notifier\TerminalNotifierBaseNotifier());
$this->addNotifier(new Notifier\ToasterBaseNotifier());
$this->addNotifier(new Notifier\ZenityBaseNotifier());
}
/**
* @param string|null $title
* @param array{success?: string, error?: string, info?: string, warning?: string}|string $icons
*
* @return static
*/
public static function create($title = null, $icons = array())
{
return new self($title, $icons);
}
/**
* {@inheritdoc}
*/
public function send($notification)
{
$notification = $this->configureNotification($notification);
$notifier = $this->createNotifier();
$notifier->send($notification);
}
/**
* @return void
*/
public function addNotifier(NotifyInterface $notifier)
{
$this->notifiers[] = $notifier;
$this->reset();
}
/**
* {@inheritdoc}
*/
public function getPriority()
{
return 0;
}
/**
* {@inheritdoc}
*/
public function isSupported()
{
if (null !== $this->isSupported) {
return $this->isSupported;
}
if ($this->notifier instanceof NotifyInterface) {
return true;
}
foreach ($this->getNotifiers() as $notifier) {
if ($notifier->isSupported()) {
return $this->isSupported = true;
}
}
return false;
}
/**
* @return void
*/
private function reset()
{
$this->notifier = null;
$this->sorted = array();
$this->isSupported = null;
}
/**
* @return NotifyInterface[]
*/
private function getNotifiers()
{
if (array() !== $this->sorted) {
return $this->sorted;
}
$this->sorted = $this->notifiers;
usort($this->sorted, static function (NotifyInterface $a, NotifyInterface $b) {
$priorityA = $a->getPriority();
$priorityB = $b->getPriority();
if ($priorityA === $priorityB) {
return 0;
}
return $priorityA < $priorityB ? 1 : -1;
});
return $this->sorted;
}
/**
* @return NotifyInterface
*/
private function createNotifier()
{
if ($this->notifier instanceof NotifyInterface) {
return $this->notifier;
}
foreach ($this->getNotifiers() as $notifier) {
if ($notifier->isSupported()) {
return $this->notifier = $notifier;
}
}
return new Notifier\NullBaseNotifier();
}
/**
* @param array{success?: string, error?: string, info?: string, warning?: string}|string $icons
*
* @return array<'default'|'error'|'info'|'success'|'warning', string>
*/
private function configureIcons($icons = array())
{
$icons = $icons ?: array();
if (!\is_array($icons)) {
$icons = array(
NotificationInterface::SUCCESS => $icons,
NotificationInterface::ERROR => $icons,
NotificationInterface::INFO => $icons,
NotificationInterface::WARNING => $icons,
'default' => $icons,
);
}
return array_merge(array(
NotificationInterface::SUCCESS => Path::realpath(__DIR__.'/Resources/icons/success.png'),
NotificationInterface::ERROR => Path::realpath(__DIR__.'/Resources/icons/error.png'),
NotificationInterface::INFO => Path::realpath(__DIR__.'/Resources/icons/info.png'),
NotificationInterface::WARNING => Path::realpath(__DIR__.'/Resources/icons/warning.png'),
'default' => Path::realpath(__DIR__.'/Resources/icons/info.png'),
), $icons);
}
/**
* @param Notification|string $notification
*
* @return Notification
*/
private function configureNotification($notification)
{
$notification = Notification::wrap($notification);
if (null === $notification->getTitle()) {
$notification->setTitle($this->title);
}
if (null === $notification->getIcon() && isset($this->icons[$notification->getType()])) {
$notification->setIcon($this->icons[$notification->getType()]);
}
if (null === $notification->getIcon()) {
$notification->setIcon($this->icons['default']); // @phpstan-ignore-line
}
return $notification;
}
}