1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Schedule\Task\Runner; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Application; |
6
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task; |
9
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask; |
10
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\Result; |
11
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\TaskRunner; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Kevin Bond <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
final class CommandTaskRunner implements TaskRunner |
17
|
|
|
{ |
18
|
|
|
/** @var Application */ |
19
|
|
|
private $application; |
20
|
4 |
|
|
21
|
|
|
public function __construct(Application $application) |
22
|
4 |
|
{ |
23
|
4 |
|
$this->application = $application; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param CommandTask $task |
28
|
3 |
|
*/ |
29
|
|
|
public function __invoke(Task $task): Result |
30
|
3 |
|
{ |
31
|
3 |
|
$shellVerbosityResetter = new ShellVerbosityResetter(); |
32
|
3 |
|
$output = new BufferedOutput(); |
33
|
|
|
$this->application->setCatchExceptions(false); |
34
|
|
|
$this->application->setAutoExit(false); |
35
|
3 |
|
|
36
|
1 |
|
try { |
37
|
1 |
|
$exitCode = $this->application->run($task->createCommandInput($this->application), $output); |
|
|
|
|
38
|
|
|
} catch (\Throwable $e) { |
39
|
|
|
return Result::exception($task, $e, $output->fetch()); |
40
|
2 |
|
} finally { |
41
|
1 |
|
$shellVerbosityResetter->reset(); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
if (0 === $exitCode) { |
45
|
|
|
return Result::successful($task, $output->fetch()); |
46
|
|
|
} |
47
|
1 |
|
|
48
|
|
|
return Result::failure($task, "Exit {$exitCode}: {$this->getFailureMessage($exitCode)}", $output->fetch()); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
public function supports(Task $task): bool |
52
|
1 |
|
{ |
53
|
|
|
return $task instanceof CommandTask; |
54
|
1 |
|
} |
55
|
1 |
|
|
56
|
|
|
private function getFailureMessage(int $exitCode): string |
57
|
|
|
{ |
58
|
|
|
if (\class_exists(Process::class) && isset(Process::$exitCodes[$exitCode])) { |
59
|
|
|
return Process::$exitCodes[$exitCode]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return 'Unknown error'; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|