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\App\Storage; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Util class |
14
|
|
|
* |
15
|
|
|
* @package CaptainHook |
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
17
|
|
|
* @link https://github.com/captainhookphp/captainhook |
18
|
|
|
* @since Class available since Release 1.0.4 |
19
|
|
|
*/ |
20
|
|
|
abstract class Util |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Array representation of a path. |
24
|
|
|
* |
25
|
|
|
* @param string $path |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
10 |
|
public static function pathToArray(string $path) : array |
29
|
|
|
{ |
30
|
10 |
|
return explode(DIRECTORY_SEPARATOR, ltrim($path, DIRECTORY_SEPARATOR)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Convert array to path |
35
|
|
|
* |
36
|
|
|
* @param array $path |
37
|
|
|
* @param bool $absolute |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
6 |
|
public static function arrayToPath(array $path, bool $absolute = false) : string |
41
|
|
|
{ |
42
|
6 |
|
return ( $absolute ? DIRECTORY_SEPARATOR : '' ) . implode(DIRECTORY_SEPARATOR, $path); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Is the given subDir a sub directory of given parentDir. |
47
|
|
|
* |
48
|
|
|
* @param array $subDir |
49
|
|
|
* @param array $parentDir |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
11 |
|
public static function isSubDirectoryOf(array $subDir, array $parentDir) : bool |
53
|
|
|
{ |
54
|
11 |
|
foreach ($parentDir as $index => $dir) { |
55
|
11 |
|
if (!isset($subDir[$index]) || $dir !== $subDir[$index]) { |
56
|
5 |
|
return false; |
57
|
|
|
} |
58
|
|
|
} |
59
|
9 |
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the relative path from parentDir to subDir. |
64
|
|
|
* |
65
|
|
|
* @param array $subDir |
66
|
|
|
* @param array $parentDir |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
9 |
|
public static function getSubPathOf(array $subDir, array $parentDir) : array |
70
|
|
|
{ |
71
|
9 |
|
if (!self::isSubDirectoryOf($subDir, $parentDir)) { |
72
|
1 |
|
throw new \RuntimeException( |
73
|
|
|
'Invalid sub directory: ' |
74
|
1 |
|
. implode('/', $subDir) |
75
|
1 |
|
. ' is not a sub directory of ' |
76
|
1 |
|
. implode('/', $parentDir) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
8 |
|
$path = []; |
81
|
8 |
|
foreach (array_slice($subDir, count($parentDir)) as $dir) { |
82
|
8 |
|
$path[] = $dir; |
83
|
|
|
} |
84
|
8 |
|
return $path; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return the path to the target path from inside the .git/hooks directory f.e. __DIR__ ../../vendor |
89
|
|
|
* |
90
|
|
|
* @param string $repoDir |
91
|
|
|
* @param string $targetPath |
92
|
|
|
* @return string |
93
|
|
|
* @throws \RuntimeException |
94
|
|
|
*/ |
95
|
6 |
|
public static function getTplTargetPath(string $repoDir, string $targetPath) : string |
96
|
|
|
{ |
97
|
6 |
|
$repo = self::pathToArray($repoDir); |
98
|
6 |
|
$target = self::pathToArray($targetPath); |
99
|
|
|
|
100
|
6 |
|
if (!self::isSubDirectoryOf($target, $repo)) { |
101
|
3 |
|
return '\'/' . implode('/', $target); |
102
|
|
|
} |
103
|
|
|
|
104
|
5 |
|
return '__DIR__ . \'/../../' . implode('/', self::getSubPathOf($target, $repo)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Resolves the path to the captainhook-run binary and returns it. |
109
|
|
|
* |
110
|
|
|
* This path is either right inside the repo itself (captainhook) or only in vendor path. |
111
|
|
|
* Which happens if captainhook is required as dependency. |
112
|
|
|
* |
113
|
|
|
* @param string $repoDir |
114
|
|
|
* @param string $vendorPath |
115
|
|
|
* @param string $binary |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
5 |
|
public static function resolveBinaryPath(string $repoDir, string $vendorPath, string $binary): string |
119
|
|
|
{ |
120
|
5 |
|
$binaryPath = $repoDir . DIRECTORY_SEPARATOR . $binary; |
121
|
|
|
|
122
|
5 |
|
if (!file_exists($binaryPath)) { |
123
|
4 |
|
return $vendorPath . '/bin/' . $binary; |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
return $binaryPath; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|