|
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
|
|
|
namespace CaptainHook\App\Hook\Condition; |
|
13
|
|
|
|
|
14
|
|
|
use CaptainHook\App\Console\IO; |
|
15
|
|
|
use CaptainHook\App\Hook\Condition; |
|
16
|
|
|
use CaptainHook\App\Hook\Constrained; |
|
17
|
|
|
use CaptainHook\App\Hook\Restriction; |
|
18
|
|
|
use CaptainHook\App\Hooks; |
|
19
|
|
|
use SebastianFeldmann\Git\Repository; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class FileChange |
|
23
|
|
|
* |
|
24
|
|
|
* @package CaptainHook |
|
25
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
26
|
|
|
* @link https://github.com/captainhook-git/captainhook |
|
27
|
|
|
* @since Class available since Release 5.2.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class File implements Condition, Constrained |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Return the hook restriction information |
|
33
|
|
|
* |
|
34
|
|
|
* @return \CaptainHook\App\Hook\Restriction |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract public static function getRestriction(): Restriction; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Evaluates a condition |
|
40
|
|
|
* |
|
41
|
|
|
* @param \CaptainHook\App\Console\IO $io |
|
42
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
|
|
abstract public function isTrue(IO $io, Repository $repository): bool; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Check if all of the given files can be found in a haystack of files |
|
49
|
|
|
* |
|
50
|
|
|
* IMPORTANT: If no files are provided this is always true. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array<string> $files |
|
53
|
|
|
* @param array<string> $haystack |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
8 |
|
protected function allFilesInHaystack(array $files, array $haystack): bool |
|
57
|
|
|
{ |
|
58
|
8 |
|
foreach ($files as $filePattern) { |
|
59
|
8 |
|
if (!$this->isFileInList($haystack, $filePattern)) { |
|
60
|
4 |
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
4 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Check if any of the given files can be found in a haystack of files |
|
68
|
|
|
* |
|
69
|
|
|
* IMPORTANT: If no files are provided this is always false. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array<string> $files |
|
72
|
|
|
* @param array<string> $haystack |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
13 |
|
protected function anyFileInHaystack(array $files, array $haystack): bool |
|
76
|
|
|
{ |
|
77
|
13 |
|
foreach ($files as $filePattern) { |
|
78
|
13 |
|
if ($this->isFileInList($haystack, $filePattern)) { |
|
79
|
9 |
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
5 |
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Check if a file matching a `fnmatch` pattern was changed |
|
87
|
|
|
* |
|
88
|
|
|
* @param array<string> $listOfFiles List of files to scan |
|
89
|
|
|
* @param string $pattern Pattern in fnmatch format to look for |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
21 |
|
protected function isFileInList(array $listOfFiles, string $pattern): bool |
|
93
|
|
|
{ |
|
94
|
21 |
|
foreach ($listOfFiles as $file) { |
|
95
|
21 |
|
if (fnmatch($pattern, $file)) { |
|
96
|
17 |
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
9 |
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|