Skip to content

Commit 0be6e58

Browse files
author
Andreas Frömer
committed
Dont know yet
1 parent 33f9d9e commit 0be6e58

12 files changed

+97
-40
lines changed

src/Scope/AbstractScope.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SystemCtl\Scope;
5+
6+
/**
7+
* AbstractScope
8+
*
9+
* @author icanhazstring <[email protected]>
10+
*/
11+
abstract class AbstractScope implements ScopeInterface
12+
{
13+
/**
14+
* @inheritdoc
15+
*/
16+
public function getArgument(): string
17+
{
18+
return '--' . $this->getName();
19+
}
20+
}

src/Scope/SystemScope.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,8 @@
99
* @package SystemCtl\Scope
1010
* @author icanhazstring <[email protected]>
1111
*/
12-
class SystemScope implements ScopeInterface
12+
class SystemScope extends AbstractScope
1313
{
14-
/**
15-
* @inheritdoc
16-
*/
17-
public function getArgument(): string
18-
{
19-
return '--' . $this->getName();
20-
}
21-
2214
/**
2315
* @inheritdoc
2416
*/

src/Scope/UserScope.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,8 @@
99
* @package SystemCtl\Scope
1010
* @author icanhazstring <[email protected]>
1111
*/
12-
class UserScope implements ScopeInterface
12+
class UserScope extends AbstractScope
1313
{
14-
/**
15-
* @inheritdoc
16-
*/
17-
public function getArgument(): string
18-
{
19-
return '--' . $this->getName();
20-
}
21-
2214
/**
2315
* @inheritdoc
2416
*/

src/SystemCtl.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
use SystemCtl\Scope\ScopeInterface;
1111
use SystemCtl\Scope\SystemScope;
1212
use SystemCtl\Scope\UserScope;
13-
use SystemCtl\Template\AbstractUnitTemplate;
1413
use SystemCtl\Template\Installer\UnitInstaller;
1514
use SystemCtl\Template\Installer\UnitInstallerInterface;
1615
use SystemCtl\Template\PathResolverInterface;
1716
use SystemCtl\Template\Renderer\PlatesRenderer;
17+
use SystemCtl\Template\UnitTemplateInterface;
1818
use SystemCtl\Unit\AbstractUnit;
1919
use SystemCtl\Unit\Service;
2020
use 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());

src/Template/AbstractUnitTemplate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Defines basic properties for a unit
1515
*
1616
* @package SystemCtl\Template
17-
* @author icanhazstring <[email protected]>
17+
* @author icanhazstring <[email protected]>
1818
*/
1919
abstract class AbstractUnitTemplate
2020
{
@@ -46,7 +46,7 @@ public function __construct(string $unitName, string $unitSuffix)
4646
/**
4747
* @return AbstractSection
4848
*/
49-
abstract public function getTypeSpecificSection(): AbstractSection;
49+
abstract protected function getTypeSpecificSection(): AbstractSection;
5050

5151
/**
5252
* Get all definitions for this template as array

src/Template/Installer/UnitInstaller.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use SystemCtl\Exception\UnitFileExistsException;
77
use SystemCtl\Template\AbstractUnitTemplate;
88
use SystemCtl\Template\RendererInterface;
9+
use SystemCtl\Template\UnitTemplateInterface;
910

1011
/**
1112
* UnitInstaller
@@ -52,7 +53,7 @@ public function getRenderer(): RendererInterface
5253
/**
5354
* @inheritDoc
5455
*/
55-
public function install(AbstractUnitTemplate $unitTemplate, bool $overwrite = false): bool
56+
public function install(UnitTemplateInterface $unitTemplate, bool $overwrite = false): bool
5657
{
5758
$unitSuffix = $unitTemplate->getUnitSuffix();
5859
$unitName = $unitTemplate->getUnitName();

src/Template/Installer/UnitInstallerInterface.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use SystemCtl\Template\AbstractUnitTemplate;
77
use SystemCtl\Template\RendererInterface;
8+
use SystemCtl\Template\UnitTemplateInterface;
89

910
/**
1011
* UnitInstallerInterface
@@ -38,10 +39,10 @@ public function getRenderer(): RendererInterface;
3839
/**
3940
* Install a unit by template
4041
*
41-
* @param AbstractUnitTemplate $unitTemplate
42-
* @param bool $overwrite
42+
* @param UnitTemplateInterface $unitTemplate
43+
* @param bool $overwrite
4344
* @return bool
4445
* @throw UnitNotInstalledException
4546
*/
46-
public function install(AbstractUnitTemplate $unitTemplate, bool $overwrite = false): bool;
47+
public function install(UnitTemplateInterface $unitTemplate, bool $overwrite = false): bool;
4748
}

src/Template/Renderer/PlatesRenderer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
use League\Plates;
77
use SystemCtl\Template\RendererInterface;
8-
use SystemCtl\Template\AbstractUnitTemplate;
8+
use SystemCtl\Template\UnitTemplateInterface;
99

1010
/**
1111
* PlatesRenderer
1212
*
1313
* Wrapper for plates to be used with RendererInterface
1414
*
1515
* @package SystemCtl\Template
16-
* @author icanhazstring <[email protected]>
16+
* @author icanhazstring <[email protected]>
1717
*/
1818
class PlatesRenderer implements RendererInterface
1919
{
@@ -34,7 +34,7 @@ public function __construct(string $directory, string $extension = 'tpl')
3434
/**
3535
* @inheritdoc
3636
*/
37-
public function render(string $templateFile, AbstractUnitTemplate $unitTemplate): string
37+
public function render(string $templateFile, UnitTemplateInterface $unitTemplate): string
3838
{
3939
return $this->engine->render($templateFile, ['sections' => $unitTemplate->getSections()]);
4040
}

src/Template/RendererInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ interface RendererInterface
1414
/**
1515
* Render a named template with given data
1616
*
17-
* @param string $templateFile
18-
* @param AbstractUnitTemplate $unitTemplate
17+
* @param string $templateFile
18+
* @param UnitTemplateInterface $unitTemplate
1919
*
2020
* @return string
2121
*/
22-
public function render(string $templateFile, AbstractUnitTemplate $unitTemplate): string;
22+
public function render(string $templateFile, UnitTemplateInterface $unitTemplate): string;
2323
}

src/Template/ServiceUnitTemplate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function getServiceSection(): ServiceSection
4141
/**
4242
* @inheritDoc
4343
*/
44-
public function getTypeSpecificSection(): AbstractSection
44+
protected function getTypeSpecificSection(): AbstractSection
4545
{
4646
return $this->serviceSection;
4747
}

0 commit comments

Comments
 (0)