1 | <?php |
||
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 | 6 | public function __construct(KernelInterface $kernel, Process $process) |
|
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 | 1 | $name |
|
55 | 1 | ) |
|
56 | 1 | ); |
|
57 | |||
58 | 2 | $output->writeln( |
|
59 | 2 | sprintf( |
|
60 | 2 | '<comment>%s</comment>', |
|
61 | 1 | $cmd |
|
62 | 1 | ) |
|
63 | 1 | ); |
|
64 | |||
65 | 2 | $this->process->setCommandLine($cmd); |
|
66 | 2 | $this->process->run( |
|
67 | 1 | 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 | 1 | } |
|
84 | 1 | ); |
|
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) |
|
131 | } |
||
132 |