1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BestIt\LicenseCheck; |
6
|
|
|
|
7
|
|
|
use BestIt\LicenseCheck\Configuration\Configuration; |
8
|
|
|
use BestIt\LicenseCheck\LicenseLoader\LicenseLoaderInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Running different loaders to check the licenses of different package managers. |
12
|
|
|
* |
13
|
|
|
* @author best it AG <[email protected]> |
14
|
|
|
* @package BestIt\LicenseCheck |
15
|
|
|
*/ |
16
|
|
|
class Checker |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array<LicenseLoaderInterface> $loaders Array of loaders for different package managers. |
20
|
|
|
*/ |
21
|
|
|
private iterable $loaders; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create checker instance and register the loaders. |
25
|
|
|
* |
26
|
|
|
* @param LicenseLoaderInterface[] $loaders |
27
|
|
|
*/ |
28
|
|
|
public function __construct(iterable $loaders) |
29
|
|
|
{ |
30
|
|
|
$this->loaders = $loaders; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Start the validation. |
35
|
|
|
* |
36
|
|
|
* @param Configuration $configuration |
37
|
|
|
* @param string $path |
38
|
|
|
* |
39
|
|
|
* @return Result |
40
|
|
|
*/ |
41
|
|
|
public function validate(Configuration $configuration, string $path): Result |
42
|
|
|
{ |
43
|
|
|
$result = new Result(); |
44
|
|
|
|
45
|
|
|
$allowedLicenses = $configuration->getAllowedLicenses(); |
46
|
|
|
foreach ($this->loaders as $loader) { |
47
|
|
|
$type = $loader->getName(); |
48
|
|
|
$allowedPackages = $configuration->getAllowedPackages($type); |
49
|
|
|
foreach ($loader->getLicenses($path) as $package => $licenses) { |
50
|
|
|
if (!$this->isPackageAllowed($package, $allowedPackages)) { |
51
|
|
|
if (count($licenses) === 0) { |
52
|
|
|
$result->addViolation( |
53
|
|
|
sprintf( |
54
|
|
|
'No license found for %s package %s.', |
55
|
|
|
$type, |
56
|
|
|
$package, |
57
|
|
|
), |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$hasValidLicense = false; |
64
|
|
|
foreach ($licenses as $license) { |
65
|
|
|
if (in_array($license, $allowedLicenses)) { |
66
|
|
|
$hasValidLicense = true; |
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!$hasValidLicense) { |
72
|
|
|
$result->addViolation( |
73
|
|
|
sprintf( |
74
|
|
|
'License (%s) of %s package %s is not allowed.', |
75
|
|
|
implode(', ', $licenses), |
76
|
|
|
$type, |
77
|
|
|
$package, |
78
|
|
|
), |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check if the given package is allowed. |
90
|
|
|
* |
91
|
|
|
* @param string $package |
92
|
|
|
* @param string[] $allowedPackages |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
private function isPackageAllowed(string $package, array $allowedPackages): bool |
97
|
|
|
{ |
98
|
|
|
$allowed = false; |
99
|
|
|
|
100
|
|
|
foreach ($allowedPackages as $allowedPackage) { |
101
|
|
|
if (preg_match($allowedPackage, $package) === 1) { |
102
|
|
|
$allowed = true; |
103
|
|
|
break; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $allowed; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|