1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Moodle component manager. |
5
|
|
|
* |
6
|
|
|
* @author Luke Carrier <[email protected]> |
7
|
|
|
* @copyright 2016 Luke Carrier |
8
|
|
|
* @license GPL-3.0+ |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ComponentManager\Step; |
12
|
|
|
|
13
|
|
|
use ComponentManager\Exception\UnsatisfiedVersionException; |
14
|
|
|
use ComponentManager\MoodleApi; |
15
|
|
|
use ComponentManager\MoodleVersion; |
16
|
|
|
use ComponentManager\Task\PackageTask; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Resolve a version specification to Moodle release. |
21
|
|
|
*/ |
22
|
|
|
class ResolveMoodleVersionStep implements Step { |
23
|
|
|
/** |
24
|
|
|
* Moodle.org API client. |
25
|
|
|
* |
26
|
|
|
* @var MoodleApi |
27
|
|
|
*/ |
28
|
|
|
protected $api; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Moodle version specification. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $specification; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initialiser. |
39
|
|
|
* |
40
|
|
|
* @param MoodleApi $api |
41
|
|
|
* @param string $specification |
42
|
|
|
*/ |
43
|
|
|
public function __construct(MoodleApi $api, $specification) { |
44
|
|
|
$this->api = $api; |
45
|
|
|
$this->specification = $specification; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc Step |
50
|
|
|
* |
51
|
|
|
* @param PackageTask $task |
52
|
|
|
*/ |
53
|
|
|
public function execute($task, LoggerInterface $logger) { |
54
|
|
|
$versions = $this->api->getMoodleVersions(); |
55
|
|
|
|
56
|
|
|
$logger->info('Resolving Moodle version from specification', [ |
57
|
|
|
'specification' => $this->specification, |
58
|
|
|
'availableVersions' => count($versions), |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$scores = []; |
62
|
|
|
foreach ($versions as $index => $version) { |
63
|
|
|
if ($score = $version->satisfies($this->specification)) { |
64
|
|
|
$scores[$score] = $version; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!count($scores)) { |
69
|
|
|
throw new UnsatisfiedVersionException( |
70
|
|
|
"Unable to satisfy Moodle version \"{$this->specification}\"", |
|
|
|
|
71
|
|
|
UnsatisfiedVersionException::CODE_UNKNOWN_VERSION); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
ksort($scores); |
75
|
|
|
/** @var MoodleVersion $version */ |
76
|
|
|
$version = end($scores); |
77
|
|
|
|
78
|
|
|
$logger->info('Selected Moodle release', [ |
79
|
|
|
'build' => $version->getBuild(), |
80
|
|
|
'release' => $version->getRelease(), |
81
|
|
|
'score' => key($scores), |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$task->setMoodleVersion($version); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.