|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the skeleton package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Gennady Knyazkin <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Gennadyx\Skeleton\VarSource; |
|
15
|
|
|
|
|
16
|
|
|
use Composer\Util\ProcessExecutor; |
|
17
|
|
|
use Gennadyx\Skeleton\Exception\RuntimeException; |
|
18
|
|
|
use Symfony\Component\Process\ExecutableFinder; |
|
19
|
|
|
use Symfony\Component\Process\Process; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class GitConfig |
|
23
|
|
|
* |
|
24
|
|
|
* @author Gennady Knyazkin <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class GitConfig extends AbstractSource |
|
27
|
|
|
{ |
|
28
|
|
|
use PreloadTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get all variables |
|
32
|
|
|
* |
|
33
|
|
|
* @return array |
|
34
|
|
|
* @throws RuntimeException |
|
35
|
|
|
*/ |
|
36
|
2 |
|
protected function preload(): array |
|
37
|
|
|
{ |
|
38
|
2 |
|
$conf = $this->getConfig(); |
|
39
|
2 |
|
$vars = []; |
|
40
|
|
|
|
|
41
|
2 |
|
foreach ($this->getVarsAssociations() as $sv => $gv) { |
|
42
|
2 |
|
$vars[$sv] = isset($conf[$gv]) ? $conf[$gv] : null; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
return $vars; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
2 |
|
private function getVarsAssociations(): array |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
2 |
|
'author_name' => 'user.name', |
|
55
|
|
|
'author_email' => 'user.email' |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get git config |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
* @throws \Gennadyx\Skeleton\Exception\RuntimeException |
|
64
|
|
|
*/ |
|
65
|
2 |
|
private function getConfig(): array |
|
66
|
|
|
{ |
|
67
|
2 |
|
$conf = []; |
|
68
|
|
|
|
|
69
|
|
|
try { |
|
70
|
2 |
|
$gitBin = $this->findExecutable(); |
|
71
|
2 |
|
$cmd = $this->executeGitConfig($gitBin); |
|
72
|
|
|
|
|
73
|
2 |
|
if ($cmd->isSuccessful()) { |
|
74
|
2 |
|
$conf = $this->parseOutput($cmd->getOutput()); |
|
75
|
|
|
} |
|
76
|
|
|
} catch (\Exception $e) { |
|
77
|
|
|
throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
return $conf; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get git binary for exec |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
2 |
|
private function findExecutable(): string |
|
89
|
|
|
{ |
|
90
|
2 |
|
$finder = new ExecutableFinder(); |
|
91
|
2 |
|
$gitBin = $finder->find('git'); |
|
92
|
|
|
|
|
93
|
2 |
|
return ProcessExecutor::escape($gitBin); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Execute git config command |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $gitBin |
|
100
|
|
|
* |
|
101
|
|
|
* @return Process |
|
102
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException |
|
103
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
|
104
|
|
|
*/ |
|
105
|
2 |
|
private function executeGitConfig(string $gitBin): Process |
|
106
|
|
|
{ |
|
107
|
2 |
|
$cmd = new Process(sprintf('%s config -l', $gitBin)); |
|
108
|
2 |
|
$cmd->run(); |
|
109
|
|
|
|
|
110
|
2 |
|
return $cmd; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $output |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
2 |
|
private function parseOutput(string $output): array |
|
119
|
|
|
{ |
|
120
|
2 |
|
$config = []; |
|
121
|
2 |
|
$matches = []; |
|
122
|
2 |
|
preg_match_all('{^([^=]+)=(.*)$}m', $output, $matches, PREG_SET_ORDER); |
|
123
|
|
|
|
|
124
|
2 |
|
if (is_array($matches)) { |
|
125
|
2 |
|
foreach ($matches as $match) { |
|
126
|
2 |
|
$config[$match[1]] = $match[2]; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
2 |
|
return $config; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|