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