|
4 | 4 |
|
5 | 5 | use Symfony\Component\Process\ProcessBuilder; |
6 | 6 | use SystemCtl\Exception\UnitTypeNotSupportedException; |
| 7 | +use SystemCtl\Unit\AbstractUnit; |
7 | 8 | use SystemCtl\Unit\Service; |
8 | 9 | use SystemCtl\Unit\Timer; |
9 | 10 | use SystemCtl\Unit\UnitInterface; |
10 | 11 |
|
| 12 | +/** |
| 13 | + * SystemCtl |
| 14 | + * |
| 15 | + * @method Service getService(string $unit) |
| 16 | + * @method Timer getTimer(string $unit) |
| 17 | + * |
| 18 | + * @method array getServices(?string $unitPrefix = null) |
| 19 | + * @method array getTimers(?string $unitPrefix = null) |
| 20 | + * |
| 21 | + * @package SystemCtl |
| 22 | + * @author icanhazstring <[email protected]> |
| 23 | + */ |
11 | 24 | class SystemCtl |
12 | 25 | { |
13 | 26 | /** @var string systemctl binary path */ |
@@ -100,47 +113,52 @@ public function listUnits(?string $unitPrefix = null, array $unitTypes = self::S |
100 | 113 | }, []); |
101 | 114 | } |
102 | 115 |
|
103 | | - /** |
104 | | - * @param string $name |
105 | | - * @return Service |
106 | | - */ |
107 | | - public function getService(string $name): Service |
| 116 | + public function __call($name, $arguments) |
108 | 117 | { |
109 | | - return new Service($name, $this->getProcessBuilder()); |
110 | | - } |
| 118 | + preg_match('/get(?<unit>[^s]+)(?<plural>s)?/', $name, $match); |
111 | 119 |
|
112 | | - /** |
113 | | - * @param null|string $unitPrefix |
114 | | - * @return Service[] |
115 | | - */ |
116 | | - public function getServices(?string $unitPrefix = null): array |
117 | | - { |
118 | | - $units = $this->listUnits($unitPrefix, [Service::UNIT]); |
| 120 | + $isPlural = isset($match['plural']); |
| 121 | + $unitName = strtolower($match['unit']); |
119 | 122 |
|
120 | | - return array_map(function ($unitName) { |
121 | | - return new Service($unitName, $this->getProcessBuilder()); |
122 | | - }, $units); |
| 123 | + if (!in_array($unitName, self::SUPPORTED_UNITS)) { |
| 124 | + throw new UnitTypeNotSupportedException("Unit {$unitName} not supported"); |
| 125 | + } |
| 126 | + |
| 127 | + // Singular differs requested name? |
| 128 | + // Get a list of units |
| 129 | + if ($isPlural) { |
| 130 | + return $this->getUnits(ucfirst($unitName), $arguments); |
| 131 | + } |
| 132 | + |
| 133 | + return $this->getUnit(ucfirst($unitName), $arguments); |
123 | 134 | } |
124 | 135 |
|
125 | 136 | /** |
126 | | - * @param string $name |
127 | | - * @return Timer |
| 137 | + * @param string $unitClass |
| 138 | + * @param $args |
| 139 | + * @return AbstractUnit |
128 | 140 | */ |
129 | | - public function getTimer(string $name): Timer |
| 141 | + private function getUnit(string $unitClass, $args): AbstractUnit |
130 | 142 | { |
131 | | - return new Timer($name, $this->getProcessBuilder()); |
| 143 | + $args[] = $this->getProcessBuilder(); |
| 144 | + $className = '\SystemCtl\Unit\\' . $unitClass; |
| 145 | + |
| 146 | + return new $className(...$args); |
132 | 147 | } |
133 | 148 |
|
134 | 149 | /** |
135 | | - * @param null|string $unitPrefix |
136 | | - * @return Timer[] |
| 150 | + * @param string $unitName |
| 151 | + * @param $arguments |
| 152 | + * @return array |
137 | 153 | */ |
138 | | - public function getTimers(?string $unitPrefix = null): array |
| 154 | + private function getUnits(string $unitName, $arguments): array |
139 | 155 | { |
140 | | - $units = $this->listUnits($unitPrefix, [Timer::UNIT]); |
| 156 | + $unitPrefix = $arguments[0] ?? null; |
| 157 | + $units = $this->listUnits($unitPrefix, [strtolower($unitName)]); |
| 158 | + $unitClass = '\SystemCtl\Unit\\' . $unitName; |
141 | 159 |
|
142 | | - return array_map(function ($unitName) { |
143 | | - return new Timer($unitName, $this->getProcessBuilder()); |
| 160 | + return array_map(function ($unitName) use ($unitClass) { |
| 161 | + return new $unitClass($unitName, $this->getProcessBuilder()); |
144 | 162 | }, $units); |
145 | 163 | } |
146 | 164 |
|
|
0 commit comments