-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathSecurityCheckerEnlightn.php
70 lines (58 loc) · 2.36 KB
/
SecurityCheckerEnlightn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace GrumPHP\Task;
use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Config\ConfigOptionsResolver;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class SecurityCheckerEnlightn extends AbstractExternalTask
{
public static function getConfigurableOptions(): ConfigOptionsResolver
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'lockfile' => './composer.lock',
'run_always' => false,
'allow_list' => []
]);
$resolver->addAllowedTypes('lockfile', ['string']);
$resolver->addAllowedTypes('run_always', ['bool']);
$resolver->addAllowedTypes('allow_list', ['array']);
return ConfigOptionsResolver::fromOptionsResolver($resolver);
}
public function canRunInContext(ContextInterface $context): bool
{
return $context instanceof GitPreCommitContext || $context instanceof RunContext;
}
public function run(ContextInterface $context): TaskResultInterface
{
$config = $this->getConfig()->getOptions();
$files = $context->getFiles()
->path(pathinfo($config['lockfile'], PATHINFO_DIRNAME))
->name(pathinfo($config['lockfile'], PATHINFO_BASENAME));
if (0 === \count($files) && !$config['run_always']) {
return TaskResult::createSkipped($this, $context);
}
$arguments = $this->processBuilder->createArgumentsForCommand('security-checker');
$arguments->add('security:check');
$arguments->addOptionalArgument('%s', $config['lockfile']);
if (!empty($config['allow_list'])) {
foreach ($config['allow_list'] as $allowListItem) {
$arguments->addOptionalArgument('--allow-list %s', $allowListItem);
}
}
$process = $this->processBuilder->buildProcess($arguments);
$process->run();
if (!$process->isSuccessful()) {
return TaskResult::createFailed($this, $context, $this->formatter->format($process));
}
return TaskResult::createPassed($this, $context);
}
}