|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Infrastructure\Composer; |
|
4
|
|
|
|
|
5
|
|
|
use Composer\IO\IOInterface; |
|
6
|
|
|
use PhpGitHooks\Application\Composer\CommitMsgProcessor; |
|
7
|
|
|
use PhpGitHooks\Application\Composer\PreCommitProcessor; |
|
8
|
|
|
use PhpGitHooks\Application\Composer\PrePushProcessor; |
|
9
|
|
|
use PhpGitHooks\Infrastructure\Disk\Config\ConfigFileReaderInterface; |
|
10
|
|
|
use PhpGitHooks\Infrastructure\Disk\Config\ConfigFileWriterInterface; |
|
11
|
|
|
use PhpGitHooks\Infrastructure\Git\HooksFileCopier; |
|
12
|
|
|
|
|
13
|
|
|
final class ConfiguratorProcessor |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var IOInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $IO; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ConfigFileReaderInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $configFileReader; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var PreCommitProcessor |
|
25
|
|
|
*/ |
|
26
|
|
|
private $preCommitProcessor; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var CommitMsgProcessor |
|
29
|
|
|
*/ |
|
30
|
|
|
private $commitMsgProcessor; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var ConfigFileWriterInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $configFileWriter; |
|
35
|
|
|
/** @var HooksFileCopier */ |
|
36
|
|
|
private $hookFileCopier; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var PrePushProcessor |
|
39
|
|
|
*/ |
|
40
|
|
|
private $prePushProcessor; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param ConfigFileReaderInterface $configFileReader |
|
44
|
|
|
* @param PreCommitProcessor $preCommitProcessor |
|
45
|
|
|
* @param CommitMsgProcessor $commitMsgProcessor |
|
46
|
|
|
* @param ConfigFileWriterInterface $configFileWriter |
|
47
|
|
|
* @param HooksFileCopier $hooksFileCopier |
|
48
|
|
|
* @param PrePushProcessor $prePushProcessor |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct( |
|
51
|
|
|
ConfigFileReaderInterface $configFileReader, |
|
52
|
|
|
PreCommitProcessor $preCommitProcessor, |
|
53
|
|
|
CommitMsgProcessor $commitMsgProcessor, |
|
54
|
|
|
ConfigFileWriterInterface $configFileWriter, |
|
55
|
|
|
HooksFileCopier $hooksFileCopier, |
|
56
|
|
|
PrePushProcessor $prePushProcessor |
|
57
|
|
|
) { |
|
58
|
|
|
$this->configFileReader = $configFileReader; |
|
59
|
|
|
$this->preCommitProcessor = $preCommitProcessor; |
|
60
|
|
|
$this->commitMsgProcessor = $commitMsgProcessor; |
|
61
|
|
|
$this->configFileWriter = $configFileWriter; |
|
62
|
|
|
$this->hookFileCopier = $hooksFileCopier; |
|
63
|
|
|
$this->prePushProcessor = $prePushProcessor; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param IOInterface $iOInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setIO(IOInterface $iOInterface) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->IO = $iOInterface; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function process() |
|
75
|
|
|
{ |
|
76
|
|
|
$configData = $this->configFileReader->getFileContents(); |
|
77
|
|
|
$preCommitConfig = $this->preCommit($configData); |
|
78
|
|
|
$this->copyHook('pre-commit', $preCommitConfig['pre-commit']['enabled']); |
|
79
|
|
|
|
|
80
|
|
|
$commitMsgConfig = $this->commitMsg($configData); |
|
81
|
|
|
$this->copyHook('commit-msg', $commitMsgConfig['commit-msg']['enabled']); |
|
82
|
|
|
|
|
83
|
|
|
$prePushConfig = $this->prePush($configData); |
|
84
|
|
|
$this->copyHook('pre-push', $prePushConfig['pre-push']['enabled']); |
|
85
|
|
|
|
|
86
|
|
|
$merge['pre-commit'] = $preCommitConfig['pre-commit']; |
|
|
|
|
|
|
87
|
|
|
$merge['commit-msg'] = $commitMsgConfig['commit-msg']; |
|
88
|
|
|
$merge['pre-push'] = $prePushConfig['pre-push']; |
|
89
|
|
|
|
|
90
|
|
|
$this->configFileWriter->write($merge); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $configData |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
private function preCommit(array $configData) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->preCommitProcessor->setIO($this->IO); |
|
101
|
|
|
|
|
102
|
|
|
return $this->preCommitProcessor->execute($configData); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param array $configData |
|
107
|
|
|
* |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
|
|
private function commitMsg(array $configData) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->commitMsgProcessor->setIO($this->IO); |
|
113
|
|
|
|
|
114
|
|
|
return $this->commitMsgProcessor->execute($configData); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $hook |
|
119
|
|
|
* @param bool $enabled |
|
120
|
|
|
*/ |
|
121
|
|
|
private function copyHook($hook, $enabled) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->hookFileCopier->copy($hook, $enabled); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param $configData |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
private function prePush($configData) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->prePushProcessor->setIO($this->IO); |
|
134
|
|
|
|
|
135
|
|
|
return $this->prePushProcessor->execute($configData); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.