|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of CaptainHook |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace CaptainHook\App\Hook\Template\Local; |
|
15
|
|
|
|
|
16
|
|
|
use CaptainHook\App\CH; |
|
17
|
|
|
use CaptainHook\App\Hook\Template; |
|
18
|
|
|
use SebastianFeldmann\Camino\Path; |
|
19
|
|
|
use SebastianFeldmann\Camino\Path\Directory; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Local class |
|
23
|
|
|
* |
|
24
|
|
|
* Generates the sourcecode for the php hook scripts in .git/hooks/*. |
|
25
|
|
|
* |
|
26
|
|
|
* @package CaptainHook |
|
27
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
28
|
|
|
* @link https://github.com/captainhookphp/captainhook |
|
29
|
|
|
* @since Class available since Release 4.3.0 |
|
30
|
|
|
*/ |
|
31
|
|
|
class PHP extends Template\Local |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Return the code for the git hook scripts |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $hook Name of the hook to generate the sourcecode for |
|
37
|
|
|
* @return array<string> |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getHookLines(string $hook): array |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->isPhar ? $this->getPharHookLines($hook) : $this->getSrcHookLines($hook); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Return the path to the target path from inside the .git/hooks directory f.e. __DIR__ ../../vendor |
|
46
|
|
|
* |
|
47
|
|
|
* @param \SebastianFeldmann\Camino\Path\Directory $repo |
|
48
|
|
|
* @param \SebastianFeldmann\Camino\Path $target |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function getPathForHookTo(Directory $repo, Path $target): string |
|
52
|
|
|
{ |
|
53
|
|
|
if (!$target->isChildOf($repo)) { |
|
54
|
|
|
return '\'' . $target->getPath() . '\''; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return '__DIR__ . \'/../../' . $target->getRelativePathFrom($repo) . '\''; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns lines of code for the local src installation |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $hook |
|
64
|
|
|
* @return array<string> |
|
65
|
|
|
*/ |
|
66
|
|
|
private function getSrcHookLines(string $hook): array |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
|
|
'#!/usr/bin/env php', |
|
70
|
|
|
'<?php', |
|
71
|
|
|
'', |
|
72
|
|
|
'# installed by CaptainHook ' . CH::VERSION, |
|
73
|
|
|
'', |
|
74
|
|
|
'use CaptainHook\App\Console\Application\Cli as CaptainHook;', |
|
75
|
|
|
'use Symfony\Component\Console\Input\ArgvInput;', |
|
76
|
|
|
'', |
|
77
|
|
|
'(static function($argv)', |
|
78
|
|
|
'{', |
|
79
|
|
|
' $bootstrap = ' . $this->configPath . '/' . $this->bootstrap . ';', |
|
80
|
|
|
' if (!file_exists($bootstrap)) {', |
|
81
|
|
|
' fwrite(STDERR, \'Boostrap file \\\'\' . $bootstrap . \'\\\' could not be found\');', |
|
82
|
|
|
' exit(1);', |
|
83
|
|
|
' }', |
|
84
|
|
|
' require $bootstrap;', |
|
85
|
|
|
'', |
|
86
|
|
|
' $argv = array_merge(', |
|
87
|
|
|
' [', |
|
88
|
|
|
' $argv[0],', |
|
89
|
|
|
' \'hook:' . $hook . '\',', |
|
90
|
|
|
' \'--configuration=\' . ' . $this->configPath . ',', |
|
91
|
|
|
' \'--git-directory=\' . dirname(__DIR__, 2) . \'/.git\',', |
|
92
|
|
|
' ],', |
|
93
|
|
|
' array_slice($argv, 1)', |
|
94
|
|
|
' );', |
|
95
|
|
|
' $captainHook = new CaptainHook($argv[0]);', |
|
96
|
|
|
' $captainHook->run(new ArgvInput($argv));', |
|
97
|
|
|
'}', |
|
98
|
|
|
')($argv);', |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Returns the lines of code for the local phar installation |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $hook |
|
106
|
|
|
* @return array<string> |
|
107
|
|
|
*/ |
|
108
|
|
|
private function getPharHookLines(string $hook): array |
|
109
|
|
|
{ |
|
110
|
|
|
return [ |
|
111
|
|
|
'#!/usr/bin/env php', |
|
112
|
|
|
'<?php', |
|
113
|
|
|
'', |
|
114
|
|
|
'(static function($argv)', |
|
115
|
|
|
'{', |
|
116
|
|
|
' $argv = array_merge(', |
|
117
|
|
|
' [', |
|
118
|
|
|
' $argv[0],', |
|
119
|
|
|
' \'hook:' . $hook . '\',', |
|
120
|
|
|
' \'--configuration=\' . ' . $this->configPath . ',', |
|
121
|
|
|
' \'--git-directory=\' . dirname(__DIR__, 2) . \'/.git\',', |
|
122
|
|
|
' \'--bootstrap=' . $this->bootstrap . '\',', |
|
123
|
|
|
' ],', |
|
124
|
|
|
' array_slice($argv, 1)', |
|
125
|
|
|
' );', |
|
126
|
|
|
' include ' . $this->executablePath . ';', |
|
127
|
|
|
'}', |
|
128
|
|
|
')($argv);', |
|
129
|
|
|
]; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|