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 SebastianFeldmann\CaptainHook\Hook; |
11
|
|
|
|
12
|
|
|
use SebastianFeldmann\CaptainHook\Storage\Util as StorageUtil; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Template class |
16
|
|
|
* |
17
|
|
|
* @package CaptainHook |
18
|
|
|
* @author Sebastian Feldmann <[email protected]> |
19
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
20
|
|
|
* @since Class available since Release 0.9.0 |
21
|
|
|
*/ |
22
|
|
|
abstract class Template |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Return the php code for the git hook scripts. |
26
|
|
|
* |
27
|
|
|
* @param string $hook Name of the hook to trigger. |
28
|
|
|
* @param string $repoPath Absolute path to the repository root. |
29
|
|
|
* @param string $vendorPath Absolute path to the vendor folder. |
30
|
|
|
* @param string $configPath Absolute path to the configuration file. |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
3 |
|
public static function getCode(string $hook, string $repoPath, string $vendorPath, string $configPath) : string |
34
|
|
|
{ |
35
|
3 |
|
$tplVendorPath = self::getTplTargetPath($repoPath, $vendorPath); |
36
|
3 |
|
$tplConfigPath = self::getTplTargetPath($repoPath, $configPath); |
37
|
|
|
|
38
|
3 |
|
return '#!/usr/bin/env php' . PHP_EOL . |
39
|
3 |
|
'<?php' . PHP_EOL . |
40
|
3 |
|
'$autoLoader = ' . $tplVendorPath . '/autoload.php\';' . PHP_EOL . PHP_EOL . |
41
|
3 |
|
'if (!file_exists($autoLoader)) {' . PHP_EOL . |
42
|
3 |
|
' fwrite(STDERR,' . PHP_EOL . |
43
|
3 |
|
' \'Composer autoload.php could not be found\' . PHP_EOL .' . PHP_EOL . |
44
|
3 |
|
' \'Please re-install the hook with:\' . PHP_EOL .' . PHP_EOL . |
45
|
3 |
|
' \'$ captainhook install --composer-vendor-path=...\' . PHP_EOL' . PHP_EOL . |
46
|
3 |
|
' );' . PHP_EOL . |
47
|
3 |
|
' exit(1);' . PHP_EOL . |
48
|
3 |
|
'}' . PHP_EOL . |
49
|
3 |
|
'require $autoLoader;' . PHP_EOL . |
50
|
3 |
|
'$config = realpath(' . $tplConfigPath . '\');' . PHP_EOL . |
51
|
3 |
|
'$app = new SebastianFeldmann\CaptainHook\Console\Application\Hook();' . PHP_EOL . |
52
|
3 |
|
'$app->setHook(\'' . $hook . '\');' . PHP_EOL . |
53
|
3 |
|
'$app->setConfigFile($config);' . PHP_EOL . |
54
|
3 |
|
'$app->run();' . PHP_EOL . PHP_EOL; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return the path to the target path from inside the .git/hooks directory f.e. __DIR__ ../../vendor. |
59
|
|
|
* |
60
|
|
|
* @param string $repoDir |
61
|
|
|
* @param string $targetPath |
62
|
|
|
* @return string |
63
|
|
|
* @throws \RuntimeException |
64
|
|
|
*/ |
65
|
4 |
|
public static function getTplTargetPath(string $repoDir, string $targetPath) : string |
66
|
|
|
{ |
67
|
4 |
|
$repo = explode(DIRECTORY_SEPARATOR, ltrim($repoDir, DIRECTORY_SEPARATOR)); |
68
|
4 |
|
$target = explode(DIRECTORY_SEPARATOR, ltrim($targetPath, DIRECTORY_SEPARATOR)); |
69
|
|
|
|
70
|
4 |
|
if (!StorageUtil::isSubDirectoryOf($target, $repo)) { |
71
|
2 |
|
return '\'' . $targetPath; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return '__DIR__ . \'/../../' . StorageUtil::getSubPathOf($target, $repo); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|