-
Notifications
You must be signed in to change notification settings - Fork 15
/
AbstractUnit.php
168 lines (143 loc) · 3.39 KB
/
AbstractUnit.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
<?php
namespace icanhazstring\SystemCtl\Unit;
use icanhazstring\SystemCtl\Command\CommandDispatcherInterface;
use icanhazstring\SystemCtl\Command\CommandInterface;
use icanhazstring\SystemCtl\Exception\CommandFailedException;
/**
* Class AbstractUnit
*
* @package icanhazstring\SystemCtl\Unit
*/
abstract class AbstractUnit implements UnitInterface
{
/** @var string */
private $name;
/** @var CommandDispatcherInterface */
protected $commandDispatcher;
/**
* @var string
*/
protected $unitSuffix;
/**
* Create new service with given name
*
* @param string $name
* @param CommandDispatcherInterface $commandDispatcher
*/
public function __construct(string $name, CommandDispatcherInterface $commandDispatcher)
{
$this->name = $name;
$this->commandDispatcher = $commandDispatcher;
}
/**
* @inheritdoc
*/
public function getName(): string
{
return $this->name;
}
/**
* @inheritDoc
*/
public function isMultiInstance(): bool
{
return strpos($this->name, '@') !== false;
}
/**
* @inheritDoc
* @todo: Everything in here should happen inside the constructor and stored
* afterwards.
*/
public function getInstanceName(): ?string
{
$instanceName = explode('@', $this->name, 2)[1] ?? null;
if (is_string($instanceName) && strpos($instanceName, '.') !== false) {
$instanceName = explode('.', $instanceName, 2)[0];
}
return $instanceName;
}
/**
* @return string
*/
abstract protected function getUnitSuffix(): string;
/**
* @return CommandInterface
* @throws CommandFailedException
*/
public function execute(string ...$commands): CommandInterface
{
$commands[] = implode(
'.',
[
$this->name,
$this->getUnitSuffix(),
]
);
return $this->commandDispatcher->dispatch(...$commands);
}
/**
* @return bool
*/
public function start(): bool
{
return $this->execute(__FUNCTION__)->isSuccessful();
}
/**
* @return bool
*/
public function stop(): bool
{
return $this->execute(__FUNCTION__)->isSuccessful();
}
/**
* @return bool
*/
public function disable(): bool
{
return $this->execute(__FUNCTION__)->isSuccessful();
}
/**
* @return bool
*/
public function reload(): bool
{
return $this->execute(__FUNCTION__)->isSuccessful();
}
/**
* @return bool
*/
public function restart(): bool
{
return $this->execute(__FUNCTION__)->isSuccessful();
}
/**
* @return bool
*/
public function enable(): bool
{
return $this->execute(__FUNCTION__)->isSuccessful();
}
/**
* @return bool
*/
public function isEnabled(): bool
{
$output = $this->execute('is-enabled')->getOutput();
return trim($output) === 'enabled';
}
/**
* @return bool
*/
public function isActive(): bool
{
$output = $this->execute('is-active')->getOutput();
return trim($output) === 'active';
}
/**
* @return bool
*/
public function isRunning(): bool
{
return $this->isActive();
}
}