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
|
12 |
|
public static function pathToArray(string $path) : array |
29
|
|
|
{ |
30
|
12 |
|
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
|
7 |
|
public static function arrayToPath(array $path, bool $absolute = false) : string |
41
|
|
|
{ |
42
|
7 |
|
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
|
13 |
|
public static function isSubDirectoryOf(array $subDir, array $parentDir) : bool |
53
|
|
|
{ |
54
|
13 |
|
foreach ($parentDir as $index => $dir) { |
55
|
13 |
|
if (!isset($subDir[$index]) || $dir !== $subDir[$index]) { |
56
|
13 |
|
return false; |
57
|
|
|
} |
58
|
|
|
} |
59
|
10 |
|
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
|
10 |
|
public static function getSubPathOf(array $subDir, array $parentDir) : array |
70
|
|
|
{ |
71
|
10 |
|
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
|
9 |
|
$path = []; |
81
|
9 |
|
foreach (array_slice($subDir, count($parentDir)) as $dir) { |
82
|
9 |
|
$path[] = $dir; |
83
|
|
|
} |
84
|
9 |
|
return $path; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Transforms an absolute path to a relative one |
89
|
|
|
* |
90
|
|
|
* @param string $subPath Absolute path to sub directory |
91
|
|
|
* @param string $parentPath Absolute path to parent directory |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
1 |
|
public static function getRelativePath(string $subPath, string $parentPath): string |
95
|
|
|
{ |
96
|
1 |
|
return Util::arrayToPath( |
97
|
1 |
|
Util::getSubPathOf( |
98
|
1 |
|
Util::pathToArray($subPath), |
99
|
1 |
|
Util::pathToArray($parentPath) |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return the path to the target path from inside the .git/hooks directory f.e. __DIR__ ../../vendor |
106
|
|
|
* |
107
|
|
|
* @param string $repoDir |
108
|
|
|
* @param string $targetPath |
109
|
|
|
* @return string |
110
|
|
|
* @throws \RuntimeException |
111
|
|
|
*/ |
112
|
6 |
|
public static function getTplTargetPath(string $repoDir, string $targetPath) : string |
113
|
|
|
{ |
114
|
6 |
|
$repo = self::pathToArray($repoDir); |
115
|
6 |
|
$target = self::pathToArray($targetPath); |
116
|
|
|
|
117
|
6 |
|
if (!self::isSubDirectoryOf($target, $repo)) { |
118
|
3 |
|
return '\'/' . implode('/', $target); |
119
|
|
|
} |
120
|
|
|
|
121
|
5 |
|
return '__DIR__ . \'/../../' . implode('/', self::getSubPathOf($target, $repo)); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|