Skip to content

Commit b6f09b3

Browse files
committed
Code style cleanup
1 parent 6719723 commit b6f09b3

22 files changed

Lines changed: 187 additions & 94 deletions

assets/unit-template.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php foreach ($sections as $section => $properties): ?>
22
[<?= $this->e($section); ?>]
33
<?php foreach ($properties as $property => $value): ?>
4-
<?= $this->e($property); ?>=<?= $this->e($value); ?>
4+
<?= $this->e($property); ?>=<?= $this->e($value); ?><?= $this->e("\n"); ?>
55
<?php endforeach; ?>
66

77
<?php endforeach; ?>

src/Command/SymfonyCommand.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getOutput(): string
4444
*/
4545
public function isSuccessful(): bool
4646
{
47-
return $this->process->isSuccessful();
47+
return \in_array($this->process->getExitCode(), self::VALID_EXITCODES, true);
4848
}
4949

5050
/**
@@ -54,9 +54,7 @@ public function run(): CommandInterface
5454
{
5555
$this->process->run();
5656

57-
$exitCode = $this->process->getExitCode();
58-
59-
if (!\in_array((int)$exitCode, self::VALID_EXITCODES, true)) {
57+
if (!$this->isSuccessful()) {
6058
throw new CommandFailedException($this->process->getErrorOutput());
6159
}
6260

src/Scope/ScopeInterface.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@ interface ScopeInterface
1616
*
1717
* @return string
1818
*/
19-
public function __toString(): string;
19+
public function getArgument(): string;
20+
21+
/**
22+
* @return string
23+
*/
24+
public function getName(): string;
2025
}

src/Scope/SystemScope.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ class SystemScope implements ScopeInterface
1414
/**
1515
* @inheritdoc
1616
*/
17-
public function __toString(): string
17+
public function getArgument(): string
1818
{
19-
return '--system';
19+
return '--' . $this->getName();
20+
}
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function getName(): string
26+
{
27+
return 'system';
2028
}
2129
}

src/Scope/UserScope.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ class UserScope implements ScopeInterface
1414
/**
1515
* @inheritdoc
1616
*/
17-
public function __toString(): string
17+
public function getArgument(): string
1818
{
19-
return '--user';
19+
return '--' . $this->getName();
20+
}
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function getName(): string
26+
{
27+
return 'user';
2028
}
2129
}

src/SystemCtl.php

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
use SystemCtl\Template\AbstractUnitTemplate;
1414
use SystemCtl\Template\Installer\UnitInstaller;
1515
use SystemCtl\Template\Installer\UnitInstallerInterface;
16+
use SystemCtl\Template\PathResolverInterface;
1617
use SystemCtl\Template\Renderer\PlatesRenderer;
18+
use SystemCtl\Unit\AbstractUnit;
1719
use SystemCtl\Unit\Service;
1820
use SystemCtl\Unit\Timer;
1921
use 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
}

src/Template/AbstractUnitTemplate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(string $unitName, string $unitSuffix)
4646
/**
4747
* @return AbstractSection
4848
*/
49-
abstract public function getTypeSpecificSection();
49+
abstract public function getTypeSpecificSection(): AbstractSection;
5050

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

src/Template/Installer/UnitInstaller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ public function getRenderer(): RendererInterface
5252
/**
5353
* @inheritDoc
5454
*/
55-
public function install(AbstractUnitTemplate $unitTemplate): bool
55+
public function install(AbstractUnitTemplate $unitTemplate, bool $overwrite = false): bool
5656
{
5757
$unitSuffix = $unitTemplate->getUnitSuffix();
5858
$unitName = $unitTemplate->getUnitName();
5959

6060
$targetFile = $this->path . DIRECTORY_SEPARATOR . $unitName . '.' . $unitSuffix;
6161

62-
if (\file_exists($targetFile)) {
62+
if (!$overwrite && \file_exists($targetFile)) {
6363
throw UnitFileExistsException::create($unitName, $unitSuffix);
6464
}
6565

src/Template/Installer/UnitInstallerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public function getRenderer(): RendererInterface;
3939
* Install a unit by template
4040
*
4141
* @param AbstractUnitTemplate $unitTemplate
42-
*
42+
* @param bool $overwrite
4343
* @return bool
4444
* @throw UnitNotInstalledException
4545
*/
46-
public function install(AbstractUnitTemplate $unitTemplate): bool;
46+
public function install(AbstractUnitTemplate $unitTemplate, bool $overwrite = false): bool;
4747
}

src/Template/PathResolver.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SystemCtl\Template;
5+
6+
use SystemCtl\Scope\ScopeInterface;
7+
8+
class PathResolver implements PathResolverInterface
9+
{
10+
public function __invoke(ScopeInterface $scope): string
11+
{
12+
13+
}
14+
}

0 commit comments

Comments
 (0)