|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of SebastianFeldmann\Git. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace SebastianFeldmann\Git\Operator; |
|
11
|
|
|
|
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
use SebastianFeldmann\Cli\Command\Runner\Result; |
|
14
|
|
|
use SebastianFeldmann\Git\Command\Config\Get; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Config |
|
18
|
|
|
* |
|
19
|
|
|
* @package SebastianFeldmann\Git |
|
20
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
21
|
|
|
* @link https://github.com/sebastianfeldmann/git |
|
22
|
|
|
* @since Class available since Release 1.0.2 |
|
23
|
|
|
*/ |
|
24
|
|
|
class Config extends Base |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Does git have a configuration key |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $name |
|
30
|
|
|
* @return boolean |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function has(string $name) : bool |
|
33
|
|
|
{ |
|
34
|
|
|
try { |
|
35
|
4 |
|
$result = $this->configCommand($name); |
|
36
|
2 |
|
} catch (RuntimeException $exception) { |
|
37
|
2 |
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
return $result->isSuccessful(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get a configuration key value |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $name |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function get(string $name) : string |
|
50
|
|
|
{ |
|
51
|
2 |
|
$result = $this->configCommand($name); |
|
52
|
|
|
|
|
53
|
2 |
|
return $result->getBufferedOutput()[0]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get config values without throwing exceptions |
|
58
|
|
|
* |
|
59
|
|
|
* You can provide a default value to return. |
|
60
|
|
|
* By default the return value on unset config values is the empty string. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $name Name of the config value to retrieve |
|
63
|
|
|
* @param string $default Value to return if config value is not set, empty string by default |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function getSafely(string $name, string $default = '') |
|
67
|
|
|
{ |
|
68
|
2 |
|
return $this->has($name) ? $this->get($name) : $default; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Run the get config command |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $name |
|
75
|
|
|
* @return \SebastianFeldmann\Cli\Command\Runner\Result |
|
76
|
|
|
*/ |
|
77
|
5 |
|
private function configCommand(string $name) : Result |
|
78
|
|
|
{ |
|
79
|
5 |
|
$cmd = (new Get($this->repo->getRoot())); |
|
80
|
5 |
|
$cmd->name($name); |
|
81
|
|
|
|
|
82
|
5 |
|
$result = $this->runner->run($cmd); |
|
83
|
|
|
|
|
84
|
3 |
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|