|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace uuf6429\ElderBrother\Vcs\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use uuf6429\ElderBrother\Event\Git as GitEvent; |
|
6
|
|
|
|
|
7
|
|
|
class Git extends Adapter |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* {@inheritdoc} |
|
11
|
|
|
*/ |
|
12
|
|
|
public function isAvailable() |
|
13
|
|
|
{ |
|
14
|
|
|
return is_dir($this->getHookPath()); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritdoc} |
|
19
|
|
|
*/ |
|
20
|
|
|
public function isInstalled() |
|
21
|
|
|
{ |
|
22
|
|
|
return file_exists($this->getInstallLockFile()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
public function install() |
|
29
|
|
|
{ |
|
30
|
|
|
$hookFiles = $this->getHookFiles(); |
|
31
|
|
|
|
|
32
|
|
|
// create install lock |
|
33
|
|
|
fclose(fopen($this->getInstallLockFile(), 'x')); |
|
34
|
|
|
|
|
35
|
|
|
foreach ($hookFiles as $event => $file) { |
|
36
|
|
|
// back up existing hook |
|
37
|
|
|
if (file_exists($file)) { |
|
38
|
|
|
rename($file, $file . '.bak'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// create new hook file |
|
42
|
|
|
file_put_contents( |
|
43
|
|
|
$file, |
|
44
|
|
|
sprintf( |
|
45
|
|
|
'#!/bin/sh%sphp -f %s -- run -e %s%s', |
|
46
|
|
|
PHP_EOL, |
|
47
|
|
|
escapeshellarg(ELDER_BROTHER_BIN), |
|
48
|
|
|
escapeshellarg($event), |
|
49
|
|
|
PHP_EOL |
|
50
|
|
|
) |
|
51
|
|
|
); |
|
52
|
|
|
chmod($file, 0755); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function uninstall() |
|
60
|
|
|
{ |
|
61
|
|
|
$hookFiles = $this->getHookFiles(); |
|
62
|
|
|
|
|
63
|
|
|
foreach ($hookFiles as $file) { |
|
64
|
|
|
// remove hook file |
|
65
|
|
|
unlink($file); |
|
66
|
|
|
|
|
67
|
|
|
// restore backed up hook |
|
68
|
|
|
if (file_exists($file . '.bak')) { |
|
69
|
|
|
rename($file . '.bak', $file); |
|
70
|
|
|
chmod($file, 0755); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// remove install lock |
|
75
|
|
|
unlink($this->getInstallLockFile()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getHookPath() |
|
82
|
|
|
{ |
|
83
|
|
|
return PROJECT_ROOT |
|
84
|
|
|
. '.git' . DIRECTORY_SEPARATOR |
|
85
|
|
|
. 'hooks' . DIRECTORY_SEPARATOR; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getInstallLockFile() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->getHookPath() . 'ebi.lock'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return string[] |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function getHookFiles() |
|
100
|
|
|
{ |
|
101
|
|
|
$hookPath = $this->getHookPath(); |
|
102
|
|
|
|
|
103
|
|
|
$result = []; |
|
104
|
|
|
foreach ((new \ReflectionClass(GitEvent::class))->getConstants() as $eventName) { |
|
105
|
|
|
$hook = explode(':', $eventName, 2); |
|
106
|
|
|
|
|
107
|
|
|
if (count($hook) === 2 && $hook[0] === 'git') { |
|
108
|
|
|
$result[$eventName] = $hookPath . $hook[1]; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $result; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|