1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-task library. |
5
|
|
|
* |
6
|
|
|
* (c) php-task |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Task\TaskBundle\Executor; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
15
|
|
|
use Task\Execution\TaskExecutionInterface; |
16
|
|
|
use Task\Runner\ExecutorInterface; |
17
|
|
|
use Task\TaskBundle\Command\ExecuteCommand; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Uses a process to start the executions via console-command. |
21
|
|
|
*/ |
22
|
|
|
class ProcessExecutor implements ExecutorInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $consoleFile; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $environment; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $consoleFile |
36
|
|
|
* @param string $environment |
37
|
|
|
*/ |
38
|
13 |
|
public function __construct($consoleFile, $environment) |
39
|
|
|
{ |
40
|
13 |
|
$this->consoleFile = $consoleFile; |
41
|
13 |
|
$this->environment = $environment; |
42
|
13 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
2 |
|
public function execute(TaskExecutionInterface $execution) |
48
|
|
|
{ |
49
|
2 |
|
$process = ProcessBuilder::create( |
50
|
2 |
|
[$this->consoleFile, 'task:execute', $execution->getUuid(), '-e ' . $this->environment] |
51
|
2 |
|
)->getProcess(); |
52
|
|
|
|
53
|
2 |
|
$process->run(); |
54
|
|
|
|
55
|
2 |
|
if (!$process->isSuccessful()) { |
56
|
1 |
|
throw new ProcessException($process->getErrorOutput()); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return $this->extractResult($process->getOutput()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Extract the result from output. |
64
|
|
|
* |
65
|
|
|
* @param string $output |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
1 |
|
private function extractResult($output) |
70
|
|
|
{ |
71
|
1 |
|
$match = preg_match( |
72
|
1 |
|
sprintf( |
73
|
1 |
|
'/%s(?<result>.*)%s/s', |
74
|
1 |
|
preg_quote(ExecuteCommand::START_RESULT), |
75
|
1 |
|
preg_quote(ExecuteCommand::END_RESULT) |
76
|
1 |
|
), |
77
|
1 |
|
$output, |
78
|
|
|
$matches |
79
|
1 |
|
); |
80
|
|
|
|
81
|
1 |
|
if (!$match) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $matches['result']; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|