|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of RoboSystemService. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Aitor García Martínez (Falc) <[email protected]> |
|
6
|
|
|
* @copyright 2015 Aitor García Martínez (Falc) <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author Polyvaniy Oleksii (alexndlm) <[email protected]> |
|
9
|
|
|
* @copyright 2016 Polyvaniy Oleksii (alexndlm) <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @license MIT |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Falc\Robo\Service\CommandBuilder; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Systemd command builder. |
|
18
|
|
|
*/ |
|
19
|
|
|
class SystemdCommandBuilder implements CommandBuilderInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Command action. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $action; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Option list. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string[] |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $options = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Service. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $service; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
3 |
|
public function start($service) |
|
46
|
|
|
{ |
|
47
|
3 |
|
if (empty($service)) { |
|
48
|
1 |
|
throw new \InvalidArgumentException('No service selected to be started'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
$this->service = $service; |
|
52
|
2 |
|
$this->action = 'start'; |
|
53
|
|
|
|
|
54
|
2 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
2 |
View Code Duplication |
public function stop($service) |
|
61
|
|
|
{ |
|
62
|
2 |
|
if (empty($service)) { |
|
63
|
1 |
|
throw new \InvalidArgumentException('No service selected to be started'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
$this->service = $service; |
|
67
|
1 |
|
$this->action = 'stop'; |
|
68
|
|
|
|
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function restart($service) |
|
76
|
|
|
{ |
|
77
|
2 |
|
if (empty($service)) { |
|
78
|
1 |
|
throw new \InvalidArgumentException('No service selected to be started'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$this->service = $service; |
|
82
|
1 |
|
$this->action = 'restart'; |
|
83
|
|
|
|
|
84
|
1 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function enable($service) |
|
91
|
|
|
{ |
|
92
|
2 |
|
if (empty($service)) { |
|
93
|
1 |
|
throw new \InvalidArgumentException('No service selected to be enabled'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
$this->service = $service; |
|
97
|
1 |
|
$this->action = 'enable'; |
|
98
|
|
|
|
|
99
|
1 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
2 |
View Code Duplication |
public function disable($service) |
|
106
|
|
|
{ |
|
107
|
2 |
|
if (empty($service)) { |
|
108
|
1 |
|
throw new \InvalidArgumentException('No service selected to be disabled'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
$this->service = $service; |
|
112
|
1 |
|
$this->action = 'disable'; |
|
113
|
|
|
|
|
114
|
1 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function daemonReload() |
|
121
|
|
|
{ |
|
122
|
1 |
|
$this->action = 'daemon-reload'; |
|
123
|
|
|
|
|
124
|
1 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritdoc} |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function quiet() |
|
131
|
|
|
{ |
|
132
|
1 |
|
$this->options[] = '-q'; |
|
133
|
|
|
|
|
134
|
1 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritdoc} |
|
139
|
|
|
*/ |
|
140
|
7 |
|
public function getCommand() |
|
141
|
|
|
{ |
|
142
|
7 |
|
$options = implode(' ', array_unique($this->options)); |
|
143
|
|
|
|
|
144
|
7 |
|
$command = "systemctl {$options} {$this->action} {$this->service}"; |
|
145
|
|
|
|
|
146
|
|
|
// Remove extra whitespaces |
|
147
|
7 |
|
$command = preg_replace('/\s+/', ' ', trim($command)); |
|
148
|
|
|
|
|
149
|
7 |
|
return $command; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|