1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of SebastianFeldmann\Crontab. |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace SebastianFeldmann\Crontab; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Job |
16
|
|
|
* |
17
|
|
|
* @package SebastianFeldmann\Crontab |
18
|
|
|
* @author Sebastian Feldmann <[email protected]> |
19
|
|
|
* @link https://github.com/sebastianfeldmann/crontab |
20
|
|
|
* @since Class available since Release 0.9.0 |
21
|
|
|
*/ |
22
|
|
|
class Job |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Cron schedule expression. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $schedule; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Command that should get executed. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $command; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* List of comments to describe the command. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $comments; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Entry constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $schedule |
49
|
|
|
* @param string $command |
50
|
|
|
* @param array $comments |
51
|
|
|
*/ |
52
|
|
|
public function __construct(string $schedule, string $command, array $comments = []) |
53
|
|
|
{ |
54
|
|
|
$this->schedule = $schedule; |
55
|
|
|
$this->command = $command; |
56
|
|
|
$this->comments = $comments; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getSchedule(): string |
60
|
|
|
{ |
61
|
|
|
return $this->schedule; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return the command executed by the crontab entry. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getCommand(): string |
70
|
|
|
{ |
71
|
|
|
return $this->command; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return comment list. |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getComments(): array |
80
|
|
|
{ |
81
|
|
|
return $this->comments; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return comments as string. |
86
|
|
|
* All lines get prefixed with '# ' |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function formatComments(): string |
91
|
|
|
{ |
92
|
|
|
$doc = ''; |
93
|
|
|
foreach ($this->comments as $comment) { |
94
|
|
|
$doc .= '# ' . $comment . PHP_EOL; |
95
|
|
|
} |
96
|
|
|
return $doc; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Magic to string method. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function __toString(): string |
105
|
|
|
{ |
106
|
|
|
return $this->formatComments() . $this->schedule . ' ' . $this->command . PHP_EOL; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|