FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tasks/src/RunResolver.php at develop · codeigniter4/tasks · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
codeigniter4
/
tasks
Public
Notifications
You must be signed in to change notification settings
Fork
24
Star
126
Code
Issues
4
Pull requests
2
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tasks
/
src
/
RunResolver.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
208 lines (177 loc) · 5.89 KB
Breadcrumbs
tasks
/
src
/
RunResolver.php
Copy path
File metadata and controls
208 lines (177 loc) · 5.89 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare
(strict_types=
1
);
/**
* This file is part of CodeIgniter Tasks.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace
CodeIgniter
\
Tasks
;
use
CodeIgniter
\
I18n
\
Time
;
class
RunResolver
{
/**
* The maximum number of times to loop
* when looking for next run date.
*/
protected
int
$
maxIterations
=
1000
;
/**
* Takes a cron expression, i.e. '* * * * 4', and returns
* a Time instance that represents that next time that
* expression would run.
*/
public
function
nextRun
(
string
$
expression
,
Time
$
next
):
Time
{
// Break the expression into separate parts
[
$
minute
,
$
hour
,
$
monthDay
,
$
month
,
$
weekDay
,
] =
explode
(
'
'
,
$
expression
);
$
cron
= [
'
minute
'
=>
$
minute
,
'
hour
'
=>
$
hour
,
'
monthDay
'
=>
$
monthDay
,
'
month
'
=>
$
month
,
'
weekDay
'
=>
$
weekDay
,
];
// We don't need to satisfy '*' values, so
// remove them to have less to loop over.
$
cron
=
array_filter
(
$
cron
,
static
fn
(
$
item
) =>
$
item
!==
'
*
'
);
// If there's nothing left then it's every minute
// so set it to one minute from now.
if
(
$
cron
=== []) {
return
$
next
->
addMinutes
(
1
)->
setSecond
(
0
);
}
// Loop over each of the remaining $cron elements
// until we manage to satisfy all of the them
for
(
$
i
=
1
;
$
i
<=
$
this
->
maxIterations
;
$
i
++) {
foreach
(
$
cron
as
$
position
=>
$
value
) {
$
satisfied
=
false
;
// The method to use on the Time instance
$
method
=
'
get
'
.
ucfirst
(
$
position
);
// monthDay and weekDay need custom methods
if
(
$
position
===
'
monthDay
'
) {
$
method
=
'
getDay
'
;
}
if
(
$
position
===
'
weekDay
'
) {
$
method
=
'
getDayOfWeek
'
;
$
value
=
$
this
->
convertDOWToNumbers
(
$
value
);
}
$
nextValue
=
$
next
->{
$
method
}();
// If it's a single value
if
(
$
nextValue
===
$
value
) {
$
satisfied
=
true
;
}
// If the value is a list
elseif
(
str_contains
(
$
value
,
'
,
'
)) {
if
(
$
this
->
isInList
(
$
nextValue
,
$
value
)) {
$
satisfied
=
true
;
}
}
// If the value is a range
elseif
(
str_contains
(
$
value
,
'
-
'
)) {
if
(
$
this
->
isInRange
(
$
nextValue
,
$
value
)) {
$
satisfied
=
true
;
}
}
// If the value is an increment
elseif
(
str_contains
(
$
value
,
'
/
'
)) {
if
(
$
this
->
isInIncrement
(
$
nextValue
,
$
value
)) {
$
satisfied
=
true
;
}
}
// If we didn't match it, then start the iterations over
if
(!
$
satisfied
) {
$
next
=
$
this
->
increment
(
$
next
,
$
position
);
continue
2
;
}
}
}
return
$
next
;
}
/**
* Increments the part of the cron to the next appropriate.
*
* Note: this is a pretty brute-force way to do it. We could
* definitely make it smarter in the future to cut down on the
* amount of iterations needed.
*/
protected
function
increment
(
Time
$
next
,
string
$
position
):
Time
{
return
match
(
$
position
) {
'
minute
'
=>
$
next
->
addMinutes
(
1
),
'
hour
'
=>
$
next
->
addHours
(
1
),
'
monthDay
'
,
'
weekDay
'
=>
$
next
->
addDays
(
1
),
'
month
'
=>
$
next
->
addMonths
(
1
),
default
=>
$
next
,
};
}
/**
* Determines if the given value is in the specified range.
*
* @param int|string $value
*/
protected
function
isInRange
(
$
value
,
string
$
range
):
bool
{
[
$
start
,
$
end
] =
explode
(
'
-
'
,
$
range
);
return
$
value
>=
$
start
&&
$
value
<=
$
end
;
}
/**
* Determines if the given value is in the specified list of values.
*
* @param int|string $value
*/
protected
function
isInList
(
$
value
,
string
$
list
):
bool
{
$
list
=
explode
(
'
,
'
,
$
list
);
return
in_array
(
trim
((
string
)
$
value
),
$
list
,
true
);
}
/**
* Determines if the $value is one of the increments.
*
* @param int|string $value
*/
protected
function
isInIncrement
(
$
value
,
string
$
increment
):
bool
{
[
$
start
,
$
increment
] =
explode
(
'
/
'
,
$
increment
);
// Allow for empty start values
if
(
$
start
===
''
||
$
start
===
'
*
'
) {
$
start
=
0
;
}
// The $start interval should be the first one to test against
if
(
$
value
===
$
start
) {
return
true
;
}
return
(
$
value
-
$
start
) >
0
&& ((
$
value
-
$
start
) %
$
increment
) ===
0
;
}
/**
* Given a cron setting for Day of Week, will convert
* settings with text days of week (Mon, Tue, etc)
* into numeric values for easier handling.
*/
protected
function
convertDOWToNumbers
(
string
$
origValue
):
string
{
$
origValue
=
strtolower
(
trim
(
$
origValue
));
// If it doesn't contain any letters, just return it.
preg_match
(
'
/\w/
'
,
$
origValue
,
$
matches
);
if
(
$
matches
=== []) {
return
$
origValue
;
}
$
days
= [
'
sun
'
=>
'
0
'
,
'
mon
'
=>
'
1
'
,
'
tue
'
=>
'
2
'
,
'
wed
'
=>
'
3
'
,
'
thu
'
=>
'
4
'
,
'
fri
'
=>
'
5
'
,
'
sat
'
=>
'
6
'
,
];
return
str_replace
(
array_keys
(
$
days
),
array_values
(
$
days
),
$
origValue
);
}
}
Back
|
FazBrowse Home
|
New Git URL