1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright MediaCT. All rights reserved. |
4
|
|
|
* https://www.mediact.nl |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Mediact\DependencyGuard\Violation\Filter; |
8
|
|
|
|
9
|
|
|
use Composer\Composer; |
10
|
|
|
|
11
|
|
|
class ViolationFilterFactory implements ViolationFilterFactoryInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Create a violation filter for the given Composer instance. |
15
|
|
|
* |
16
|
|
|
* @param Composer $composer |
17
|
|
|
* |
18
|
|
|
* @return ViolationFilterInterface |
19
|
|
|
*/ |
20
|
10 |
|
public function create(Composer $composer): ViolationFilterInterface |
21
|
|
|
{ |
22
|
10 |
|
$chain = new ViolationFilterChain( |
23
|
10 |
|
...array_merge( |
24
|
10 |
|
$this->getSuggestsFilters($composer), |
25
|
10 |
|
$this->getIgnoreFilters($composer) |
26
|
|
|
) |
27
|
|
|
); |
28
|
|
|
|
29
|
10 |
|
return new ViolationFilterChain( |
30
|
10 |
|
$chain, |
31
|
10 |
|
new PackageRequirementsFilter( |
32
|
10 |
|
$composer->getLocker(), |
33
|
10 |
|
$chain |
34
|
|
|
) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Exclude packages suggested by the root package from violating dependencies. |
40
|
|
|
* |
41
|
|
|
* @param Composer $composer |
42
|
|
|
* |
43
|
|
|
* @return ViolationFilterInterface[] |
44
|
|
|
*/ |
45
|
10 |
|
private function getSuggestsFilters(Composer $composer): array |
46
|
|
|
{ |
47
|
10 |
|
return array_map( |
48
|
|
|
function (string $package) : ViolationFilterInterface { |
49
|
4 |
|
return new ExactPackageFilter($package); |
50
|
10 |
|
}, |
51
|
10 |
|
array_keys( |
52
|
10 |
|
$composer->getPackage()->getSuggests() |
53
|
|
|
) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the violation filters for the ignore rules in the given Composer instance. |
59
|
|
|
* |
60
|
|
|
* @param Composer $composer |
61
|
|
|
* |
62
|
|
|
* @return ViolationFilterInterface[] |
63
|
|
|
*/ |
64
|
10 |
|
private function getIgnoreFilters(Composer $composer): array |
65
|
|
|
{ |
66
|
10 |
|
$extra = $composer->getPackage()->getExtra(); |
67
|
|
|
|
68
|
10 |
|
return array_map( |
69
|
|
|
function (string $rule) : ViolationFilterInterface { |
70
|
|
|
$filters = [ |
71
|
5 |
|
new ExactPackageFilter($rule), |
72
|
5 |
|
new PatternPackageFilter($rule) |
73
|
|
|
]; |
74
|
|
|
|
75
|
5 |
|
if (preg_match('#/$#', $rule)) { |
76
|
1 |
|
$filters[] = new VendorFilter($rule); |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
return new ViolationFilterChain(...$filters); |
80
|
10 |
|
}, |
81
|
10 |
|
$extra['dependency-guard']['ignore'] ?? [] |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|