Skip to content

Commit cfcc9cc

Browse files
committed
Merge branch 'master' into 1.0
2 parents f1339a5 + 34e752c commit cfcc9cc

22 files changed

+565
-235
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ PHP wrapper for systemctl (PHP7.1)
1818
- disable
1919
- reload
2020
- restart
21+
- isEnabled
22+
- isActive
2123

2224
> If you like to add support for more commands, feel free to contribute.
2325

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
},
1515
"autoload": {
1616
"psr-4": {
17-
"SystemCtl\\": "src/"
17+
"SystemCtl\\": [
18+
"src/"
19+
]
1820
}
1921
},
2022
"autoload-dev": {
2123
"psr-4": {
22-
"SystemCtl\\Test\\": "tests/"
24+
"SystemCtl\\Tests\\": [
25+
"tests/"
26+
]
2327
}
2428
},
2529
"license": "MIT",

phpunit.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
bootstrap="./vendor/autoload.php"
1313
>
1414
<testsuites>
15-
<testsuite name="unittests">
16-
<directory>./tests/</directory>
15+
<testsuite name="unit-tests">
16+
<directory>./tests/Unit</directory>
17+
</testsuite>
18+
<testsuite name="integration-tests">
19+
<directory>./tests/Integration</directory>
1720
</testsuite>
1821
</testsuites>
1922

@@ -26,4 +29,4 @@
2629
</whitelist>
2730
</filter>
2831

29-
</phpunit>
32+
</phpunit>

src/Unit/AbstractUnit.php

Lines changed: 77 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22

33
namespace SystemCtl\Unit;
44

5+
use Symfony\Component\Process\Process;
56
use Symfony\Component\Process\ProcessBuilder;
67
use SystemCtl\Exception\CommandFailedException;
78

