Repository navigation
Expand file tree
/
Copy pathPhpVersion.php
More file actions
93 lines (77 loc) · 2.63 KB
/
Copy pathPhpVersion.php
File metadata and controls
93 lines (77 loc) · 2.63 KB
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
87
88
89
90
91
92
93
<?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\Config\EmptyTaskConfig;
use GrumPHP\Task\Config\TaskConfigInterface;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use GrumPHP\Util\PhpVersion as PhpVersionUtility;
use Symfony\Component\OptionsResolver\OptionsResolver;
class PhpVersion implements TaskInterface
{
/**
* @var TaskConfigInterface
*/
private $config;
/**
* @var PhpVersionUtility
*/
private $phpVersionUtility;
public function __construct(PhpVersionUtility $phpVersionUtility)
{
$this->config = new EmptyTaskConfig();
$this->phpVersionUtility = $phpVersionUtility;
}
public function canRunInContext(ContextInterface $context): bool
{
return $context instanceof RunContext || $context instanceof GitPreCommitContext;
}
public function run(ContextInterface $context): TaskResultInterface
{
$config = $this->getConfig()->getOptions();
if (null === $config['project']) {
return TaskResult::createSkipped($this, $context);
}
// Check the current version
if (!$this->phpVersionUtility->isSupportedVersion(PHP_VERSION)) {
return TaskResult::createFailed(
$this,
$context,
sprintf('PHP version %s is unsupported', PHP_VERSION)
);
}
// Check the project version if defined
if (!$this->phpVersionUtility->isSupportedProjectVersion(PHP_VERSION, $config['project'])) {
return TaskResult::createFailed(
$this,
$context,
sprintf('This project requires PHP version %s, you have %s', $config['project'], PHP_VERSION)
);
}
return TaskResult::createPassed($this, $context);
}
public function withConfig(TaskConfigInterface $config): TaskInterface
{
$new = clone $this;
$new->config = $config;
return $new;
}
public function getConfig(): TaskConfigInterface
{
return $this->config;
}
public static function getConfigurableOptions(): ConfigOptionsResolver
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'project' => null,
]);
$resolver->addAllowedTypes('project', ['null', 'string']);
return ConfigOptionsResolver::fromOptionsResolver($resolver);
}
}