1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pheanstalk\Structure; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A job in a EvQueue server. |
7
|
|
|
* |
8
|
|
|
* @author Valentin Corre |
9
|
|
|
* @package Pheanstalk |
10
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php |
11
|
|
|
*/ |
12
|
|
|
class Task |
13
|
|
|
{ |
14
|
|
|
const OUTPUT_TEXT = "TEXT"; |
15
|
|
|
const PARAMETER_MODE_CMD = "CMDLINE"; |
16
|
|
|
|
17
|
|
|
/** @var string $outputMethod */ |
18
|
|
|
protected $outputMethod; |
19
|
|
|
|
20
|
|
|
/** @var string $parametersMode */ |
21
|
|
|
protected $parametersMode; |
22
|
|
|
|
23
|
|
|
/** @var string $path */ |
24
|
|
|
protected $path; |
25
|
|
|
|
26
|
|
|
/** @var string string $queue */ |
27
|
|
|
protected $queue; |
28
|
|
|
|
29
|
|
|
/** @var string|null $host */ |
30
|
|
|
protected $host; |
31
|
|
|
|
32
|
|
|
/** @var string|null $user */ |
33
|
|
|
protected $user; |
34
|
|
|
|
35
|
|
|
/** @var bool */ |
36
|
|
|
protected $useAgent; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $path The command that should be executed by the server |
40
|
|
|
* @param string $queue The queue used by the Task |
41
|
|
|
* @param bool $useAgent Tell if you want to use the EvQueue Agent (Used to send status of tasks) |
42
|
|
|
* @param string|null $user The user that will execute the command |
43
|
|
|
* @param string|null $host The Ip address where to execute the command |
44
|
|
|
* @param string $outputMethod The output mode |
45
|
|
|
* @param string $parametersMode The type of parameters |
46
|
|
|
*/ |
47
|
6 |
|
public function __construct( |
48
|
|
|
$path, |
49
|
|
|
$queue, |
50
|
|
|
$useAgent = false, |
51
|
|
|
$user = null, |
52
|
|
|
$host = null, |
53
|
|
|
$outputMethod = self::OUTPUT_TEXT, |
54
|
|
|
$parametersMode = self::PARAMETER_MODE_CMD |
55
|
|
|
) { |
56
|
6 |
|
$this->path = $path; |
57
|
6 |
|
$this->queue = $queue; |
58
|
6 |
|
$this->useAgent = $useAgent; |
59
|
6 |
|
$this->user = $user; |
60
|
6 |
|
$this->host = $host; |
61
|
6 |
|
$this->outputMethod = $outputMethod; |
62
|
6 |
|
$this->parametersMode = $parametersMode; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
2 |
|
public function getOutputMethod(): string |
69
|
|
|
{ |
70
|
2 |
|
return $this->outputMethod; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $outputMethod |
75
|
|
|
* |
76
|
|
|
* @return Task |
77
|
|
|
*/ |
78
|
|
|
public function setOutputMethod(string $outputMethod): Task |
79
|
|
|
{ |
80
|
|
|
$this->outputMethod = $outputMethod; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
2 |
|
public function getParametersMode(): string |
88
|
|
|
{ |
89
|
2 |
|
return $this->parametersMode; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $parametersMode |
94
|
|
|
* |
95
|
|
|
* @return Task |
96
|
|
|
*/ |
97
|
|
|
public function setParametersMode(string $parametersMode): Task |
98
|
|
|
{ |
99
|
|
|
$this->parametersMode = $parametersMode; |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
2 |
|
public function getPath(): string |
107
|
|
|
{ |
108
|
2 |
|
return $this->path; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $path |
113
|
|
|
* |
114
|
|
|
* @return Task |
115
|
|
|
*/ |
116
|
1 |
|
public function setPath(string $path): Task |
117
|
|
|
{ |
118
|
1 |
|
$this->path = $path; |
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
2 |
|
public function getQueue(): string |
126
|
|
|
{ |
127
|
2 |
|
return $this->queue; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $queue |
132
|
|
|
* |
133
|
|
|
* @return Task |
134
|
|
|
*/ |
135
|
|
|
public function setQueue(string $queue): Task |
136
|
|
|
{ |
137
|
|
|
$this->queue = $queue; |
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return string|null |
143
|
|
|
*/ |
144
|
2 |
|
public function getHost(): ?string |
145
|
|
|
{ |
146
|
2 |
|
return $this->host; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string|null $host |
151
|
|
|
* |
152
|
|
|
* @return Task |
153
|
|
|
*/ |
154
|
|
|
public function setHost(?string $host): Task |
155
|
|
|
{ |
156
|
|
|
$this->host = $host; |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string|null |
162
|
|
|
*/ |
163
|
2 |
|
public function getUser(): ?string |
164
|
|
|
{ |
165
|
2 |
|
return $this->user; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string|null $user |
170
|
|
|
* |
171
|
|
|
* @return Task |
172
|
|
|
*/ |
173
|
|
|
public function setUser(?string $user): Task |
174
|
|
|
{ |
175
|
|
|
$this->user = $user; |
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
2 |
|
public function getUseAgent(): bool |
183
|
|
|
{ |
184
|
2 |
|
return $this->useAgent; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param bool $agent |
189
|
|
|
* |
190
|
|
|
* @return Task |
191
|
|
|
*/ |
192
|
|
|
public function setUseAgent(bool $agent): Task |
193
|
|
|
{ |
194
|
|
|
$this->useAgent = $agent; |
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return \DOMDocument |
200
|
|
|
* @throws \ReflectionException |
201
|
|
|
*/ |
202
|
2 |
|
public function getXml() |
203
|
|
|
{ |
204
|
2 |
|
$reflection = new \ReflectionClass($this); |
205
|
2 |
|
$dom = new \DOMDocument("1.0", "utf-8"); |
206
|
2 |
|
$root = $dom->createElement("task"); |
207
|
2 |
|
foreach ($reflection->getProperties() as $property) { |
208
|
2 |
|
$value = $this->{'get'.ucfirst($property->getName())}(); |
209
|
2 |
|
$root->setAttribute($this->from_camel_case($property->getName()), $value); |
210
|
|
|
} |
211
|
2 |
|
$dom->appendChild($root); |
212
|
2 |
|
return $dom; |
213
|
|
|
} |
214
|
|
|
|
215
|
7 |
|
function from_camel_case($input) |
|
|
|
|
216
|
|
|
{ |
217
|
7 |
|
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); |
218
|
7 |
|
$ret = $matches[0]; |
219
|
7 |
|
foreach ($ret as &$match) { |
220
|
7 |
|
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); |
221
|
|
|
} |
222
|
7 |
|
return implode('-', $ret); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|