|
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\File; |
|
13
|
|
|
|
|
14
|
|
|
use CaptainHook\App\Console\IO; |
|
15
|
|
|
use CaptainHook\App\Git\Diff\FilterUtil; |
|
16
|
|
|
use CaptainHook\App\Hook\Condition; |
|
17
|
|
|
use CaptainHook\App\Hook\Condition\File; |
|
18
|
|
|
use CaptainHook\App\Hook\Constrained; |
|
19
|
|
|
use CaptainHook\App\Hook\Restriction; |
|
20
|
|
|
use CaptainHook\App\Hooks; |
|
21
|
|
|
use SebastianFeldmann\Git\Repository; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class OfType |
|
25
|
|
|
* |
|
26
|
|
|
* All FileStaged conditions are only applicable for `pre-commit` hooks. |
|
27
|
|
|
* The diff filter argument is optional. |
|
28
|
|
|
* |
|
29
|
|
|
* Example configuration: |
|
30
|
|
|
* |
|
31
|
|
|
* <code> |
|
32
|
|
|
* { |
|
33
|
|
|
* "action": "some-action" |
|
34
|
|
|
* "conditions": [ |
|
35
|
|
|
* { |
|
36
|
|
|
* "exec": "CaptainHook.FileStaged.OfType", |
|
37
|
|
|
* "args": [ |
|
38
|
|
|
* "php", |
|
39
|
|
|
* ["A", "C"] |
|
40
|
|
|
* ] |
|
41
|
|
|
* } |
|
42
|
|
|
* ] |
|
43
|
|
|
* } |
|
44
|
|
|
* </code> |
|
45
|
|
|
* |
|
46
|
|
|
* Multiple types can be configured using a comma separated string or an array |
|
47
|
|
|
* - "php,html,xml" |
|
48
|
|
|
* - ["php", "html", "xml"] |
|
49
|
|
|
* |
|
50
|
|
|
* @package CaptainHook |
|
51
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
52
|
|
|
* @link https://github.com/captainhook-git/captainhook |
|
53
|
|
|
* @since Class available since Release 5.0.0 |
|
54
|
|
|
*/ |
|
55
|
|
|
abstract class OfType extends File |
|
56
|
|
|
{ |
|
57
|
|
|
/** |
|
58
|
|
|
* File type to check e.g. 'php' or 'html' |
|
59
|
|
|
* |
|
60
|
|
|
* @var array<int, string> |
|
61
|
|
|
*/ |
|
62
|
|
|
protected array $suffixes; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* --diff-filter option |
|
66
|
|
|
* |
|
67
|
|
|
* @var array<int, string> |
|
68
|
|
|
*/ |
|
69
|
|
|
protected array $diffFilter; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* OfType constructor |
|
73
|
|
|
* |
|
74
|
|
|
* @param array<int, string>|string $types |
|
75
|
|
|
* @param array<int, string>|string $filter |
|
76
|
|
|
*/ |
|
77
|
7 |
|
public function __construct(array|string $types, array|string $filter = []) |
|
78
|
|
|
{ |
|
79
|
7 |
|
$this->suffixes = is_array($types) ? $types : explode(',', $types); |
|
|
|
|
|
|
80
|
7 |
|
$this->diffFilter = FilterUtil::filterFromConfigValue($filter); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Evaluates the condition |
|
85
|
|
|
* |
|
86
|
|
|
* @param \CaptainHook\App\Console\IO $io |
|
87
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
7 |
|
public function isTrue(IO $io, Repository $repository): bool |
|
91
|
|
|
{ |
|
92
|
7 |
|
$files = $this->getFiles($io, $repository, $this->diffFilter); |
|
93
|
7 |
|
$filtered = $this->filterFilesByType($files, $this->suffixes); |
|
94
|
7 |
|
return count($filtered) > 0; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|