|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace GitWrapper; |
|
4
|
|
|
|
|
5
|
|
|
use GitWrapper\Event\GitEvent; |
|
6
|
|
|
use GitWrapper\Event\GitEvents; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Symfony\Component\Process\Process; |
|
9
|
|
|
|
|
10
|
|
|
final class GitProcess extends Process |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var GitWrapper |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $gitWrapper; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var GitCommand |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $gitCommand; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(GitWrapper $gitWrapper, GitCommand $gitCommand, ?string $cwd = null) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->gitWrapper = $gitWrapper; |
|
25
|
|
|
$this->gitCommand = $gitCommand; |
|
26
|
|
|
|
|
27
|
|
|
// Build the command line options, flags, and arguments. |
|
28
|
|
|
$gitCommandLine = $gitCommand->getCommandLine(); |
|
29
|
|
|
$commandLine = array_merge([$gitWrapper->getGitBinary()], (array) $gitCommandLine); |
|
30
|
|
|
|
|
31
|
|
|
// Support for executing an arbitrary git command. |
|
32
|
|
|
if (is_string($gitCommandLine)) { |
|
33
|
|
|
$commandLine = implode(' ', $commandLine); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// Resolve the working directory of the Git process. Use the directory |
|
37
|
|
|
// in the command object if it exists. |
|
38
|
|
|
if ($cwd === null) { |
|
39
|
|
|
$directory = $gitCommand->getDirectory(); |
|
40
|
|
|
if ($directory !== null) { |
|
41
|
|
|
if (! $cwd = realpath($directory)) { |
|
42
|
|
|
throw new GitException('Path to working directory could not be resolved: ' . $directory); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Finalize the environment variables, an empty array is converted |
|
48
|
|
|
// to null which enherits the environment of the PHP process. |
|
49
|
|
|
$env = $gitWrapper->getEnvVars(); |
|
50
|
|
|
if (! $env) { |
|
|
|
|
|
|
51
|
|
|
$env = null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
parent::__construct($commandLine, $cwd, $env, null, (float) $gitWrapper->getTimeout()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function start(?callable $callback = null, array $env = []): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->dispatchGitEvent(GitEvents::GIT_PREPARE); |
|
63
|
|
|
|
|
64
|
|
|
if ($this->gitCommand->notBypassed()) { |
|
65
|
|
|
parent::start($callback, $env); |
|
66
|
|
|
} else { |
|
67
|
|
|
$this->dispatchGitEvent(GitEvents::GIT_BYPASS); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function wait(?callable $callback = null): int |
|
72
|
|
|
{ |
|
73
|
|
|
if (! $this->gitCommand->notBypassed()) { |
|
74
|
|
|
return -1; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
try { |
|
78
|
|
|
$exitCode = parent::wait($callback); |
|
79
|
|
|
|
|
80
|
|
|
if ($this->isSuccessful()) { |
|
81
|
|
|
$this->dispatchGitEvent(GitEvents::GIT_SUCCESS); |
|
82
|
|
|
} else { |
|
83
|
|
|
$output = $this->getErrorOutput(); |
|
84
|
|
|
|
|
85
|
|
|
if (trim($output) === '') { |
|
86
|
|
|
$output = $this->getOutput(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
throw new GitException($output); |
|
90
|
|
|
} |
|
91
|
|
|
} catch (RuntimeException $runtimeException) { |
|
92
|
|
|
$this->dispatchGitEvent(GitEvents::GIT_ERROR); |
|
93
|
|
|
throw new GitException($runtimeException->getMessage(), $runtimeException->getCode(), $runtimeException); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $exitCode; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function dispatchGitEvent(string $eventName): void |
|
100
|
|
|
{ |
|
101
|
|
|
$this->gitWrapper->getDispatcher()->dispatch( |
|
102
|
|
|
$eventName, |
|
103
|
|
|
new GitEvent($this->gitWrapper, $this, $this->gitCommand) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.