1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MyBuilder\Cronos\Formatter; |
4
|
|
|
|
5
|
|
|
class Header |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Cron |
9
|
|
|
*/ |
10
|
|
|
private $cron; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $path; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $mailTo; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $home; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $shell; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $contentType; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $encoding; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $timezone; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Cron $cron |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Cron $cron) |
51
|
|
|
{ |
52
|
|
|
$this->cron = $cron; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Works just like the shell PATH, but it does not inherit from your environment. |
57
|
|
|
* |
58
|
|
|
* @param string $path |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function setPath($path) |
63
|
|
|
{ |
64
|
|
|
$this->path = $path; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $home |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function setHome($home) |
75
|
|
|
{ |
76
|
|
|
$this->home = $home; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $email |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
* |
86
|
|
|
* @throws InvalidEmail if given email is invalid |
87
|
|
|
*/ |
88
|
|
|
public function setMailto($email) |
89
|
|
|
{ |
90
|
|
|
$this->assertValidEmail($email); |
91
|
|
|
$this->mailTo = $email; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function assertValidEmail($email) |
97
|
|
|
{ |
98
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
99
|
|
|
throw new InvalidEmail($email); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set the shell to be used when executing commands |
105
|
|
|
* |
106
|
|
|
* Default is /bin/sh but can also be changed to /bin/php |
107
|
|
|
* |
108
|
|
|
* @param string $shell |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function setShell($shell) |
113
|
|
|
{ |
114
|
|
|
$this->shell = $shell; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set the content-type to use for cron output emails. |
121
|
|
|
* |
122
|
|
|
* @param string $contentType |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function setContentType($contentType) |
127
|
|
|
{ |
128
|
|
|
$this->contentType = $contentType; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the charset to use for cron output emails. |
135
|
|
|
* |
136
|
|
|
* @param string $encoding |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function setContentTransferEncoding($encoding) |
141
|
|
|
{ |
142
|
|
|
$this->encoding = $encoding; |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param $timezone |
149
|
|
|
* |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
public function setTimezone($timezone) |
153
|
|
|
{ |
154
|
|
|
$this->timezone = $timezone; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
public function format() |
163
|
|
|
{ |
164
|
|
|
$headers = ''; |
165
|
|
|
if ($this->path) { |
166
|
|
|
$headers .= $this->createHeader('PATH', $this->path); |
167
|
|
|
} |
168
|
|
|
if ($this->mailTo) { |
169
|
|
|
$headers .= $this->createHeader('MAILTO', $this->mailTo); |
170
|
|
|
} |
171
|
|
|
if ($this->home) { |
172
|
|
|
$headers .= $this->createHeader('HOME', $this->home); |
173
|
|
|
} |
174
|
|
|
if ($this->shell) { |
175
|
|
|
$headers .= $this->createHeader('SHELL', $this->shell); |
176
|
|
|
} |
177
|
|
|
if ($this->contentType) { |
178
|
|
|
$headers .= $this->createHeader('CONTENT_TYPE', $this->contentType); |
179
|
|
|
} |
180
|
|
|
if ($this->encoding) { |
181
|
|
|
$headers .= $this->createHeader('CONTENT_TRANSFER_ENCODING', $this->encoding); |
182
|
|
|
} |
183
|
|
|
if ($this->timezone) { |
184
|
|
|
$headers .= $this->createHeader('CRON_TZ', $this->timezone); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $headers; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
private function createHeader($name, $value) |
191
|
|
|
{ |
192
|
|
|
return $name . '=' . $value . PHP_EOL; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return Cron |
197
|
|
|
*/ |
198
|
|
|
public function end() |
199
|
|
|
{ |
200
|
|
|
return $this->cron; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|