Skip to content

Commit 607dca9

Browse files
author
Andreas Frömer
committed
Move units and introduce multi instance
The units where moved to a more specific namespace. Also a multi instance capability was introduced to get the full unit name and the instance of it.
1 parent 5a9cf56 commit 607dca9

File tree

8 files changed

+152
-44
lines changed

8 files changed

+152
-44
lines changed

src/SystemCtl.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,57 @@
44

55
use Symfony\Component\Process\ProcessBuilder;
66
use SystemCtl\Exception\UnitTypeNotSupportedException;
7+
use SystemCtl\Unit\Service;
8+
use SystemCtl\Unit\Timer;
9+
use SystemCtl\Unit\UnitInterface;
710

811
class SystemCtl
912
{
1013
/** @var string systemctl binary path */
1114
private static $binary = '/bin/systemctl';
1215

13-
/** @var bool */
14-
private static $sudo = false;
16+
/** @var int timeout for commands */
17+
private static $timeout = 3;
1518

1619
public const AVAILABLE_UNITS = [
17-
'service',
20+
Service::UNIT,
1821
'socket',
1922
'device',
2023
'mount',
2124
'automount',
2225
'swap',
2326
'target',
2427
'path',
25-
'timer',
28+
Timer::UNIT,
2629
'slice',
2730
'scope'
2831
];
2932

3033
public const SUPPORTED_UNITS = [
31-
'service',
32-
'timer'
34+
Service::UNIT,
35+
Timer::UNIT,
3336
];
3437

3538
/**
39+
* Change systemctl binary
40+
*
3641
* @param string $binary
3742
*/
3843
public static function setBinary(string $binary): void
3944
{
4045
self::$binary = $binary;
4146
}
4247

48+
/**
49+
* Change command execution timeout
50+
*
51+
* @param int $timeout
52+
*/
53+
public static function setTimeout(int $timeout): void
54+
{
55+
self::$timeout = $timeout;
56+
}
57+
4358
/**
4459
* @param string $unitSuffix
4560
* @param string $unitName
@@ -49,13 +64,13 @@ public static function setBinary(string $binary): void
4964
*/
5065
public static function unitFromSuffix(string $unitSuffix, string $unitName): UnitInterface
5166
{
52-
$unitClass = 'SystemCtl\\' . ucfirst($unitSuffix);
67+
$unitClass = 'SystemCtl\\Unit\\' . ucfirst($unitSuffix);
5368

5469
if (!class_exists($unitClass)) {
5570
throw new UnitTypeNotSupportedException('Unit type ' . $unitSuffix . ' not supported');
5671
}
5772

58-
return new $unitClass($unitName, new ProcessBuilder(['sudo', self::$binary]));
73+
return new $unitClass($unitName, new ProcessBuilder([self::$binary]));
5974
}
6075

6176
/**
@@ -100,7 +115,7 @@ public function getService(string $name): Service
100115
*/
101116
public function getServices(?string $unitPrefix = null): array
102117
{
103-
$units = $this->listUnits($unitPrefix, ['service']);
118+
$units = $this->listUnits($unitPrefix, [Service::UNIT]);
104119

105120
return array_map(function ($unitName) {
106121
return new Service($unitName, $this->getProcessBuilder());
@@ -122,7 +137,7 @@ public function getTimer(string $name): Timer
122137
*/
123138
public function getTimers(?string $unitPrefix = null): array
124139
{
125-
$units = $this->listUnits($unitPrefix, ['timer']);
140+
$units = $this->listUnits($unitPrefix, [Timer::UNIT]);
126141

127142
return array_map(function ($unitName) {
128143
return new Timer($unitName, $this->getProcessBuilder());
@@ -136,7 +151,7 @@ public function getProcessBuilder(): ProcessBuilder
136151
{
137152
$builder = ProcessBuilder::create();
138153
$builder->setPrefix(self::$binary);
139-
$builder->setTimeout(3);
154+
$builder->setTimeout(self::$timeout);
140155

141156
return $builder;
142157
}
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace SystemCtl;
3+
namespace SystemCtl\Unit;
44

55
use Symfony\Component\Process\ProcessBuilder;
66

@@ -24,11 +24,31 @@ public function __construct(string $name, ProcessBuilder $processBuilder)
2424
$this->processBuilder = $processBuilder;
2525
}
2626

27+
/**
28+
* @inheritdoc
29+
*/
2730
public function getName(): string
2831
{
2932
return $this->name;
3033
}
3134

35+
/**
36+
* @inheritDoc
37+
*/
38+
public function isMultiInstance(): bool
39+
{
40+
return strpos($this->name, '@') !== false;
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function getInstanceName(): ?string
47+
{
48+
$parts = explode('@', $this->name);
49+
return $parts[1] ?? null;
50+
}
51+
3252
/**
3353
* Execute a single command
3454
*

src/Service.php renamed to src/Unit/Service.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

33

4-
namespace SystemCtl;
4+
namespace SystemCtl\Unit;
55

66
use SystemCtl\Exception\CommandFailedException;
77

88
class Service extends AbstractUnit
99
{
10+
public const UNIT = 'service';
11+
1012
protected function execute(string $command): bool
1113
{
1214
$process = $this->processBuilder

src/Timer.php renamed to src/Unit/Timer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

33

4-
namespace SystemCtl;
4+
namespace SystemCtl\Unit;
55

66
use SystemCtl\Exception\CommandFailedException;
77

88
class Timer extends AbstractUnit
99
{
10+
public const UNIT = 'timer';
11+
1012
protected function execute(string $command): bool
1113
{
1214
$process = $this->processBuilder

src/Unit/UnitInterface.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace SystemCtl\Unit;
4+
5+
/**
6+
* UnitInterface for handling single units
7+
*
8+
* @package SystemCtl
9+
*/
10+
interface UnitInterface
11+
{
12+
/**
13+
* Get the units full name
14+
*
15+
* @return string
16+
*/
17+
public function getName(): string;
18+
19+
/**
20+
* Whether the unit has multiple instances or not
21+
*
22+
* @return bool
23+
*/
24+
public function isMultiInstance(): bool;
25+
26+
/**
27+
* Get instance name
28+
*
29+
* @return null|string
30+
*/
31+
public function getInstanceName(): ?string;
32+
33+
/**
34+
* Start command
35+
*
36+
* @return bool
37+
*/
38+
public function start(): bool;
39+
40+
/**
41+
* Stop command
42+
*
43+
* @return bool
44+
*/
45+
public function stop(): bool;
46+
47+
/**
48+
* Disable command
49+
*
50+
* @return bool
51+
*/
52+
public function disable(): bool;
53+
54+
/**
55+
* Reload command
56+
*
57+
* @return bool
58+
*/
59+
public function reload(): bool;
60+
61+
/**
62+
* Restart command
63+
*
64+
* @return bool
65+
*/
66+
public function restart(): bool;
67+
68+
/**
69+
* Enable command
70+
*
71+
* @return bool
72+
*/
73+
public function enable(): bool;
74+
}

src/UnitInterface.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/SystemCtlTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use Symfony\Component\Process\Process;
77
use Symfony\Component\Process\ProcessBuilder;
88
use SystemCtl\Exception\UnitTypeNotSupportedException;
9-
use SystemCtl\Service;
9+
use SystemCtl\Unit\Service;
1010
use SystemCtl\SystemCtl;
11-
use SystemCtl\Timer;
12-
use SystemCtl\UnitInterface;
11+
use SystemCtl\Unit\Timer;
12+
use SystemCtl\Unit\UnitInterface;
1313

1414
class SystemCtlTest extends TestCase
1515
{
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22

3-
namespace SystemCtl\Test;
3+
namespace SystemCtl\Test\Unit;
44

55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\Process\Process;
77
use Symfony\Component\Process\ProcessBuilder;
88
use SystemCtl\Exception\CommandFailedException;
99
use SystemCtl\SystemCtl;
10+
use SystemCtl\Unit\Service;
1011

1112
class UnitTest extends TestCase
1213
{
@@ -127,8 +128,27 @@ public function testTimerCommandsIfProcessIsUnsuccessFulShouldRaiseException()
127128

128129
$timer = $systemctl->getTimer('AwesomeTimer');
129130

130-
$process->method('isSuccessful')->willReturn(false);
131131
$this->expectException(CommandFailedException::class);
132132
$timer->start();
133133
}
134+
135+
public function testMultiInstanceUnit()
136+
{
137+
$process = $this->getMockBuilder(Process::class)
138+
->disableOriginalConstructor()
139+
->getMock();
140+
141+
/** @var \PHPUnit_Framework_MockObject_MockObject|ProcessBuilder $processBuilder */
142+
$processBuilder = $this->getMockBuilder(ProcessBuilder::class)
143+
->disableOriginalConstructor()
144+
->setMethods(['getProcess'])
145+
->getMock();
146+
147+
$processBuilder->method('getProcess')->willReturn($process);
148+
149+
$unit = new Service('service@1', $processBuilder);
150+
$this->assertEquals('service@1', $unit->getName());
151+
$this->assertTrue($unit->isMultiInstance());
152+
$this->assertEquals('1', $unit->getInstanceName());
153+
}
134154
}

0 commit comments

Comments
 (0)