@@ -31,7 +31,6 @@ public function __construct(string $name, CommandDispatcherInterface $commandDis
3131 $ this ->commandDispatcher = $ commandDispatcher ;
3232 }
3333
34-
3534 /**
3635 * @param string $type
3736 * @param string $name
@@ -154,19 +153,41 @@ public function enable(): bool
154153 */
155154 public function isEnabled (): bool
156155 {
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 ());
158168
159- return trim ( $ output) === ' enabled ' ;
169+ return $ output ;
160170 }
161171
162172 /**
163173 * @return bool
164174 */
165175 public function isActive (): bool
166176 {
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 ());
168189
169- return trim ( $ output) === ' active ' ;
190+ return $ output ;
170191 }
171192
172193 /**
@@ -176,4 +197,43 @@ public function isRunning(): bool
176197 {
177198 return $ this ->isActive ();
178199 }
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+ }
179239}
0 commit comments