|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GitWrapper; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Process\Process; |
|
6
|
|
|
use Symfony\Component\Process\ProcessUtils; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* GitProcess runs a Git command in an independent process. |
|
10
|
|
|
*/ |
|
11
|
|
|
class GitProcess extends Process |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \GitWrapper\GitWrapper |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $git; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \GitWrapper\GitCommand |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $command; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructs a GitProcess object. |
|
25
|
|
|
* |
|
26
|
|
|
* @param \GitWrapper\GitWrapper $git |
|
27
|
|
|
* @param \GitWrapper\GitCommand $command |
|
28
|
|
|
* @param string|null $cwd |
|
29
|
|
|
*/ |
|
30
|
88 |
|
public function __construct(GitWrapper $git, GitCommand $command, $cwd = null) |
|
31
|
|
|
{ |
|
32
|
88 |
|
$this->git = $git; |
|
33
|
88 |
|
$this->command = $command; |
|
34
|
|
|
|
|
35
|
|
|
// Build the command line options, flags, and arguments. |
|
36
|
88 |
|
$binary = ProcessUtils::escapeArgument($git->getGitBinary()); |
|
37
|
88 |
|
$commandLine = rtrim($binary . ' ' . $command->getCommandLine()); |
|
38
|
|
|
|
|
39
|
|
|
// Resolve the working directory of the Git process. Use the directory |
|
40
|
|
|
// in the command object if it exists. |
|
41
|
88 |
|
if (null === $cwd) { |
|
42
|
88 |
|
if (null !== $directory = $command->getDirectory()) { |
|
43
|
56 |
|
if (!$cwd = realpath($directory)) { |
|
44
|
4 |
|
throw new GitException('Path to working directory could not be resolved: ' . $directory); |
|
45
|
|
|
} |
|
46
|
52 |
|
} |
|
47
|
84 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Finalize the environment variables, an empty array is converted |
|
50
|
|
|
// to null which enherits the environment of the PHP process. |
|
51
|
84 |
|
$env = $git->getEnvVars(); |
|
52
|
84 |
|
if (!$env) { |
|
|
|
|
|
|
53
|
84 |
|
$env = null; |
|
54
|
84 |
|
} |
|
55
|
|
|
|
|
56
|
84 |
|
parent::__construct($commandLine, $cwd, $env, null, $git->getTimeout(), $git->getProcOptions()); |
|
57
|
84 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
84 |
|
public function run($callback = null) |
|
63
|
|
|
{ |
|
64
|
84 |
|
$event = new Event\GitEvent($this->git, $this, $this->command); |
|
65
|
84 |
|
$dispatcher = $this->git->getDispatcher(); |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
|
|
|
|
69
|
|
|
// Throw the "git.command.prepare" event prior to executing. |
|
70
|
84 |
|
$dispatcher->dispatch(Event\GitEvents::GIT_PREPARE, $event); |
|
71
|
|
|
|
|
72
|
|
|
// Execute command if it is not flagged to be bypassed and throw the |
|
73
|
|
|
// "git.command.success" event, otherwise do not execute the comamnd |
|
74
|
|
|
// and throw the "git.command.bypass" event. |
|
75
|
84 |
|
if ($this->command->notBypassed()) { |
|
76
|
72 |
|
parent::run($callback); |
|
77
|
|
|
|
|
78
|
72 |
|
if ($this->isSuccessful()) { |
|
79
|
64 |
|
$dispatcher->dispatch(Event\GitEvents::GIT_SUCCESS, $event); |
|
80
|
64 |
|
} else { |
|
81
|
56 |
|
$output = $this->getErrorOutput(); |
|
82
|
|
|
|
|
83
|
56 |
|
if(trim($output) == '') { |
|
84
|
|
|
$output = $this->getOutput(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
56 |
|
throw new \RuntimeException($output); |
|
88
|
|
|
} |
|
89
|
64 |
|
} else { |
|
90
|
12 |
|
$dispatcher->dispatch(Event\GitEvents::GIT_BYPASS, $event); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
84 |
|
} catch (\RuntimeException $e) { |
|
94
|
56 |
|
$dispatcher->dispatch(Event\GitEvents::GIT_ERROR, $event); |
|
95
|
56 |
|
throw new GitException($e->getMessage()); |
|
96
|
|
|
} |
|
97
|
76 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
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.