1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of codeception-phiremock-extension. |
4
|
|
|
* |
5
|
|
|
* phiremock-codeception-extension is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* phiremock-codeception-extension is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with phiremock-codeception-extension. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Codeception\Extension; |
20
|
|
|
|
21
|
|
|
use Symfony\Component\Process\Process; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Manages the current running Phiremock process. |
25
|
|
|
*/ |
26
|
|
|
class PhiremockProcessManager |
27
|
|
|
{ |
28
|
|
|
/** @var Process[] */ |
29
|
|
|
private $processes; |
30
|
|
|
|
31
|
|
|
/** @var callable */ |
32
|
|
|
private $output; |
33
|
|
|
|
34
|
|
|
public function __construct(callable $output) |
35
|
|
|
{ |
36
|
|
|
$this->processes = []; |
37
|
|
|
$this->output = $output; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function start(Config $config): void |
41
|
|
|
{ |
42
|
|
|
$commandBuilder = new CommandBuilder($config); |
43
|
|
|
$process = $this->initProcess($commandBuilder); |
44
|
|
|
call_user_func($this->output, 'Running ' . $process->getCommandLine()); |
45
|
|
|
$process->start(); |
46
|
|
|
$this->processes[$process->getPid()] = $process; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function stop(): void |
50
|
|
|
{ |
51
|
|
|
foreach ($this->processes as $pid => $process) { |
52
|
|
|
call_user_func($this->output, "Stopping phiremock process with pid: " . $pid); |
53
|
|
|
$process->stop(3); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function initProcess(CommandBuilder $builder): Process |
58
|
|
|
{ |
59
|
|
|
$commandline = $builder->build(); |
60
|
|
|
|
61
|
|
|
if (method_exists(Process::class, 'fromShellCommandline')) { |
62
|
|
|
return Process::fromShellCommandline(implode(' ', $commandline)); |
63
|
|
|
} |
64
|
|
|
return new Process(implode(' ', $commandline)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|