1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Watchmaker; |
4
|
|
|
|
5
|
|
|
use Watchmaker\error\EmptyCommandException; |
6
|
|
|
|
7
|
|
|
class WatchmakerCore |
8
|
|
|
{ |
9
|
|
|
private $command = ''; |
10
|
|
|
private $rawLine = ''; |
11
|
|
|
|
12
|
|
|
private $month = '*'; |
13
|
|
|
private $day = '*'; |
14
|
|
|
private $hour = '*'; |
15
|
|
|
private $minute = '*'; |
16
|
|
|
private $week = '*'; |
17
|
|
|
|
18
|
|
|
public const INSTALLED = 1; |
19
|
|
|
public const NOT_INSTALLED = 2; |
20
|
|
|
public const CRON_ONLY = 3; |
21
|
|
|
private $mark = null; |
22
|
|
|
|
23
|
|
|
private $manage = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* WatchmakerCore constructor. |
27
|
|
|
* @param string $command |
28
|
|
|
*/ |
29
|
|
|
public function __construct($command = '') |
30
|
|
|
{ |
31
|
|
|
$this->command = $command; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $cronLine |
36
|
|
|
* @return WatchmakerCore |
37
|
|
|
*/ |
38
|
|
|
public static function parse($cronLine) : WatchmakerCore |
39
|
|
|
{ |
40
|
|
|
$instance = new static(); |
41
|
|
|
|
42
|
|
|
$cronLine = trim($cronLine); |
43
|
|
|
$arr = explode(' ', $cronLine, 6); |
44
|
|
|
if (count($arr) !== 6) { |
45
|
|
|
// not cron line |
46
|
|
|
return $instance |
47
|
|
|
->rawLine($cronLine); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$instance = $instance |
51
|
|
|
->minute($arr[0]) |
52
|
|
|
->hour($arr[1]) |
53
|
|
|
->day($arr[2]) |
54
|
|
|
->month($arr[3]) |
55
|
|
|
->week($arr[4]) |
56
|
|
|
->command($arr[5]); |
57
|
|
|
|
58
|
|
|
return $instance; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $v |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function month($v) : self |
66
|
|
|
{ |
67
|
|
|
$new = clone $this; |
68
|
|
|
$new->month = (string)$v; |
69
|
|
|
|
70
|
|
|
return $new; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $v |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function day($v) : self |
78
|
|
|
{ |
79
|
|
|
$new = clone $this; |
80
|
|
|
$new->day = (string)$v; |
81
|
|
|
|
82
|
|
|
return $new; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $v |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function hour($v) : self |
90
|
|
|
{ |
91
|
|
|
$new = clone $this; |
92
|
|
|
$new->hour = (string)$v; |
93
|
|
|
|
94
|
|
|
return $new; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $v |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function minute($v) : self |
102
|
|
|
{ |
103
|
|
|
$new = clone $this; |
104
|
|
|
$new->minute = (string)$v; |
105
|
|
|
|
106
|
|
|
return $new; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $v |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function week($v) : self |
114
|
|
|
{ |
115
|
|
|
$new = clone $this; |
116
|
|
|
$new->week = (string)$v; |
117
|
|
|
|
118
|
|
|
return $new; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function monday() : self |
122
|
|
|
{ |
123
|
|
|
return $this->week(1); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function tuesday() : self |
127
|
|
|
{ |
128
|
|
|
return $this->week(2); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function wednesday() : self |
132
|
|
|
{ |
133
|
|
|
return $this->week(3); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function thursday() : self |
137
|
|
|
{ |
138
|
|
|
return $this->week(4); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function friday() : self |
142
|
|
|
{ |
143
|
|
|
return $this->week(5); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function saturday() : self |
147
|
|
|
{ |
148
|
|
|
return $this->week(6); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function sunday() : self |
152
|
|
|
{ |
153
|
|
|
return $this->week(7); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function command($v) : self |
157
|
|
|
{ |
158
|
|
|
$new = clone $this; |
159
|
|
|
$new->command = $v; |
160
|
|
|
|
161
|
|
|
return $new; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function mark($mark) : self |
165
|
|
|
{ |
166
|
|
|
$new = clone $this; |
167
|
|
|
$new->mark = $mark; |
168
|
|
|
|
169
|
|
|
return $new; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function manage(bool $manage) : self |
173
|
|
|
{ |
174
|
|
|
$new = clone $this; |
175
|
|
|
$new->manage = $manage; |
176
|
|
|
|
177
|
|
|
return $new; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function rawLine(string $rawLine) : self |
181
|
|
|
{ |
182
|
|
|
$new = clone $this; |
183
|
|
|
$new->rawLine = $rawLine; |
184
|
|
|
$new->manage = false; |
185
|
|
|
|
186
|
|
|
return $new; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function installed() : self |
190
|
|
|
{ |
191
|
|
|
$new = clone $this; |
192
|
|
|
$new->mark = self::INSTALLED; |
193
|
|
|
|
194
|
|
|
return $new; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function notInstalled() : self |
198
|
|
|
{ |
199
|
|
|
$new = clone $this; |
200
|
|
|
$new->mark = self::NOT_INSTALLED; |
201
|
|
|
|
202
|
|
|
return $new; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function cronOnly() : self |
206
|
|
|
{ |
207
|
|
|
$new = clone $this; |
208
|
|
|
$new->mark = self::CRON_ONLY; |
209
|
|
|
|
210
|
|
|
return $new; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function isInstalled() : bool |
214
|
|
|
{ |
215
|
|
|
return $this->mark === self::INSTALLED; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function isNotInstalled() : bool |
219
|
|
|
{ |
220
|
|
|
return $this->mark === self::NOT_INSTALLED; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function isCronOnly() : bool |
224
|
|
|
{ |
225
|
|
|
return $this->mark === self::CRON_ONLY; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function isManage() : bool |
229
|
|
|
{ |
230
|
|
|
return $this->manage; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/*public function isInstalled() : bool |
234
|
|
|
{ |
235
|
|
|
$originList = CrontabLoader::load(); |
236
|
|
|
foreach($originList as $originKairos) |
237
|
|
|
{ |
238
|
|
|
$origin = $originKairos->generate(); |
239
|
|
|
if ($origin === $this->generate()) { |
240
|
|
|
return true; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return false; |
245
|
|
|
}*/ |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return string |
250
|
|
|
* @throws EmptyCommandException |
251
|
|
|
*/ |
252
|
|
|
public function generate() : string |
253
|
|
|
{ |
254
|
|
|
if ($this->manage === true) { |
255
|
|
|
if (empty($this->command)) { |
256
|
|
|
throw new EmptyCommandException(); |
257
|
|
|
} |
258
|
|
|
return sprintf("%s %s %s %s %s %s", $this->minute, $this->hour, $this->day, $this->month, $this->week, $this->command); |
259
|
|
|
} else { |
260
|
|
|
return $this->rawLine; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|