89
abstract class AbstractUnit implements UnitInterface
910
{
1011
/** @var string */
11-
protected $type;
12+
private $name;
1213

1314
/** @var string */
14-
private $name;
15+
private $type;
1516

1617
/** @var ProcessBuilder */
17-
protected $processBuilder;
18+
private $processBuilder;
19+
20+
/** @var bool */
21+
private $yell = false;
1822

1923
/**
2024
* Create new service with given name
@@ -38,6 +42,16 @@ public function getName(): string
3842
return $this->name;
3943
}
4044

45+
/**
46+
* Set a flag whether to yell with an exception if an command failes or not
47+
*
48+
* @param $state
49+
*/
50+
public function yell($state)
51+
{
52+
$this->yell = $state;
53+
}
54+
4155
/**
4256
* @inheritDoc
4357
*/
@@ -48,33 +62,35 @@ public function isMultiInstance(): bool
4862

4963
/**
5064
* @inheritDoc
65+
* @todo: Everything in here should happen inside the constructor and stored
66+
* afterwards.
5167
*/
5268
public function getInstanceName(): ?string
5369
{
54-
$parts = explode('@', $this->name);
55-
return $parts[1] ?? null;
70+
$instanceName = explode('@', $this->name, 2)[1] ?? null;
71+
72+
if (is_string($instanceName) && strpos($instanceName, '.') !== false) {
73+
$instanceName = explode('.', $instanceName, 2)[0];
74+
}
75+
76+
return $instanceName;
5677
}
5778

5879
/**
5980
* Execute a single command
6081
*
6182
* @param string $command
62-
* @param bool $raise
6383
*
6484
* @throws CommandFailedException Raised if process was not successful and user wants to raise exception
6585
* instead of returning the actual process exit code
6686
*
6787
* @return bool
6888
*/
69-
protected function execute(string $command, bool $raise = true): bool
89+
protected function execute(string $command): bool
7090
{
71-
$process = $this->processBuilder
72-
->setArguments([$command, $this->getName()])
73-
->getProcess();
91+
$process = $this->runCommandAgainstService($command);
7492

75-
$process->run();
76-
77-
if (!$process->isSuccessful() && $raise) {
93+
if (!$process->isSuccessful() && $this->yell) {
7894
$exceptionCall = CommandFailedException::class . '::from' . ucfirst($this->type);
7995
throw call_user_func_array($exceptionCall, [$this->getName(), $command]);
8096
}
@@ -83,88 +99,94 @@ protected function execute(string $command, bool $raise = true): bool
8399
}
84100

85101
/**
86-
* @inheritdoc
102+
* @param string $command
103+
*
104+
* @return Process
87105
*/
88-
public function start(bool $raise = false): bool
106+
protected function runCommandAgainstService(string $command): Process
89107
{
90-
return $this->execute(__FUNCTION__, $raise);
108+
$process = $this->processBuilder
109+
->setArguments([$command, $this->getName()])
110+
->getProcess();
111+
112+
$process->run();
113+
114+
return $process;
91115
}
92116

93117
/**
94-
* @inheritdoc
118+
* @return bool
95119
*/
96-
public function stop(bool $raise = false): bool
120+
public function start(): bool
97121
{
98-
return $this->execute(__FUNCTION__, $raise);
122+
return $this->execute(__FUNCTION__);
99123
}
100124

101125
/**
102-
* @inheritdoc
126+
* @return bool
103127
*/
104-
public function disable(bool $raise = false): bool
128+
public function stop(): bool
105129
{
106-
return $this->execute(__FUNCTION__, $raise);
130+
return $this->execute(__FUNCTION__);
107131
}
108132

109133
/**
110-
* @inheritdoc
134+
* @return bool
111135
*/
112-
public function reload(bool $raise = false): bool
136+
public function disable(): bool
113137
{
114-
return $this->execute(__FUNCTION__, $raise);
138+
return $this->execute(__FUNCTION__);
115139
}
116140

117141
/**
118-
* @inheritdoc
142+
* @return bool
119143
*/
120-
public function restart(bool $raise = false): bool
144+
public function reload(): bool
121145
{
122-
return $this->execute(__FUNCTION__, $raise);
146+
return $this->execute(__FUNCTION__);
123147
}
124148

125149
/**
126-
* @inheritdoc
150+
* @return bool
127151
*/
128-
public function enable(bool $raise = false): bool
152+
public function restart(): bool
129153
{
130-
return $this->execute(__FUNCTION__, $raise);
154+
return $this->execute(__FUNCTION__);
131155
}
132156

133157
/**
134-
* @inheritdoc
158+
* @return bool
135159
*/
136-
public function isActive(bool $raise = false): bool
160+
public function enable(): bool
137161
{
138-
$process = $this->processBuilder
139-
->setArguments(['is-active', $this->getName()])
140-
->getProcess();
141-
142-
$process->run();
162+
return $this->execute(__FUNCTION__);
163+
}
143164

144-
if (!$process->isSuccessful() && $raise) {
145-
$exceptionCall = CommandFailedException::class . '::from' . ucfirst($this->type);
146-
throw call_user_func_array($exceptionCall, [$this->getName(), 'is-active']);
147-
}
165+
/**
166+
* @return bool
167+
*/
168+
public function isEnabled(): bool
169+
{
170+
$process = $this->runCommandAgainstService('is-enabled');
148171

149-
return trim($process->getOutput()) === 'active';
172+
return $process->isSuccessful() && trim($process->getOutput()) === 'enabled';
150173
}
151174

152175
/**
153-
* @inheritDoc
176+
* @return bool
154177
*/
155-
public function isEnabled(bool $raise = false): bool
178+
public function isActive(): bool
156179
{
157-
$process = $this->processBuilder
158-
->setArguments(['is-enabled', $this->getName()])
159-
->getProcess();
160-
161-
$process->run();
180+
$process = $this->runCommandAgainstService('is-active');
162181

163-
if (!$process->isSuccessful() && $raise) {
164-
$exceptionCall = CommandFailedException::class . '::from' . ucfirst($this->type);
165-
throw call_user_func_array($exceptionCall, [$this->getName(), 'is-enabled']);
166-
}
182+
return $process->isSuccessful() && trim($process->getOutput()) === 'active';
183+
}
167184

168-
return trim($process->getOutput()) === 'enabled';
185+
/**
186+
* @return bool
187+
*/
188+
public function isRunning(): bool
189+
{
190+
return $this->isActive();
169191
}
170192
}

src/Unit/Service.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class Service extends AbstractUnit
88
{
9+
/**
10+
* @var string
11+
*/
912
public const UNIT = 'service';
1013

1114
/**

src/Unit/Timer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class Timer extends AbstractUnit
88
{
9+
/**
10+
* @var string
11+
*/
912
public const UNIT = 'timer';
1013

1114
/**

src/Unit/UnitInterface.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,72 +33,56 @@ public function getInstanceName(): ?string;
3333
/**
3434
* Start command
3535
*
36-
* @param bool $raise Raise exception on failure instead of process result
37-
*
3836
* @return bool
3937
*/
40-
public function start(bool $raise = false): bool;
38+
public function start(): bool;
4139

4240
/**
4341
* Stop command
4442
*
45-
* @param bool $raise Raise exception on failure instead of process result
46-
*
4743
* @return bool
4844
*/
49-
public function stop(bool $raise = false): bool;
45+
public function stop(): bool;
5046

5147
/**
5248
* Disable command
5349
*
54-
* @param bool $raise Raise exception on failure instead of process result
55-
*
5650
* @return bool
5751
*/
58-
public function disable(bool $raise = false): bool;
52+
public function disable(): bool;
5953

6054
/**
6155
* Reload command
6256
*
63-
* @param bool $raise Raise exception on failure instead of process result
64-
*
6557
* @return bool
6658
*/
67-
public function reload(bool $raise = false): bool;
59+
public function reload(): bool;
6860

6961
/**
7062
* Restart command
7163
*
72-
* @param bool $raise Raise exception on failure instead of process result
73-
*
7464
* @return bool
7565
*/
76-
public function restart(bool $raise = false): bool;
66+
public function restart(): bool;
7767

7868
/**
7969
* Enable command
8070
*
81-
* @param bool $raise Raise exception on failure instead of process result
82-
*
8371
* @return bool
8472
*/
85-
public function enable(bool $raise = false): bool;
73+
public function enable(): bool;
8674

8775
/**
8876
* Check whether unit is active
8977
*
90-
* @param bool $raise Raise exception on failure instead of process result
91-
*
9278
* @return bool
9379
*/
94-
public function isActive(bool $raise = false): bool;
80+
public function isActive(): bool;
9581

9682
/**
9783
* Check whether unit is enabled
9884
*
99-
* @param bool $raise Raise exception on failure instead of process result
100-
*
10185
* @return bool
10286
*/
103-
public function isEnabled(bool $raise = false): bool;
87+
public function isEnabled(): bool;
10488
}

0 commit comments

Comments
 (0)