22
33namespace SystemCtl ;
44
5- use Symfony \Component \Process \ProcessBuilder ;
5+ use SystemCtl \Command \CommandDispatcherInterface ;
6+ use SystemCtl \Command \SymfonyCommandDispatcher ;
67use SystemCtl \Exception \UnitTypeNotSupportedException ;
78use SystemCtl \Unit \Service ;
89use SystemCtl \Unit \Timer ;
@@ -35,6 +36,8 @@ class SystemCtl
3536 Timer::UNIT ,
3637 ];
3738
39+ private $ commandDispatcher ;
40+
3841 /**
3942 * Change systemctl binary
4043 *
@@ -61,6 +64,7 @@ public static function setTimeout(int $timeout): void
6164 *
6265 * @return UnitInterface
6366 * @throws UnitTypeNotSupportedException
67+ * @deprecated This static method is deprecated, please refer to a specifc get method for a unit
6468 */
6569 public static function unitFromSuffix (string $ unitSuffix , string $ unitName ): UnitInterface
6670 {
@@ -70,105 +74,121 @@ public static function unitFromSuffix(string $unitSuffix, string $unitName): Uni
7074 throw new UnitTypeNotSupportedException ('Unit type ' . $ unitSuffix . ' not supported ' );
7175 }
7276
73- return new $ unitClass ($ unitName , new ProcessBuilder ([self ::$ binary ]));
77+ $ commandDispatcher = (new SymfonyCommandDispatcher )
78+ ->setTimeout (self ::$ timeout )
79+ ->setBinary (self ::$ binary );
80+
81+ return new $ unitClass ($ unitName , $ commandDispatcher );
7482 }
7583
7684 /**
7785 * List all supported units
7886 *
7987 * @param null|string $unitPrefix
80- * @param string[] $unitTypes
88+ * @param string[] $unitTypes
89+ *
8190 * @return array|\string[]
8291 */
8392 public function listUnits (?string $ unitPrefix = null , array $ unitTypes = self ::SUPPORTED_UNITS ): array
8493 {
85- $ processBuilder = $ this ->getProcessBuilder ()
86- ->add ('list-units ' );
94+ $ commands = ['list-units ' ];
8795
8896 if ($ unitPrefix ) {
89- $ processBuilder -> add ( $ unitPrefix . '* ' ) ;
97+ $ commands [] = [ $ unitPrefix . '* ' ] ;
9098 }
9199
92- $ process = $ processBuilder ->getProcess ();
93-
94- $ process ->run ();
95- $ output = $ process ->getOutput ();
100+ $ output = $ this ->getCommandDispatcher ()->dispatch (...$ commands )->getOutput ();
96101
97102 return array_reduce ($ unitTypes , function ($ carry , $ unitSuffix ) use ($ output ) {
98103 $ result = Utils \OutputFetcher::fetchUnitNames ($ unitSuffix , $ output );
104+
99105 return array_merge ($ carry , $ result );
100106 }, []);
101107 }
102108
103109 /**
104110 * @param string $name
111+ *
105112 * @return Service
106113 */
107114 public function getService (string $ name ): Service
108115 {
109- return new Service ($ name , $ this ->getProcessBuilder ());
116+ return new Service ($ name , $ this ->getCommandDispatcher ());
110117 }
111118
112119 /**
113120 * @param null|string $unitPrefix
121+ *
114122 * @return Service[]
115123 */
116124 public function getServices (?string $ unitPrefix = null ): array
117125 {
118126 $ units = $ this ->listUnits ($ unitPrefix , [Service::UNIT ]);
119127
120128 return array_map (function ($ unitName ) {
121- return new Service ($ unitName , $ this ->getProcessBuilder ());
129+ return new Service ($ unitName , $ this ->getCommandDispatcher ());
122130 }, $ units );
123131 }
124132
125133 /**
126134 * @param string $name
135+ *
127136 * @return Timer
128137 */
129138 public function getTimer (string $ name ): Timer
130139 {
131- return new Timer ($ name , $ this ->getProcessBuilder ());
140+ return new Timer ($ name , $ this ->getCommandDispatcher ());
132141 }
133142
134143 /**
135144 * @param null|string $unitPrefix
145+ *
136146 * @return Timer[]
137147 */
138148 public function getTimers (?string $ unitPrefix = null ): array
139149 {
140150 $ units = $ this ->listUnits ($ unitPrefix , [Timer::UNIT ]);
141151
142152 return array_map (function ($ unitName ) {
143- return new Timer ($ unitName , $ this ->getProcessBuilder ());
153+ return new Timer ($ unitName , $ this ->getCommandDispatcher ());
144154 }, $ units );
145155 }
146156
147157 /**
148- * @return ProcessBuilder
158+ * Restart the daemon to reload specs and new units
159+ *
160+ * @return bool
149161 */
150- public function getProcessBuilder (): ProcessBuilder
162+ public function daemonReload (): bool
151163 {
152- $ builder = ProcessBuilder::create ();
153- $ builder ->setPrefix (self ::$ binary );
154- $ builder ->setTimeout (self ::$ timeout );
164+ return $ this ->getCommandDispatcher ()->dispatch ('daemon-reload ' )->isSuccessful ();
165+ }
155166
156- return $ builder ;
167+ /**
168+ * @return CommandDispatcherInterface
169+ */
170+ public function getCommandDispatcher (): CommandDispatcherInterface
171+ {
172+ if ($ this ->commandDispatcher === null ) {
173+ $ this ->commandDispatcher = (new SymfonyCommandDispatcher )
174+ ->setTimeout (self ::$ timeout )
175+ ->setBinary (self ::$ binary );
176+ }
177+
178+ return $ this ->commandDispatcher ;
157179 }
158180
159181 /**
160- * Restart the daemon to reload specs and new units
182+ * @param CommandDispatcherInterface $dispatcher
161183 *
162- * @return bool
184+ * @return SystemCtl
163185 */
164- public function daemonReload (): bool
186+ public function setCommandDispatcher ( CommandDispatcherInterface $ dispatcher )
165187 {
166- $ processBuilder = $ this ->getProcessBuilder ();
167- $ processBuilder ->add ('daemon-reload ' );
168-
169- $ process = $ processBuilder ->getProcess ();
170- $ process ->run ();
188+ $ this ->commandDispatcher = $ dispatcher
189+ ->setTimeout (self ::$ timeout )
190+ ->setBinary (self ::$ binary );
171191
172- return $ process -> isSuccessful () ;
192+ return $ this ;
173193 }
174194}
0 commit comments