forked from phpro/grumphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymfonyConsole.php
86 lines (74 loc) · 3.16 KB
/
SymfonyConsole.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?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;
use Symfony\Component\Process\PhpExecutableFinder;
/** @extends AbstractExternalTask<ProcessFormatterInterface> */
class SymfonyConsole extends AbstractExternalTask
{
public static function getConfigurableOptions(): ConfigOptionsResolver
{
return ConfigOptionsResolver::fromOptionsResolver(
(new OptionsResolver())
->setDefaults([
'bin' => './bin/console',
'command' => [],
'ignore_patterns' => [],
'whitelist_patterns' => [],
'triggered_by' => ['php', 'yml', 'xml'],
'run_always' => false,
])
->addAllowedTypes('bin', ['string'])
->addAllowedTypes('command', ['string[]'])
->addAllowedTypes('ignore_patterns', ['array'])
->addAllowedTypes('whitelist_patterns', ['array'])
->addAllowedTypes('triggered_by', ['array'])
->addAllowedTypes('run_always', ['bool'])
->setAllowedValues(
'bin',
static fn(string $bin): bool => '' !== $bin
)
->setAllowedValues(
'command',
static fn(array $command): bool => '' !== \implode('', $command)
)
->setAllowedValues('bin', static fn(string $bin): bool => '' !== $bin)
->setRequired('command')
);
}
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()
->extensions($config['triggered_by'])
->paths($config['whitelist_patterns'] ?? [])
->notPaths($config['ignore_patterns'] ?? []);
if (!$config['run_always'] && 0 === \count($files)) {
return TaskResult::createSkipped($this, $context);
}
$php = (new PhpExecutableFinder())->find();
if (false === $php) {
return TaskResult::createFailed($this, $context, 'Unable to locate the PHP executable.');
}
$arguments = $this->processBuilder->createArgumentsForCommand($php);
$arguments->add($config['bin']);
$arguments->addArgumentArray('%s', $config['command']);
$process = $this->processBuilder->buildProcess($arguments);
$process->run();
if (!$process->isSuccessful()) {
return TaskResult::createFailed($this, $context, $this->formatter->format($process));
}
return TaskResult::createPassed($this, $context);
}
}