1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright MediaCT. All rights reserved. |
4
|
|
|
* https://www.mediact.nl |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Mediact\DependencyGuard\GrumPHP; |
8
|
|
|
|
9
|
|
|
use Composer\Composer; |
10
|
|
|
use GrumPHP\Runner\TaskResult; |
11
|
|
|
use GrumPHP\Runner\TaskResultInterface; |
12
|
|
|
use GrumPHP\Task\Context\ContextInterface; |
13
|
|
|
use GrumPHP\Task\Context\GitPreCommitContext; |
14
|
|
|
use GrumPHP\Task\Context\RunContext; |
15
|
|
|
use GrumPHP\Task\TaskInterface; |
16
|
|
|
use Mediact\DependencyGuard\DependencyGuardFactoryInterface; |
17
|
|
|
use Mediact\DependencyGuard\Exporter\ViolationExporterInterface; |
18
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
19
|
|
|
|
20
|
|
|
class DependencyGuard implements TaskInterface |
21
|
|
|
{ |
22
|
|
|
/** @var Composer */ |
23
|
|
|
private $composer; |
24
|
|
|
|
25
|
|
|
/** @var DependencyGuardFactoryInterface */ |
26
|
|
|
private $guardFactory; |
27
|
|
|
|
28
|
|
|
/** @var ViolationExporterInterface */ |
29
|
|
|
private $exporter; |
30
|
|
|
|
31
|
|
|
/** @var null|string */ |
32
|
|
|
private $workingDirectory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param Composer $composer |
38
|
|
|
* @param DependencyGuardFactoryInterface $guardFactory |
39
|
|
|
* @param ViolationExporterInterface $exporter |
40
|
|
|
* @param string|null $workingDirectory |
41
|
|
|
*/ |
42
|
1 |
|
public function __construct( |
43
|
|
|
Composer $composer, |
44
|
|
|
DependencyGuardFactoryInterface $guardFactory, |
45
|
|
|
ViolationExporterInterface $exporter, |
46
|
|
|
string $workingDirectory = null |
47
|
|
|
) { |
48
|
1 |
|
$this->composer = $composer; |
49
|
1 |
|
$this->guardFactory = $guardFactory; |
50
|
1 |
|
$this->exporter = $exporter; |
51
|
1 |
|
$this->workingDirectory = $workingDirectory ?? getcwd(); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
1 |
|
public function getName(): string |
59
|
|
|
{ |
60
|
1 |
|
return 'dependency-guard'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the configurable options for the dependency guard. |
65
|
|
|
* |
66
|
|
|
* @return OptionsResolver |
67
|
|
|
*/ |
68
|
1 |
|
public function getConfigurableOptions(): OptionsResolver |
69
|
|
|
{ |
70
|
1 |
|
return new OptionsResolver(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* This methods specifies if a task can run in a specific context. |
75
|
|
|
* |
76
|
|
|
* @param ContextInterface $context |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
3 |
|
public function canRunInContext(ContextInterface $context): bool |
81
|
|
|
{ |
82
|
|
|
return ( |
83
|
3 |
|
$context instanceof GitPreCommitContext |
84
|
3 |
|
|| $context instanceof RunContext |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param ContextInterface $context |
90
|
|
|
* |
91
|
|
|
* @return TaskResultInterface |
92
|
|
|
*/ |
93
|
6 |
|
public function run(ContextInterface $context): TaskResultInterface |
94
|
|
|
{ |
95
|
6 |
|
foreach (['composer.lock', 'composer.json'] as $file) { |
96
|
6 |
|
if (!is_readable( |
97
|
6 |
|
$this->workingDirectory . DIRECTORY_SEPARATOR . $file |
98
|
|
|
)) { |
99
|
3 |
|
return TaskResult::createSkipped($this, $context); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
$guard = $this->guardFactory->create(); |
104
|
3 |
|
$violations = $guard->determineViolations($this->composer); |
105
|
|
|
|
106
|
3 |
|
if (count($violations)) { |
107
|
2 |
|
$this->exporter->export($violations); |
108
|
|
|
|
109
|
2 |
|
return TaskResult::createFailed( |
110
|
2 |
|
$this, |
111
|
2 |
|
$context, |
112
|
2 |
|
'Encountered dependency violations.' |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
return TaskResult::createPassed($this, $context); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the configuration. |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
1 |
|
public function getConfiguration(): array |
125
|
|
|
{ |
126
|
1 |
|
return []; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|