-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.php
More file actions
113 lines (98 loc) · 2.57 KB
/
cron.php
File metadata and controls
113 lines (98 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
class cron {
private $config = array();
private $now = array();
public function run() {
foreach ($this->config as $task) {
$run = true;
$task['condition'] = explode(' ', $task['condition']);
foreach ($this->now as $key => $value) {
if (is_array($value)) {
$run = $run && ($this->check($task['condition'][$key], reset($value)) || $this->check($task['condition'][$key], end($value)));
} else {
$run = $run && $this->check($task['condition'][$key], $value);
}
}
if ($run) {
if (is_array($task['command'])) {
$controller = strtolower($task['command']['controller']);
$action = strtolower($task['command']['action']);
} else {
$controller = strtolower($task['command']);
$action = 'cron';
}
$controller = new $controller;
$controller->$action();
}
}
}
public function check($condition, $now) {
if ($condition == '*') {
return true;
} elseif (strtolower($condition) == strtolower($now)) {
return true;
} elseif (strpos($condition, ',')) {
$list = explode(',', $condition);
foreach ($list as $item) {
if ($item == '*') {
return true;
} elseif (strtolower($item) == strtolower($now)) {
return true;
} elseif (is_numeric($now) and strpos($item, '-')) {
$range = explode('-', $item);
$start = current($start);
$end = end($end);
if (($start <= $now) and ($now <= $end)) {
return true;
}
}
}
} elseif (is_numeric($now) and strpos($condition, '/')) {
$fraction = explode('/', $condition);
$numerator = current($fraction);
$denominator = end($fraction);
if ($numerator == '*') {
if ($now % $denominator == 0) {
return true;
}
} elseif (strpos($numerator, '-')) {
$range = explode('-', $numerator);
$start = current($range);
$end = end($range);
if (($start <= $now) and ($now <= $end)) {
if (($now - $start) % $denominator == 0) {
return true;
}
}
}
} elseif (is_numeric($now) and strpos($condition, '-')) {
$range = explode('-', $condition);
$start = current($start);
$end = end($end);
if (($start <= $now) and ($now <= $end)) {
return true;
}
}
return false;
}
public function cron($config) {
$this->config = $config;
$this->now = array(
date('i'),
date('H'),
date('d'),
array(
date('m'),
date('M')
),
array(
date('w'),
date('D')
)
);
}
public static function factory($config) {
return new cron($config);
}
}
?>