1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Support\Console; |
4
|
|
|
|
5
|
|
|
use App\Support\Contracts\Cli as CliContract; |
6
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
|
9
|
|
|
class Cli implements CliContract |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The process timeout in seconds. |
13
|
|
|
* |
14
|
|
|
* @var int|null |
15
|
|
|
*/ |
16
|
|
|
protected $timeout = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Execute a command. |
20
|
|
|
* |
21
|
|
|
* @param string $command |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
6 |
|
public function exec($command) |
26
|
|
|
{ |
27
|
6 |
|
$process = $this->getProcess($command); |
28
|
6 |
|
$process->run(); |
29
|
|
|
|
30
|
6 |
|
return $process->getOutput(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Execute a command in real time. |
35
|
|
|
* |
36
|
|
|
* @param string $command |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
2 |
|
public function execRealTime($command) |
41
|
|
|
{ |
42
|
2 |
|
$process = $this->getProcess($command); |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
$process->mustRun(function ($type, $buffer) { |
46
|
1 |
|
echo $buffer; |
47
|
2 |
|
}); |
48
|
|
|
} catch (ProcessFailedException $e) { |
49
|
|
|
echo $e->getMessage(); |
50
|
|
|
} |
51
|
2 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Execute a command and allow the user to interact with it. |
55
|
|
|
* |
56
|
|
|
* @param string $command |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
1 |
|
public function passthru($command) |
61
|
|
|
{ |
62
|
1 |
|
$process = $this->getProcess($command); |
63
|
|
|
|
64
|
|
|
try { |
65
|
1 |
|
$process->setTty(true); |
66
|
|
|
$process->mustRun(function ($type, $buffer) { |
67
|
|
|
echo $buffer; |
68
|
1 |
|
}); |
69
|
|
|
} catch (ProcessFailedException $e) { |
70
|
|
|
echo $e->getMessage(); |
71
|
|
|
} |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get a Symfony process object that can execute a command. |
76
|
|
|
* |
77
|
|
|
* @param string $command The command to execute |
78
|
|
|
* |
79
|
|
|
* @return Process |
80
|
|
|
*/ |
81
|
8 |
|
protected function getProcess($command) |
82
|
|
|
{ |
83
|
8 |
|
return app()->make(Process::class, [ |
84
|
8 |
|
'command' => $command, |
85
|
8 |
|
'timeout' => $this->timeout, |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return the current working directory. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
2 |
|
public function currentWorkingDirectory() |
95
|
|
|
{ |
96
|
2 |
|
return getcwd(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set the timeout for the wrapping PHP Process. |
101
|
|
|
* |
102
|
|
|
* @param int $seconds |
103
|
|
|
* |
104
|
|
|
* @return Cli |
105
|
|
|
*/ |
106
|
172 |
|
public function setTimeout(int $seconds) |
107
|
|
|
{ |
108
|
172 |
|
$this->timeout = $seconds; |
109
|
|
|
|
110
|
172 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Remove the timeout for the wrapping PHP Process. |
115
|
|
|
* |
116
|
|
|
* @return Cli |
117
|
|
|
*/ |
118
|
3 |
|
public function doNotTimeout() |
119
|
|
|
{ |
120
|
3 |
|
$this->timeout = null; |
121
|
|
|
|
122
|
3 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return the timeout for the wrapping PHP Process. |
127
|
|
|
* |
128
|
|
|
* @return int|null |
129
|
|
|
*/ |
130
|
4 |
|
public function getTimeout() |
131
|
|
|
{ |
132
|
4 |
|
return $this->timeout; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|