blob: 923a54692c3937d69b540cd580978a46d9c8e6da (
plain)
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
|
<?php
namespace gipfl\SimpleDaemon;
use Evenement\EventEmitterInterface;
use Evenement\EventEmitterTrait;
use gipfl\Json\JsonSerialization;
use function implode;
use function strlen;
class DaemonState implements JsonSerialization, EventEmitterInterface
{
use EventEmitterTrait;
const ON_CHANGE = 'change';
/** @var string */
protected $processTitle;
/** @var ?string */
protected $state;
/** @var ?string */
protected $currentProcessTitle;
/** @var ?string */
protected $currentMessage;
/** @var string[] */
protected $componentStates = [];
/**
* @return string
*/
public function getProcessTitle()
{
return $this->processTitle;
}
/**
* @param string $processTitle
* @return DaemonState
*/
public function setProcessTitle($processTitle)
{
$this->processTitle = $processTitle;
$this->refreshMessage();
return $this;
}
/**
* @return string|null
*/
public function getState()
{
return $this->state;
}
/**
* @param string|null $state
* @return DaemonState
*/
public function setState($state)
{
$this->state = $state;
$this->refreshMessage();
return $this;
}
/**
* @return string|null
*/
public function getCurrentMessage()
{
return $this->currentMessage;
}
/**
* @return string[]
*/
public function getComponentStates()
{
return $this->componentStates;
}
/**
* @param string[] $componentStates
* @return DaemonState
*/
public function setComponentStates($componentStates)
{
$this->componentStates = $componentStates;
$this->refreshMessage();
return $this;
}
/**
* @param string $name
* @param string $stateMessage
* @return $this
*/
public function setComponentState($name, $stateMessage)
{
if ($stateMessage === null) {
unset($this->componentStates[$name]);
} else {
$this->componentStates[$name] = $stateMessage;
}
$this->refreshMessage();
return $this;
}
public function getComponentState($name)
{
if (isset($this->componentStates[$name])) {
return $this->componentStates[$name];
}
return null;
}
public static function fromSerialization($any)
{
$self = new static();
if (isset($any->state)) {
$self->state = $any->state;
}
if (isset($any->currentMessage)) {
$self->currentMessage = $any->currentMessage;
}
if (isset($any->processTitle)) {
$self->processTitle = $any->processTitle;
}
if (isset($any->components)) {
$self->componentStates = $any->components;
}
return $self;
}
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return (object) [
'state' => $this->state,
'currentMessage' => $this->currentMessage,
'processTitle' => $this->processTitle,
'components' => $this->componentStates
];
}
protected function refreshMessage()
{
$messageParts = [];
$state = $this->getState();
if ($state !== null && strlen($state)) {
$messageParts[] = $state;
}
foreach ($this->getComponentStates() as $component => $message) {
$messageParts[] = "$component: $message";
}
$message = implode(', ', $messageParts);
if ($message !== $this->currentMessage || $this->processTitle !== $this->currentProcessTitle) {
$this->currentMessage = $message;
$this->currentProcessTitle = $this->processTitle;
$this->emit(self::ON_CHANGE, [$this->currentProcessTitle, $this->currentMessage]);
}
}
}
|