diff --git a/README.md b/README.md index b87c205..defeec4 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ Available unit commands are: - restart - isEnabled - isActive +- show ```php $systemCtl = new SystemCtl(); diff --git a/src/Unit/AbstractUnit.php b/src/Unit/AbstractUnit.php index 3bbea32..05c6735 100644 --- a/src/Unit/AbstractUnit.php +++ b/src/Unit/AbstractUnit.php @@ -31,7 +31,6 @@ public function __construct(string $name, CommandDispatcherInterface $commandDis $this->commandDispatcher = $commandDispatcher; } - /** * @param string $type * @param string $name @@ -154,9 +153,20 @@ public function enable(): bool */ public function isEnabled(): bool { - $output = $this->execute('is-enabled')->getOutput(); + return $this->isEnabledRaw() === 'enabled'; + } + + /** + * Get the raw (text) output of the `is-enabled` command. + * + * @return string + */ + public function isEnabledRaw(): string + { + // We have to trim() the output, as it may end in a newline character that we don't want. + $output = \trim($this->execute('is-enabled')->getOutput()); - return trim($output) === 'enabled'; + return $output; } /** @@ -164,9 +174,20 @@ public function isEnabled(): bool */ public function isActive(): bool { - $output = $this->execute('is-active')->getOutput(); + return $this->isActiveRaw() === 'active'; + } + + /** + * Get the raw (text) output of the `is-active` command. + * + * @return string + */ + public function isActiveRaw(): string + { + // We have to trim() the output, as it may end in a newline character that we don't want. + $output = \trim($this->execute('is-active')->getOutput()); - return trim($output) === 'active'; + return $output; } /** @@ -176,4 +197,43 @@ public function isRunning(): bool { return $this->isActive(); } + + /** + * Get an array of debugging unit information from the output of the systemctl `show` command. + * + * The output uses the service information as the returned array key, e.g. + * [ + * 'Type' => 'service', + * 'Restart' => 'no', + * ... + * ] + * + * @return array + */ + public function show(): array + { + // Turn the output string into an array, using a newline to separate entries. + $output = \explode( + "\n", + $this->execute('show')->getOutput() + ); + + // Walk the array to re-key it based on the systemd service information kay/value. + $outputArray = []; + \array_walk( + $output, + function ($line) use (&$outputArray) { + // Skip any empty lines/lines that do not contain '=', as the raw systemd output always + // contains =, e.g. 'Restart=no'. If we do not have this value, then we cannot split it as below. + if (empty($line) || false === \strpos($line, "=")) { + return; + } + $lineSplit = \explode("=", $line, 2); + + $outputArray[$lineSplit[0]] = $lineSplit[1]; + } + ); + + return $outputArray; + } } diff --git a/src/Unit/UnitInterface.php b/src/Unit/UnitInterface.php index 807767e..6f9b31e 100644 --- a/src/Unit/UnitInterface.php +++ b/src/Unit/UnitInterface.php @@ -98,10 +98,38 @@ public function enable(): bool; */ public function isActive(): bool; + /** + * Get the raw (text) output of the `is-active` command. + * + * @return string + */ + public function isActiveRaw(): string; + /** * Check whether unit is enabled * * @return bool */ public function isEnabled(): bool; + + /** + * Get the raw (text) output of the `is-enabled` command. + * + * @return string + */ + public function isEnabledRaw(): string; + + /** + * Get an array of debugging unit information from the output of the systemctl `show` command. + * + * The output uses the service information as the returned array key, e.g. + * [ + * 'Type' => 'service', + * 'Restart' => 'no', + * ... + * ] + * + * @return array + */ + public function show(): array; }