1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of RoboSystemService. |
4
|
|
|
* |
5
|
|
|
* @author Aitor García Martínez (Falc) <[email protected]> |
6
|
|
|
* @copyright 2016 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
|
|
|
* SysVinit command builder. |
18
|
|
|
*/ |
19
|
|
|
abstract class SysVinitCommandBuilder implements CommandBuilderInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Command name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $cmd; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Command action. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $action; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Whether the command should be quiet or not. |
37
|
|
|
* |
38
|
|
|
* @var boolean |
39
|
|
|
*/ |
40
|
|
|
protected $quiet = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Service. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $service; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Option list. |
51
|
|
|
* |
52
|
|
|
* @var string[] |
53
|
|
|
*/ |
54
|
|
|
protected $options = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
6 |
|
public function start($service) |
60
|
|
|
{ |
61
|
6 |
|
if (empty($service)) { |
62
|
2 |
|
throw new \InvalidArgumentException('No service selected to be started'); |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
$this->cmd = '/etc/init.d'; |
66
|
4 |
|
$this->service = $service; |
67
|
4 |
|
$this->action = 'start'; |
68
|
|
|
|
69
|
4 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
4 |
View Code Duplication |
public function stop($service) |
|
|
|
|
76
|
|
|
{ |
77
|
4 |
|
if (empty($service)) { |
78
|
2 |
|
throw new \InvalidArgumentException('No service selected to be stopped'); |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
$this->cmd = '/etc/init.d'; |
82
|
2 |
|
$this->service = $service; |
83
|
2 |
|
$this->action = 'stop'; |
84
|
|
|
|
85
|
2 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
4 |
|
public function restart($service) |
92
|
|
|
{ |
93
|
4 |
|
if (empty($service)) { |
94
|
2 |
|
throw new \InvalidArgumentException('No service selected to be restarted'); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
$this->cmd = '/etc/init.d'; |
98
|
2 |
|
$this->service = $service; |
99
|
2 |
|
$this->action = 'restart'; |
100
|
|
|
|
101
|
2 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
4 |
View Code Duplication |
public function reload($service) |
|
|
|
|
108
|
|
|
{ |
109
|
4 |
|
if (empty($service)) { |
110
|
2 |
|
throw new \InvalidArgumentException('No service selected to be reloaded'); |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
$this->cmd = '/etc/init.d'; |
114
|
2 |
|
$this->service = $service; |
115
|
2 |
|
$this->action = 'reload'; |
116
|
|
|
|
117
|
2 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
abstract public function enable($service); |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
abstract public function disable($service); |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
* |
133
|
|
|
* @throws \BadMethodCallException |
134
|
|
|
*/ |
135
|
2 |
|
public function daemonReload() |
136
|
|
|
{ |
137
|
2 |
|
throw new \BadMethodCallException('Action "daemon-reload" not supported for SysVinit'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
2 |
|
public function quiet() |
144
|
|
|
{ |
145
|
2 |
|
$this->quiet = true; |
146
|
|
|
|
147
|
2 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
14 |
|
public function getCommand() |
154
|
|
|
{ |
155
|
14 |
|
$options = implode(' ', array_unique($this->options)); |
156
|
|
|
|
157
|
14 |
|
$command = "{$this->cmd}/{$this->service} {$this->action} {$options}"; |
158
|
|
|
|
159
|
14 |
|
if (!in_array($this->action, ['start', 'stop', 'restart', 'reload'])) { |
160
|
4 |
|
$command = "{$this->cmd} {$options} {$this->service} {$this->action}"; |
161
|
4 |
|
} |
162
|
|
|
|
163
|
14 |
|
if ($this->quiet === true) { |
164
|
2 |
|
$command = "{$command} > /dev/null"; |
165
|
2 |
|
} |
166
|
|
|
|
167
|
|
|
// Remove extra whitespaces |
168
|
14 |
|
$command = preg_replace('/\s+/', ' ', trim($command)); |
169
|
|
|
|
170
|
14 |
|
return $command; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.