1010use SystemCtl \Scope \ScopeInterface ;
1111use SystemCtl \Scope \SystemScope ;
1212use SystemCtl \Scope \UserScope ;
13- use SystemCtl \Template \AbstractUnitTemplate ;
1413use SystemCtl \Template \Installer \UnitInstaller ;
1514use SystemCtl \Template \Installer \UnitInstallerInterface ;
1615use SystemCtl \Template \PathResolverInterface ;
1716use SystemCtl \Template \Renderer \PlatesRenderer ;
17+ use SystemCtl \Template \UnitTemplateInterface ;
1818use SystemCtl \Unit \AbstractUnit ;
1919use SystemCtl \Unit \Service ;
2020use SystemCtl \Unit \Timer ;
@@ -39,7 +39,7 @@ class SystemCtl
3939
4040 private const INSTALL_PATHS = [
4141 'system ' => '/etc/systemd/system/ ' ,
42- 'user ' => '~/.config/systemd/user/ '
42+ 'user ' => '~/.config/systemd/user/ '
4343 ];
4444
4545 /** @var PathResolverInterface */
@@ -54,6 +54,18 @@ class SystemCtl
5454 /** @var ScopeInterface */
5555 private $ scope ;
5656
57+ /**
58+ * Hold scope instace with configured values
59+ * @var ScopeInterface
60+ */
61+ private $ userScope ;
62+
63+ /**
64+ * Hold scope instance with configured values
65+ * @var ScopeInterface
66+ */
67+ private $ systemScope ;
68+
5769 public const AVAILABLE_UNITS = [
5870 Service::UNIT ,
5971 'socket ' ,
@@ -143,7 +155,7 @@ public function listUnits(?string $unitPrefix = null, array $unitTypes = self::S
143155 public function getScope (): ScopeInterface
144156 {
145157 if ($ this ->scope === null ) {
146- $ this ->scope = new SystemScope ;
158+ $ this ->scope = $ this -> system () ;
147159 }
148160
149161 return $ this ->scope ;
@@ -156,7 +168,11 @@ public function getScope(): ScopeInterface
156168 */
157169 public function user (): self
158170 {
159- $ this ->scope = new UserScope ;
171+ if ($ this ->userScope === null ) {
172+ $ this ->userScope = new UserScope ;
173+ }
174+
175+ $ this ->scope = $ this ->userScope ;
160176
161177 return $ this ;
162178 }
@@ -166,7 +182,11 @@ public function user(): self
166182 */
167183 public function system (): self
168184 {
169- $ this ->scope = new SystemScope ;
185+ if ($ this ->systemScope === null ) {
186+ $ this ->systemScope = new SystemScope ;
187+ }
188+
189+ $ this ->scope = $ this ->systemScope ;
170190
171191 return $ this ;
172192 }
@@ -272,6 +292,7 @@ public function getPathResolver(): PathResolverInterface
272292 public function setPathResolver (PathResolverInterface $ pathResolver ): SystemCtl
273293 {
274294 $ this ->pathResolver = $ pathResolver ;
295+
275296 return $ this ;
276297 }
277298
@@ -320,12 +341,12 @@ public function setUnitInstaller(UnitInstallerInterface $unitInstaller): self
320341 /**
321342 * Install a given template, reload the daemon and return the freshly installed unit.
322343 *
323- * @param AbstractUnitTemplate $unitTemplate
324- * @param bool $overwrite
344+ * @param UnitTemplateInterface $unitTemplate
345+ * @param bool $overwrite
325346 *
326347 * @return UnitInterface
327348 */
328- public function install (AbstractUnitTemplate $ unitTemplate , bool $ overwrite = false ): UnitInterface
349+ public function install (UnitTemplateInterface $ unitTemplate , bool $ overwrite = false ): UnitInterface
329350 {
330351 $ unitSuffix = $ unitTemplate ->getUnitSuffix ();
331352 $ unitName = $ unitTemplate ->getUnitName ();
@@ -334,6 +355,7 @@ public function install(AbstractUnitTemplate $unitTemplate, bool $overwrite = fa
334355 throw UnitTypeNotSupportedException::create ($ unitSuffix );
335356 }
336357
358+ // TODO: set path to unit installer with current scope
337359 $ this ->getUnitInstaller ()->install ($ unitTemplate , $ overwrite );
338360
339361 $ unit = AbstractUnit::byType ($ unitSuffix , $ unitName , $ this ->getCommandDispatcher ());
0 commit comments