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