|
1
|
|
|
<?php namespace Comodojo\Extender\Utils; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Validation\DataFilter; |
|
4
|
|
|
use \Cron\CronExpression; |
|
5
|
|
|
use \Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @package Comodojo Extender |
|
9
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* |
|
12
|
|
|
* LICENSE: |
|
13
|
|
|
* |
|
14
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
15
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
16
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
17
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
18
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
19
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
20
|
|
|
* THE SOFTWARE. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
class Validator { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Validate a cron expression and, if valid, return next run timestamp plus |
|
27
|
|
|
* an array of expression parts |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $expression |
|
30
|
|
|
* |
|
31
|
|
|
* @return array Next run timestamp at first position, expression parts at second |
|
32
|
|
|
* @throws \Exception |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function cronExpression($expression) { |
|
35
|
|
|
|
|
36
|
|
|
try { |
|
37
|
|
|
|
|
38
|
|
|
$cron = CronExpression::factory($expression); |
|
39
|
|
|
|
|
40
|
|
|
$s = $cron->getNextRunDate()->format('c'); |
|
41
|
|
|
|
|
42
|
|
|
$e = $cron->getExpression(); |
|
43
|
|
|
|
|
44
|
|
|
$e_array = preg_split('/\s/', $e, -1, PREG_SPLIT_NO_EMPTY); |
|
45
|
|
|
|
|
46
|
|
|
$e_count = count($e_array); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
if ( $e_count < 5 || $e_count > 6 ) throw new Exception($e." is not a valid cron expression"); |
|
49
|
|
|
|
|
50
|
|
|
if ( $e_count == 5 ) $e_array[] = "*"; |
|
51
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
catch (Exception $e) { |
|
54
|
|
|
|
|
55
|
|
|
throw $e; |
|
56
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return array($s, $e_array); |
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function laggerTimeout($timeout) { |
|
64
|
|
|
|
|
65
|
|
|
return DataFilter::filterInteger( |
|
66
|
|
|
$timeout, |
|
67
|
|
|
0, |
|
|
|
|
|
|
68
|
|
|
PHP_INT_MAX, |
|
|
|
|
|
|
69
|
|
|
5 |
|
|
|
|
|
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public static function maxChildRuntime($runtime) { |
|
75
|
|
|
|
|
76
|
|
|
return DataFilter::filterInteger( |
|
77
|
|
|
$runtime, |
|
78
|
|
|
1, |
|
|
|
|
|
|
79
|
|
|
PHP_INT_MAX, |
|
|
|
|
|
|
80
|
|
|
600 |
|
|
|
|
|
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public static function forkLimit($limit) { |
|
86
|
|
|
|
|
87
|
|
|
return DataFilter::filterInteger( |
|
88
|
|
|
$limit, |
|
89
|
|
|
0, |
|
|
|
|
|
|
90
|
|
|
PHP_INT_MAX, |
|
|
|
|
|
|
91
|
|
|
5 |
|
|
|
|
|
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public static function niceness($niceness) { |
|
97
|
|
|
|
|
98
|
|
|
return DataFilter::filterInteger( |
|
99
|
|
|
$niceness, |
|
100
|
|
|
-20, |
|
|
|
|
|
|
101
|
|
|
20, |
|
|
|
|
|
|
102
|
|
|
0 |
|
|
|
|
|
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public static function multithread($multithread) { |
|
108
|
|
|
|
|
109
|
|
|
return $multithread === true && Checks::multithread(); |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|