|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Deployee\Plugins\ShellTasks\Dispatcher; |
|
5
|
|
|
|
|
6
|
|
|
use Deployee\Plugins\Deploy\Definitions\Tasks\TaskDefinitionInterface; |
|
7
|
|
|
use Deployee\Plugins\Deploy\Dispatcher\DispatchResult; |
|
8
|
|
|
use Deployee\Plugins\Deploy\Dispatcher\DispatchResultInterface; |
|
9
|
|
|
use Deployee\Plugins\Deploy\Dispatcher\TaskDefinitionDispatcherInterface; |
|
10
|
|
|
use Deployee\Plugins\ShellTasks\Definitions\ShellTaskDefinition; |
|
11
|
|
|
use Deployee\Plugins\ShellTasks\Helper\ExecutableFinder; |
|
12
|
|
|
use Phizzl\PhpShellCommand\ShellCommand; |
|
13
|
|
|
|
|
14
|
|
|
class ShellTaskDefinitionDispatcher implements TaskDefinitionDispatcherInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ExecutableFinder |
|
18
|
|
|
*/ |
|
19
|
|
|
private $executableFinder; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param ExecutableFinder $executableFinder |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(ExecutableFinder $executableFinder) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->executableFinder = $executableFinder; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param TaskDefinitionInterface $taskDefinition |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
|
|
public function canDispatchTaskDefinition(TaskDefinitionInterface $taskDefinition): bool |
|
34
|
|
|
{ |
|
35
|
|
|
return $taskDefinition instanceof ShellTaskDefinition; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param TaskDefinitionInterface $taskDefinition |
|
40
|
|
|
* @return DispatchResult |
|
41
|
|
|
*/ |
|
42
|
|
|
public function dispatch(TaskDefinitionInterface $taskDefinition): DispatchResultInterface |
|
43
|
|
|
{ |
|
44
|
|
|
$definition = $taskDefinition->define(); |
|
45
|
|
|
$executable = $this->executableFinder->find($definition->get('executable')); |
|
46
|
|
|
$arguments = (string)$definition->get('arguments'); |
|
47
|
|
|
|
|
48
|
|
|
$cmd = new ShellCommand("$executable $arguments"); |
|
49
|
|
|
$return = $cmd->run(); |
|
50
|
|
|
|
|
51
|
|
|
return $return->getExitCode() > 0 |
|
52
|
|
|
? new DispatchResult( |
|
53
|
|
|
$return->getExitCode(), |
|
54
|
|
|
$return->getOutput(), |
|
55
|
|
|
sprintf('Error executing: %s (%s)' . PHP_EOL . '%s', $cmd->getCommand(), $return->getExecTime(), $return->getError()) |
|
56
|
|
|
) |
|
57
|
|
|
: new DispatchResult( |
|
58
|
|
|
$return->getExitCode(), |
|
59
|
|
|
sprintf('Executed command: %s (%s)' . PHP_EOL . '%s', $cmd->getCommand(), $return->getExitCode(), $return->getOutput()) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
} |