@@ -31,7 +31,6 @@ public function __construct(string $name, CommandDispatcherInterface $commandDis
31
31
$ this ->commandDispatcher = $ commandDispatcher ;
32
32
}
33
33
34
-
35
34
/**
36
35
* @param string $type
37
36
* @param string $name
@@ -154,19 +153,41 @@ public function enable(): bool
154
153
*/
155
154
public function isEnabled (): bool
156
155
{
157
- $ output = $ this ->execute ('is-enabled ' )->getOutput ();
156
+ return $ this ->isEnabledRaw () === 'enabled ' ;
157
+ }
158
+
159
+ /**
160
+ * Get the raw (text) output of the `is-enabled` command.
161
+ *
162
+ * @return string
163
+ */
164
+ public function isEnabledRaw (): string
165
+ {
166
+ // We have to trim() the output, as it may end in a newline character that we don't want.
167
+ $ output = \trim ($ this ->execute ('is-enabled ' )->getOutput ());
158
168
159
- return trim ( $ output) === ' enabled ' ;
169
+ return $ output ;
160
170
}
161
171
162
172
/**
163
173
* @return bool
164
174
*/
165
175
public function isActive (): bool
166
176
{
167
- $ output = $ this ->execute ('is-active ' )->getOutput ();
177
+ return $ this ->isActiveRaw () === 'active ' ;
178
+ }
179
+
180
+ /**
181
+ * Get the raw (text) output of the `is-active` command.
182
+ *
183
+ * @return string
184
+ */
185
+ public function isActiveRaw (): string
186
+ {
187
+ // We have to trim() the output, as it may end in a newline character that we don't want.
188
+ $ output = \trim ($ this ->execute ('is-active ' )->getOutput ());
168
189
169
- return trim ( $ output) === ' active ' ;
190
+ return $ output ;
170
191
}
171
192
172
193
/**
@@ -176,4 +197,43 @@ public function isRunning(): bool
176
197
{
177
198
return $ this ->isActive ();
178
199
}
200
+
201
+ /**
202
+ * Get an array of debugging unit information from the output of the systemctl `show` command.
203
+ *
204
+ * The output uses the service information as the returned array key, e.g.
205
+ * [
206
+ * 'Type' => 'service',
207
+ * 'Restart' => 'no',
208
+ * ...
209
+ * ]
210
+ *
211
+ * @return array
212
+ */
213
+ public function show (): array
214
+ {
215
+ // Turn the output string into an array, using a newline to separate entries.
216
+ $ output = \explode (
217
+ "\n" ,
218
+ $ this ->execute ('show ' )->getOutput ()
219
+ );
220
+
221
+ // Walk the array to re-key it based on the systemd service information kay/value.
222
+ $ outputArray = [];
223
+ \array_walk (
224
+ $ output ,
225
+ function ($ line ) use (&$ outputArray ) {
226
+ // Skip any empty lines/lines that do not contain '=', as the raw systemd output always
227
+ // contains =, e.g. 'Restart=no'. If we do not have this value, then we cannot split it as below.
228
+ if (empty ($ line ) || false === \strpos ($ line , "= " )) {
229
+ return ;
230
+ }
231
+ $ lineSplit = \explode ("= " , $ line , 2 );
232
+
233
+ $ outputArray [$ lineSplit [0 ]] = $ lineSplit [1 ];
234
+ }
235
+ );
236
+
237
+ return $ outputArray ;
238
+ }
179
239
}
0 commit comments