1313use SystemCtl \Template \AbstractUnitTemplate ;
1414use SystemCtl \Template \Installer \UnitInstaller ;
1515use SystemCtl \Template \Installer \UnitInstallerInterface ;
16+ use SystemCtl \Template \PathResolverInterface ;
1617use SystemCtl \Template \Renderer \PlatesRenderer ;
18+ use SystemCtl \Unit \AbstractUnit ;
1719use SystemCtl \Unit \Service ;
1820use SystemCtl \Unit \Timer ;
1921use SystemCtl \Unit \UnitInterface ;
@@ -32,12 +34,17 @@ class SystemCtl
3234 /** @var int timeout for commands */
3335 private static $ timeout = 3 ;
3436
35- /** @var string install path for new units */
36- private static $ installPath = '/etc/systemd/system ' ;
37-
3837 /** @var string */
3938 private static $ assetPath = __DIR__ . '/../assets ' ;
4039
40+ private const INSTALL_PATHS = [
41+ 'system ' => '/etc/systemd/system/ ' ,
42+ 'user ' => '~/.config/systemd/user/ '
43+ ];
44+
45+ /** @var PathResolverInterface */
46+ private $ pathResolver ;
47+
4148 /** @var CommandDispatcherInterface */
4249 private $ commandDispatcher ;
4350
@@ -63,7 +70,7 @@ class SystemCtl
6370
6471 public const SUPPORTED_UNITS = [
6572 Service::UNIT ,
66- Timer::UNIT ,
73+ Timer::UNIT
6774 ];
6875
6976 /**
@@ -86,16 +93,6 @@ public static function setTimeout(int $timeout): void
8693 self ::$ timeout = $ timeout ;
8794 }
8895
89- /**
90- * Change install path for units
91- *
92- * @param string $installPath
93- */
94- public static function setInstallPath (string $ installPath ): void
95- {
96- self ::$ installPath = $ installPath ;
97- }
98-
9996 /**
10097 * Change asset path
10198 *
@@ -255,17 +252,35 @@ public function getCommandDispatcher(): CommandDispatcherInterface
255252 ->setBinary (self ::$ binary );
256253 }
257254
258- $ this ->commandDispatcher ->setArguments ([( string ) $ this ->getScope ()]);
255+ $ this ->commandDispatcher ->setArguments ([$ this ->getScope ()-> getArgument ()]);
259256
260257 return $ this ->commandDispatcher ;
261258 }
262259
260+ /**
261+ * @return PathResolverInterface
262+ */
263+ public function getPathResolver (): PathResolverInterface
264+ {
265+ return $ this ->pathResolver ;
266+ }
267+
268+ /**
269+ * @param PathResolverInterface $pathResolver
270+ * @return SystemCtl
271+ */
272+ public function setPathResolver (PathResolverInterface $ pathResolver ): SystemCtl
273+ {
274+ $ this ->pathResolver = $ pathResolver ;
275+ return $ this ;
276+ }
277+
263278 /**
264279 * @param CommandDispatcherInterface $dispatcher
265280 *
266281 * @return SystemCtl
267282 */
268- public function setCommandDispatcher (CommandDispatcherInterface $ dispatcher )
283+ public function setCommandDispatcher (CommandDispatcherInterface $ dispatcher ): SystemCtl
269284 {
270285 $ this ->commandDispatcher = $ dispatcher
271286 ->setTimeout (self ::$ timeout )
@@ -281,7 +296,7 @@ public function getUnitInstaller(): UnitInstallerInterface
281296 {
282297 if ($ this ->unitInstaller === null ) {
283298 $ this ->unitInstaller = (new UnitInstaller )
284- ->setPath (self ::$ installPath )
299+ ->setPath (self ::INSTALL_PATHS [ $ this -> getScope ()-> getName ()] )
285300 ->setRenderer (new PlatesRenderer (self ::$ assetPath ));
286301 }
287302
@@ -306,22 +321,26 @@ public function setUnitInstaller(UnitInstallerInterface $unitInstaller): self
306321 * Install a given template, reload the daemon and return the freshly installed unit.
307322 *
308323 * @param AbstractUnitTemplate $unitTemplate
324+ * @param bool $overwrite
309325 *
310326 * @return UnitInterface
311- * @throws UnitTypeNotSupportedException
312327 */
313- public function install (AbstractUnitTemplate $ unitTemplate ): UnitInterface
328+ public function install (AbstractUnitTemplate $ unitTemplate, bool $ overwrite = false ): UnitInterface
314329 {
315330 $ unitSuffix = $ unitTemplate ->getUnitSuffix ();
316331 $ unitName = $ unitTemplate ->getUnitName ();
317332
318- if (!in_array ($ unitSuffix , self ::SUPPORTED_UNITS )) {
333+ if (!\ in_array ($ unitSuffix , self ::SUPPORTED_UNITS , true )) {
319334 throw UnitTypeNotSupportedException::create ($ unitSuffix );
320335 }
321336
322- $ this ->getUnitInstaller ()->install ($ unitTemplate );
337+ $ this ->getUnitInstaller ()->install ($ unitTemplate , $ overwrite );
338+
339+ $ unit = AbstractUnit::byType ($ unitSuffix , $ unitName , $ this ->getCommandDispatcher ());
340+ $ unit ->enable ();
341+
323342 $ this ->daemonReload ();
324343
325- return $ this ->{ ' get ' . ucfirst ( $ unitSuffix )}( $ unitName ) ;
344+ return $ unit ;
326345 }
327346}
0 commit comments