|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of SebastianFeldmann\Cli. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace SebastianFeldmann\Cli; |
|
11
|
|
|
|
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class CommandLine |
|
16
|
|
|
* |
|
17
|
|
|
* @package SebastianFeldmann\Cli |
|
18
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
19
|
|
|
* @link https://github.com/sebastianfeldmann/cli |
|
20
|
|
|
* @since Class available since Release 0.9.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class CommandLine implements Command |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* List of system commands to execute. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \SebastianFeldmann\Cli\Command[] |
|
28
|
|
|
*/ |
|
29
|
|
|
private $commands = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Redirect the output |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $redirectOutput; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Output pipeline |
|
40
|
|
|
* |
|
41
|
|
|
* @var \SebastianFeldmann\Cli\Command[] |
|
42
|
|
|
*/ |
|
43
|
|
|
private $pipeline = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Redirect the stdOut. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $path |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function redirectOutputTo($path) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$this->redirectOutput = $path; |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Should the output be redirected. |
|
57
|
|
|
* |
|
58
|
|
|
* @return boolean |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function isOutputRedirected() |
|
61
|
|
|
{ |
|
62
|
1 |
|
return !empty($this->redirectOutput); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Redirect getter. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getRedirectPath() |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->redirectOutput; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Pipe the command into given command. |
|
77
|
|
|
* |
|
78
|
|
|
* @param \SebastianFeldmann\Cli\Command $cmd |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function pipeOutputTo(Command $cmd) |
|
81
|
|
|
{ |
|
82
|
1 |
|
if (!$this->canPipe()) { |
|
83
|
|
|
throw new RuntimeException('Can\'t pipe output'); |
|
84
|
|
|
} |
|
85
|
1 |
|
$this->pipeline[] = $cmd; |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Can the pipe '|' operator be used. |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function canPipe() |
|
94
|
|
|
{ |
|
95
|
1 |
|
return !defined('PHP_WINDOWS_VERSION_BUILD'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Is there a command pipeline. |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
3 |
|
public function isPiped() |
|
104
|
|
|
{ |
|
105
|
3 |
|
return !empty($this->pipeline); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Return command pipeline. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
3 |
|
public function getPipeline() |
|
114
|
|
|
{ |
|
115
|
3 |
|
return $this->isPiped() ? ' | ' . implode(' | ', $this->pipeline) : ''; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Adds a cli command to list of commands to execute. |
|
120
|
|
|
* |
|
121
|
|
|
* @param \SebastianFeldmann\Cli\Command |
|
122
|
|
|
*/ |
|
123
|
4 |
|
public function addCommand(Command $cmd) |
|
124
|
|
|
{ |
|
125
|
4 |
|
$this->commands[] = $cmd; |
|
126
|
4 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Generates the system command. |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
4 |
|
public function getCommand() : string |
|
134
|
|
|
{ |
|
135
|
4 |
|
$amount = count($this->commands); |
|
136
|
4 |
|
if ($amount < 1) { |
|
137
|
1 |
|
throw new RuntimeException('no command to execute'); |
|
138
|
|
|
} |
|
139
|
3 |
|
$cmd = ($amount > 1 ? '(' . implode(' && ', $this->commands) . ')' : $this->commands[0]) |
|
140
|
3 |
|
. $this->getPipeline() |
|
141
|
3 |
|
. (!empty($this->redirectOutput) ? ' > ' . $this->redirectOutput : ''); |
|
142
|
|
|
|
|
143
|
3 |
|
return $cmd; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Returns the command to execute. |
|
148
|
|
|
* |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function __toString() : string |
|
152
|
|
|
{ |
|
153
|
1 |
|
return $this->getCommand(); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|