|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Runs a command using the process component. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\GeneratorBundle; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
10
|
|
|
use Symfony\Component\Process\Process; |
|
11
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
16
|
|
|
* @link http://swisscom.ch |
|
17
|
|
|
*/ |
|
18
|
|
|
class CommandRunner |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var Process */ |
|
21
|
|
|
private $process; |
|
22
|
|
|
/** @var KernelInterface */ |
|
23
|
|
|
private $kernel; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param KernelInterface $kernel Application kernel |
|
27
|
|
|
* @param Process $process Process component |
|
28
|
|
|
*/ |
|
29
|
8 |
|
public function __construct(KernelInterface $kernel, Process $process) |
|
30
|
|
|
{ |
|
31
|
8 |
|
$this->process = $process; |
|
32
|
8 |
|
$this->kernel = $kernel; |
|
33
|
8 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Executes a app/console command |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $args Arguments |
|
40
|
|
|
* @param OutputInterface $output Output |
|
41
|
|
|
* @param string $message Message to be shown on error. |
|
42
|
|
|
* |
|
43
|
|
|
* @return integer|null Exit code |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function executeCommand(array $args, OutputInterface $output, $message) |
|
46
|
|
|
{ |
|
47
|
2 |
|
$name = $args[0]; |
|
48
|
2 |
|
$cmd = $this->getCmd($args); |
|
49
|
|
|
|
|
50
|
2 |
|
$output->writeln(''); |
|
51
|
2 |
|
$output->writeln( |
|
52
|
2 |
|
sprintf( |
|
53
|
2 |
|
'<info>Running %s</info>', |
|
54
|
2 |
|
$name |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
2 |
|
$output->writeln( |
|
59
|
2 |
|
sprintf( |
|
60
|
2 |
|
'<comment>%s</comment>', |
|
61
|
2 |
|
$cmd |
|
62
|
|
|
) |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
2 |
|
$this->process->setCommandLine($cmd); |
|
66
|
2 |
|
$this->process->run( |
|
67
|
2 |
|
function ($type, $buffer) use ($output, $cmd) { |
|
68
|
|
|
if (Process::ERR === $type) { |
|
69
|
|
|
$output->writeln( |
|
70
|
|
|
sprintf( |
|
71
|
|
|
'<error>%s</error>', |
|
72
|
|
|
$buffer |
|
73
|
|
|
) |
|
74
|
|
|
); |
|
75
|
|
|
} else { |
|
76
|
|
|
$output->writeln( |
|
77
|
|
|
sprintf( |
|
78
|
|
|
'<comment>%s</comment>', |
|
79
|
|
|
$buffer |
|
80
|
|
|
) |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
2 |
|
} |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
2 |
|
if (!$this->process->isSuccessful()) { |
|
87
|
2 |
|
throw new \RuntimeException($message . '<error>' . $this->process->getErrorOutput() . '</error>'); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* get subcommand |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $args args |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
2 |
|
private function getCmd(array $args) |
|
99
|
|
|
{ |
|
100
|
|
|
// get path to console from kernel.. |
|
101
|
2 |
|
$consolePath = $this->kernel->getRootDir() . '/console'; |
|
102
|
|
|
|
|
103
|
|
|
// this code was copied from Symfony\Component\Process\PhpProcess and deserves a cleanup |
|
104
|
2 |
|
$executableFinder = new PhpExecutableFinder(); |
|
105
|
2 |
|
if (false === $php = $executableFinder->find()) { |
|
106
|
|
|
$php = null; |
|
107
|
|
|
} |
|
108
|
2 |
|
if ('\\' !== DIRECTORY_SEPARATOR && null !== $php) { |
|
109
|
|
|
// exec is mandatory to deal with sending a signal to the process |
|
110
|
|
|
// see https://github.com/symfony/symfony/issues/5030 about prepending |
|
111
|
|
|
// command with exec |
|
112
|
2 |
|
$php = 'exec '.$php; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
$cmd = $php.' '.$consolePath.' -n '; |
|
116
|
|
|
|
|
117
|
2 |
|
foreach ($args as $key => $val) { |
|
118
|
2 |
|
if (strlen($key) > 1) { |
|
119
|
|
|
$cmd .= ' ' . $key; |
|
120
|
|
|
} |
|
121
|
2 |
|
if (strlen($key) > 1 && !is_null($val)) { |
|
122
|
|
|
$cmd .= '='; |
|
123
|
|
|
} |
|
124
|
2 |
|
if (strlen($val) > 1) { |
|
125
|
2 |
|
$cmd .= escapeshellarg($val); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
2 |
|
return $cmd; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|