1 | <?php declare(strict_types=1); |
||
22 | final class GitWrapper |
||
23 | { |
||
24 | /** |
||
25 | * Path to the Git binary. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | private $gitBinary; |
||
30 | |||
31 | /** |
||
32 | * The timeout of the Git command in seconds. |
||
33 | * |
||
34 | * @var int |
||
35 | */ |
||
36 | private $timeout = 60; |
||
37 | |||
38 | /** |
||
39 | * Environment variables defined in the scope of the Git command. |
||
40 | * |
||
41 | * @var string[] |
||
42 | */ |
||
43 | private $env = []; |
||
44 | |||
45 | /** |
||
46 | * @var GitOutputListenerInterface |
||
47 | */ |
||
48 | private $gitOutputListener; |
||
49 | |||
50 | /** |
||
51 | * @var EventDispatcherInterface |
||
52 | */ |
||
53 | private $eventDispatcher; |
||
54 | |||
55 | public function __construct(?string $gitBinary = null) |
||
67 | |||
68 | public function getDispatcher(): EventDispatcherInterface |
||
76 | |||
77 | public function setDispatcher(EventDispatcherInterface $eventDispatcher): void |
||
81 | |||
82 | public function setGitBinary(string $gitBinary): void |
||
86 | |||
87 | public function getGitBinary(): string |
||
91 | |||
92 | /** |
||
93 | * @param mixed $value |
||
94 | */ |
||
95 | public function setEnvVar(string $var, $value): void |
||
99 | |||
100 | public function unsetEnvVar(string $var): void |
||
104 | |||
105 | /** |
||
106 | * Returns an environment variable that is defined only in the scope of the |
||
107 | * Git command. |
||
108 | * |
||
109 | * @param string $var The name of the environment variable, e.g. "HOME", "GIT_SSH". |
||
110 | * @param mixed $default The value returned if the environment variable is not set, defaults to |
||
111 | * null. |
||
112 | * |
||
113 | * @return mixed |
||
114 | */ |
||
115 | public function getEnvVar(string $var, $default = null) |
||
119 | |||
120 | /** |
||
121 | * @return mixed[] |
||
122 | */ |
||
123 | public function getEnvVars(): array |
||
127 | |||
128 | public function setTimeout(int $timeout): void |
||
132 | |||
133 | public function getTimeout(): int |
||
137 | |||
138 | /** |
||
139 | * Set an alternate private key used to connect to the repository. |
||
140 | * |
||
141 | * This method sets the GIT_SSH environment variable to use the wrapper |
||
142 | * script included with this library. It also sets the custom GIT_SSH_KEY |
||
143 | * and GIT_SSH_PORT environment variables that are used by the script. |
||
144 | * |
||
145 | * @param string|null $wrapper Path the the GIT_SSH wrapper script, defaults to null which uses the |
||
146 | * script included with this library. |
||
147 | */ |
||
148 | public function setPrivateKey(string $privateKey, int $port = 22, ?string $wrapper = null): void |
||
166 | |||
167 | /** |
||
168 | * Unsets the private key by removing the appropriate environment variables. |
||
169 | */ |
||
170 | public function unsetPrivateKey(): void |
||
176 | |||
177 | public function addOutputListener(GitOutputListenerInterface $gitOutputListener): void |
||
182 | |||
183 | public function addLoggerEventSubscriber(GitLoggerEventSubscriber $gitLoggerEventSubscriber): void |
||
184 | { |
||
185 | $this->getDispatcher() |
||
186 | ->addSubscriber($gitLoggerEventSubscriber); |
||
187 | } |
||
188 | |||
189 | public function removeOutputListener(GitOutputListenerInterface $gitOutputListener): void |
||
194 | |||
195 | /** |
||
196 | * Set whether or not to stream real-time output to STDOUT and STDERR. |
||
197 | */ |
||
198 | public function streamOutput(bool $streamOutput = true): void |
||
210 | |||
211 | /** |
||
212 | * Returns an object that interacts with a working copy. |
||
213 | * |
||
214 | * @param string $directory Path to the directory containing the working copy. |
||
215 | */ |
||
216 | public function workingCopy(string $directory): GitWorkingCopy |
||
220 | |||
221 | /** |
||
222 | * Returns the version of the installed Git client. |
||
223 | */ |
||
224 | public function version(): string |
||
228 | |||
229 | /** |
||
230 | * For example, passing the "[email protected]:cpliakas/git-wrapper.git" |
||
231 | * repository would return "git-wrapper". |
||
232 | */ |
||
233 | public static function parseRepositoryName(string $repositoryUrl): string |
||
248 | |||
249 | /** |
||
250 | * Executes a `git init` command. |
||
251 | * |
||
252 | * Create an empty git repository or reinitialize an existing one. |
||
253 | * |
||
254 | * @param mixed[] $options An associative array of command line options. |
||
255 | */ |
||
256 | public function init(string $directory, array $options = []): GitWorkingCopy |
||
264 | |||
265 | /** |
||
266 | * Executes a `git clone` command and returns a working copy object. |
||
267 | * |
||
268 | * Clone a repository into a new directory. Use @see GitWorkingCopy::cloneRepository() |
||
269 | * instead for more readable code. |
||
270 | * |
||
271 | * @param string $repository The Git URL of the repository being cloned. |
||
272 | * @param string $directory The directory that the repository will be cloned into. If null is |
||
273 | * passed, the directory will automatically be generated from the URL via |
||
274 | * the GitWrapper::parseRepositoryName() method. |
||
275 | * @param mixed[] $options An associative array of command line options. |
||
276 | */ |
||
277 | public function cloneRepository(string $repository, ?string $directory = null, array $options = []): GitWorkingCopy |
||
288 | |||
289 | /** |
||
290 | * The command is simply a raw command line entry for everything after the Git binary. |
||
291 | * For example, a `git config -l` command would be passed as `config -l` via the first argument of this method. |
||
292 | * |
||
293 | * @return string The STDOUT returned by the Git command. |
||
294 | */ |
||
295 | public function git(string $commandLine, ?string $cwd = null): string |
||
302 | |||
303 | /** |
||
304 | * @return string The STDOUT returned by the Git command. |
||
305 | */ |
||
306 | public function run(GitCommand $gitCommand, ?string $cwd = null): string |
||
316 | } |
||
317 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: