|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of CaptainHook. |
|
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 CaptainHook\Console\Application; |
|
11
|
|
|
|
|
12
|
|
|
use CaptainHook\Hook\Util; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Hook |
|
18
|
|
|
* |
|
19
|
|
|
* @package CaptainHook |
|
20
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
21
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
|
22
|
|
|
* @since Class available since Release 0.9.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class Hook extends ConfigHandler |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Path to the git repository |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $repositoryPath; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Hook that gets executed. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $hookToExecute; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Hook to command map |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $hookCommandMap = [ |
|
46
|
|
|
'pre-commit' => 'PreCommit', |
|
47
|
|
|
'commit-msg' => 'CommitMsg', |
|
48
|
|
|
'pre-push' => 'PrePush' |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Repository path setter. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $git |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function setRepositoryPath($git) |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->repositoryPath = $git; |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function getRepositoryPath() |
|
65
|
|
|
{ |
|
66
|
2 |
|
if (null === $this->repositoryPath) { |
|
67
|
1 |
|
$this->repositoryPath = getcwd(); |
|
68
|
|
|
} |
|
69
|
2 |
|
return $this->repositoryPath; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set the hook to execute. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $hook |
|
76
|
|
|
* @return \CaptainHook\Console\Application\Hook |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function setHook($hook) |
|
79
|
|
|
{ |
|
80
|
2 |
|
if (!Util::isValid($hook)) { |
|
81
|
1 |
|
throw new \RuntimeException('Invalid hook name'); |
|
82
|
|
|
} |
|
83
|
1 |
|
$this->hookToExecute = $hook; |
|
84
|
1 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Execute hook. |
|
89
|
|
|
* |
|
90
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
91
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
92
|
|
|
* @return int |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function doRun(InputInterface $input, OutputInterface $output) |
|
95
|
|
|
{ |
|
96
|
2 |
|
$input->setInteractive(false); |
|
97
|
|
|
|
|
98
|
2 |
|
$command = $this->createCommand(); |
|
99
|
1 |
|
return $command->run($input, $output); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Create the hook command. |
|
104
|
|
|
* |
|
105
|
|
|
* @return \CaptainHook\Console\Command\Hook |
|
106
|
|
|
*/ |
|
107
|
2 |
|
private function createCommand() |
|
108
|
|
|
{ |
|
109
|
|
|
/* @var \CaptainHook\Console\Command\Hook $command */ |
|
110
|
2 |
|
$class = '\\CaptainHook\\Console\\Command\\Hook\\' . $this->getHookCommand(); |
|
111
|
1 |
|
$command = new $class($this->getConfigFile(), $this->getRepositoryPath()); |
|
112
|
1 |
|
$command->setHelperSet($this->getHelperSet()); |
|
113
|
|
|
|
|
114
|
1 |
|
return $command; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get the command class name to execute. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
2 |
|
private function getHookCommand() |
|
123
|
|
|
{ |
|
124
|
2 |
|
if (null === $this->hookToExecute) { |
|
125
|
1 |
|
throw new \RuntimeException('No hook to execute'); |
|
126
|
|
|
} |
|
127
|
1 |
|
return $this->hookCommandMap[$this->hookToExecute]; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|