-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRunCommand.php
105 lines (91 loc) · 2.19 KB
/
RunCommand.php
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
<?php
namespace Enqueue\AsyncCommand;
final class RunCommand implements \JsonSerializable
{
/**
* @var string
*/
private $command;
/**
* @var string[]
*/
private $arguments;
/**
* @var string[]
*/
private $options;
/**
* @param string $command
* @param string[] $arguments
* @param string[] $options
*/
public function __construct($command, array $arguments = [], array $options = [])
{
$this->command = $command;
$this->arguments = $arguments;
$this->options = $options;
}
/**
* @return string
*/
public function getCommandLine()
{
$optionsString = '';
foreach ($this->options as $name => $value) {
$optionsString .= " $name=$value";
}
$optionsString = trim($optionsString);
$argumentsString = '';
foreach ($this->arguments as $value) {
$argumentsString .= " $value";
}
$argumentsString = trim($argumentsString);
return trim($this->command.' '.$argumentsString.' '.$optionsString);
}
/**
* @return string
*/
public function getCommand(): string
{
return $this->command;
}
/**
* @return string[]
*/
public function getArguments(): array
{
return $this->arguments;
}
/**
* @return string[]
*/
public function getOptions(): array
{
return $this->options;
}
public function jsonSerialize()
{
return [
'command' => $this->command,
'arguments' => $this->arguments,
'options' => $this->options,
];
}
/**
* @param string $json
*
* @return self
*/
public static function jsonUnserialize($json)
{
$data = json_decode($json, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf(
'The malformed json given. Error %s and message %s',
json_last_error(),
json_last_error_msg()
));
}
return new self($data['command'], $data['arguments'], $data['options']);
}
}