|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mistletoe; |
|
3
|
|
|
use Cron\CronExpression; |
|
4
|
|
|
use Mistletoe\Contracts\ExpressionBuilderInterface; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class ExpressionBuilder |
|
8
|
|
|
* @package Mistletoe |
|
9
|
|
|
*/ |
|
10
|
|
|
class ExpressionBuilder implements ExpressionBuilderInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var TaskBag */ |
|
13
|
|
|
protected $bag; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Builds an expression from the TaskBag |
|
17
|
|
|
* @param TaskBag|null $bag |
|
18
|
|
|
* @return CronExpression |
|
19
|
|
|
*/ |
|
20
|
12 |
|
public function build(TaskBag $bag = null) |
|
21
|
|
|
{ |
|
22
|
12 |
|
if ($bag) { |
|
23
|
|
|
$this->setTaskBag($bag); |
|
24
|
|
|
} |
|
25
|
12 |
|
$parts = []; |
|
26
|
|
|
|
|
27
|
12 |
|
if ($this->onlyIntervalIsSet($this->bag)) { |
|
28
|
2 |
|
$expression = $this->bag->getInterval(); |
|
29
|
|
|
|
|
30
|
2 |
|
} else { |
|
31
|
|
|
// Get with defaults |
|
32
|
10 |
|
$parts['minute'] = $this->getPartWithDefault('minute'); |
|
33
|
10 |
|
$parts['hour'] = $this->getPartWithDefault('hour'); |
|
34
|
10 |
|
$parts['date'] = $this->getPartWithDefault('day'); |
|
35
|
10 |
|
$parts['month'] = $this->getPartWithDefault('month'); |
|
36
|
10 |
|
$parts['weekday'] = $this->getPartWithDefault('weekday'); |
|
37
|
|
|
|
|
38
|
|
|
/* Are we dealing with outside scenarios? */ |
|
39
|
|
|
// Are we setting a day and month without a time? |
|
40
|
|
|
if ( |
|
41
|
10 |
|
($this->bag->getDay() && $this->bag->getMonth() |
|
42
|
10 |
|
&& (!$this->bag->getHour() && !$this->bag->getMinute())) |
|
43
|
10 |
|
) { |
|
44
|
|
|
// Yes. We don't want it to run every minute! |
|
45
|
2 |
|
$parts['minute'] = '0'; |
|
46
|
2 |
|
$parts['hour'] = '0'; |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Any other outside scenarios? |
|
50
|
|
|
|
|
51
|
10 |
|
$expression = implode(' ', $parts); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
12 |
|
return $this->buildFrom($expression); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Builds expression from a string expression |
|
59
|
|
|
* @param null|string $string |
|
60
|
|
|
* @return CronExpression |
|
61
|
|
|
*/ |
|
62
|
13 |
|
public function buildFrom($string) |
|
63
|
|
|
{ |
|
64
|
13 |
|
return CronExpression::factory($string); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set the Task Bag |
|
69
|
|
|
* @param TaskBag $bag |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
12 |
|
public function setTaskBag(TaskBag $bag) |
|
73
|
|
|
{ |
|
74
|
12 |
|
$this->bag = $bag; |
|
75
|
12 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return TaskBag |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getTaskBag() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->bag; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Is only the interval set in the bag? |
|
88
|
|
|
* @param TaskBag $bag |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
12 |
|
protected function onlyIntervalIsSet(TaskBag $bag) |
|
92
|
|
|
{ |
|
93
|
12 |
|
return $bag->getInterval() !== null |
|
94
|
12 |
|
&& $bag->getMonth() === null |
|
95
|
12 |
|
&& $bag->getDay() === null |
|
96
|
12 |
|
&& $bag->getMinute() === null |
|
97
|
12 |
|
&& $bag->getHour() === null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Ensures the parts are cast as strings |
|
102
|
|
|
* @param $value |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
10 |
|
protected function toPart($value) |
|
106
|
|
|
{ |
|
107
|
10 |
|
if ($value === 0) { |
|
108
|
1 |
|
return '0'; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
10 |
|
return (string) $value; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns the part from the bag with * as default |
|
116
|
|
|
* @param string $part |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
10 |
|
protected function getPartWithDefault($part) |
|
120
|
|
|
{ |
|
121
|
10 |
|
$value = $this->bag->{'get'.ucfirst($part)}(); |
|
122
|
10 |
|
if (!is_null($value)) { |
|
123
|
10 |
|
return $this->toPart($value); |
|
124
|
|
|
} else { |
|
125
|
10 |
|
return '*'; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|