|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Application\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command as BaseCommand; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
8
|
|
|
use Symfony\Component\Process\Process; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Base Command |
|
12
|
|
|
* |
|
13
|
|
|
* @method \Application\AppConsole getApplication() This allows the auto-completion to work correctly |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class Command extends BaseCommand |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var \Symfony\Component\Console\Input\InputInterface */ |
|
18
|
|
|
protected $input; |
|
19
|
|
|
|
|
20
|
|
|
/** @var \Symfony\Component\Console\Output\OutputInterface */ |
|
21
|
|
|
protected $output; |
|
22
|
|
|
|
|
23
|
|
|
/** @var \Symfony\Component\Console\Helper\FormatterHelper */ |
|
24
|
|
|
protected $formatter; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input The input instance |
|
28
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output The output instance |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->input = $input; |
|
33
|
|
|
$this->output = $output; |
|
34
|
|
|
$this->formatter = $this->getHelper('formatter'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Output a command's title |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $message Message |
|
41
|
|
|
* @param string $styles Styles |
|
42
|
|
|
* @param bool $large Large block or not |
|
43
|
|
|
* @param bool $breakAfter Break line after the block |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function outputBlock($message, $styles = 'question', $large = true, $breakAfter = true) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->output->writeln($this->formatter->formatBlock($message, $styles, $large)); |
|
48
|
|
|
if ($breakAfter) { |
|
49
|
|
|
$this->output->write(PHP_EOL); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Output an info |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $section Section of the message |
|
57
|
|
|
* @param string $message Message |
|
58
|
|
|
* @param string $styles Styles |
|
59
|
|
|
* @param bool $breakBefore Break line before the section |
|
60
|
|
|
* @param bool $breakAfter Break line after the section |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function outputSection($section, $message, $styles = 'comment', $breakBefore = false, $breakAfter = false) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($breakBefore) { |
|
65
|
|
|
$this->output->write(PHP_EOL); |
|
66
|
|
|
} |
|
67
|
|
|
$this->output->write($this->formatter->formatSection($section, $message, $styles)); |
|
68
|
|
|
if ($breakAfter) { |
|
69
|
|
|
$this->output->write(PHP_EOL); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $command Command to run |
|
75
|
|
|
* @param bool $liveOutput Define if we display the output of the command in live |
|
76
|
|
|
* @param int $timeout Timeout for the command to fail |
|
77
|
|
|
* @param bool $throwExceptions Define if we want the method to throw exceptions |
|
78
|
|
|
* @param bool $oneLiner Puts the command on one line |
|
79
|
|
|
* |
|
80
|
|
|
* @return \Symfony\Component\Process\Process |
|
81
|
|
|
* @throws \RuntimeException |
|
82
|
|
|
* |
|
83
|
|
|
* This method must remain public as it's used in closures. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function runProcess( |
|
86
|
|
|
$command, |
|
87
|
|
|
$liveOutput = false, |
|
88
|
|
|
$timeout = 120, |
|
89
|
|
|
$throwExceptions = true, |
|
90
|
|
|
$oneLiner = false |
|
91
|
|
|
) { |
|
92
|
|
|
if ($oneLiner) { |
|
93
|
|
|
$command = preg_replace('/\s+/', ' ', $command); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Instantiate a process object |
|
97
|
|
|
$process = new Process($command); |
|
98
|
|
|
$process->setTimeout($timeout); |
|
99
|
|
|
$process->run($liveOutput ? array($this, 'outputProcess') : null); |
|
100
|
|
|
|
|
101
|
|
|
// Check the process result |
|
102
|
|
|
if ($throwExceptions && !$process->isSuccessful()) { |
|
103
|
|
|
throw new \RuntimeException($process->getErrorOutput().' // '.$process->getOutput()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// Return the console output of the process |
|
107
|
|
|
return $process; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Output the process's output in live time |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $type Type of the output |
|
114
|
|
|
* @param string $buffer Output string |
|
115
|
|
|
* |
|
116
|
|
|
* @internal Warning ! This class is used by runProcess() and must remain public in order to work |
|
117
|
|
|
*/ |
|
118
|
|
|
public function outputProcess($type = '', $buffer = '') |
|
119
|
|
|
{ |
|
120
|
|
|
if (false === strpos($buffer, 'stdin')) { |
|
121
|
|
|
$this->output->write((('err' === $type) ? 'WARNING: ' : '').$buffer); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|