-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathHookCopier.php
More file actions
66 lines (52 loc) · 1.6 KB
/
HookCopier.php
File metadata and controls
66 lines (52 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace PhpGitHooks\Module\Configuration\Infrastructure\Hook;
use Symfony\Component\Process\Process;
class HookCopier
{
public const DEFAULT_GIT_HOOKS_DIR = '.git/hooks';
protected $hooksDir = self::DEFAULT_GIT_HOOKS_DIR;
protected $sourceHooksDir = __DIR__ . '/../../../../Infrastructure/Hook';
public function copyPreCommitHook(): void
{
$this->copyHookFile('pre-commit');
}
public function copyCommitMsgHook(): void
{
$this->copyHookFile('commit-msg');
}
public function copyPrePushHook(): void
{
$this->copyHookFile('pre-push');
}
public function copyPrepareCommitMsgHook(): void
{
$this->copyHookFile('prepare-commit-msg');
}
protected function hookExists(string $hookFile): bool
{
return \file_exists($this->hooksDir . "/$hookFile");
}
protected function copyFile(string $hookFile): void
{
$this->createHooksDir();
$copy = new Process(['cp', $hookFile, $this->hooksDir]);
$copy->run();
}
protected function createHooksDir(): void
{
$mkdir = new Process(['mkdir', '-p', $this->hooksDir]);
$mkdir->run();
}
protected function setPermissions(string $hookFile): void
{
$permissions = new Process(['chmod', '775', $this->hooksDir . "/$hookFile"]);
$permissions->run();
}
protected function copyHookFile(string $file): void
{
if (false === $this->hookExists($file)) {
$this->copyFile($this->sourceHooksDir . "/$file");
$this->setPermissions($file);
}
}
}