|
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
|
|
|
const TIMEOUT = 1200; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Execute a command. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $command |
|
20
|
|
|
* |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
8 |
|
public function exec($command) |
|
24
|
|
|
{ |
|
25
|
8 |
|
$process = $this->getProcess($command); |
|
26
|
8 |
|
$process->run(); |
|
27
|
|
|
|
|
28
|
8 |
|
return $process->getOutput(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Execute a command in real time. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $command |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
4 |
|
public function execRealTime($command) |
|
39
|
|
|
{ |
|
40
|
4 |
|
$process = $this->getProcess($command); |
|
41
|
|
|
|
|
42
|
|
|
try { |
|
43
|
|
|
$process->mustRun(function ($type, $buffer) { |
|
44
|
3 |
|
echo $buffer; |
|
45
|
4 |
|
}); |
|
46
|
2 |
|
} catch (ProcessFailedException $e) { |
|
47
|
2 |
|
echo $e->getMessage(); |
|
48
|
|
|
} |
|
49
|
4 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Execute a command and allow the user to interact with it. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $command |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function passthru($command) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$process = $this->getProcess($command); |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
1 |
|
$process->setTty(true); |
|
64
|
|
|
$process->mustRun(function ($type, $buffer) { |
|
65
|
|
|
echo $buffer; |
|
66
|
1 |
|
}); |
|
67
|
|
|
} catch (ProcessFailedException $e) { |
|
68
|
|
|
echo $e->getMessage(); |
|
69
|
|
|
} |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get a Symfony process object that can execute a command. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $command The command to execute |
|
76
|
|
|
* |
|
77
|
|
|
* @return Process |
|
78
|
|
|
*/ |
|
79
|
10 |
|
protected function getProcess($command) |
|
80
|
|
|
{ |
|
81
|
10 |
|
return app()->make(Process::class, [ |
|
82
|
10 |
|
'commandline' => $command, |
|
83
|
10 |
|
'timeout' => static::TIMEOUT, |
|
84
|
|
|
]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Return the current working directory. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function currentWorkingDirectory() |
|
93
|
|
|
{ |
|
94
|
2 |
|
return getcwd(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|