forked from opendevshop/devshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerProcess.php
More file actions
125 lines (106 loc) · 3.7 KB
/
PowerProcess.php
File metadata and controls
125 lines (106 loc) · 3.7 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace DevShop\Component\PowerProcess;
use Psr\Log\LoggerAwareTrait;
use Robo\Common\OutputAwareTrait;
use Robo\Common\TimeKeeper;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process as BaseProcess;
use Symfony\Component\Console\Style\SymfonyStyle;
class PowerProcess extends BaseProcess {
/**
* @var PowerProcessStyle
*/
public $io;
public $successMessage = 'Process Succeeded';
public $failureMessage = 'Process Failed';
public $duration = '';
/**
* @param $io PowerProcessStyle
*/
public function setIo(PowerProcessStyle $io) {
$this->io = $io;
}
/**
* PowerProcess constructor.
* @param string $commandline
* @param PowerProcessStyle $io
* @param null $cwd
* @param array|null $env
* @param null $input
* @param int $timeout
* @param array $options
*/
public function __construct($commandline, PowerProcessStyle $io, $cwd = null, array $env = null, $input = null, $timeout = 60, array $options = array())
{
$this->setIo($io);
// Detect Symfony Process 4.x and up: if so, make $commandline an array.
if (property_exists(BaseProcess::class, 'STATUS_READY')) {
// @TODO: What is better? a single item array? or should we explode by spaces?
$commandline = [$commandline];
}
parent::__construct($commandline, $cwd, $env, $input, $timeout, $options);
}
/**
* Runs the process.
*
* @param callable|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @return int The exit status code
*
* @throws RuntimeException When process can't be launched
* @throws RuntimeException When process stopped after receiving signal
* @throws LogicException In case a callback is provided and output has been disabled
*
* @final since version 3.3
*/
public function run($callback = null, $env = [])
{
// Handle a null $env variable.
if (!array($env)) {
$env = [];
}
$this->io->writeln(" <comment>$</comment> {$this->getCommandLine()} <fg=black>Output:/path/to/file</>");
$timer = new TimeKeeper();
$timer->start();
if ($this->io->isDebug()) {
$this->io->table(["Execution Environment"], $this->getEnvTableRows());
}
$exit = parent::run(function ($type, $buffer) {
if (getenv('PROVISION_PROCESS_OUTPUT') == 'direct') {
echo $buffer;
}
else {
$lines = explode("\n", $buffer);
foreach ($lines as $line) {
$this->io->outputBlock(trim($line), false, false);
}
}
}, $env);
$timer->stop();
$this->duration = $timer->formatDuration($timer->elapsed());
// @TODO: Optionally print something helpful but hideable here.
// $suffix = "<fg=black>Output: /path/to/file</>";
$suffix = '';
if ($exit == 0) {
$this->io->newLine();
$this->io->writeln(" <info>✔</info> {$this->successMessage} in {$this->duration} $suffix");
}
else {
$this->io->newLine();
$this->io->writeln(" <fg=red>✘</> {$this->failureMessage} in {$this->duration} {$suffix}");
}
return $exit;
}
/**
* Convert the execution environment to table row arrays.
*/
public function getEnvTableRows() {
$rows = [];
foreach ($this->getEnv() as $name => $value) {
$rows[] = [$name, $value];
}
return $rows;
}
}