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\InstallationFailureException; |
14
|
|
|
use ComponentManager\HttpClient; |
15
|
|
|
use ComponentManager\Task\PackageTask; |
16
|
|
|
use GuzzleHttp\Client; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use ZipArchive; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Obtain Moodle source code step. |
23
|
|
|
* |
24
|
|
|
* Downloads the specified Moodle version to the specified on-disk location. |
25
|
|
|
*/ |
26
|
|
|
class ObtainMoodleSourceStep implements Step { |
27
|
|
|
/** |
28
|
|
|
* Moodle source archive path. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $archive; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Target directory. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $destination; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* HTTP client. |
43
|
|
|
* |
44
|
|
|
* @var HttpClient |
45
|
|
|
*/ |
46
|
|
|
protected $httpClient; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Initialiser. |
50
|
|
|
* |
51
|
|
|
* @param HttpClient $httpClient |
52
|
|
|
* @param string $archive |
53
|
|
|
* @param string $destination |
54
|
|
|
*/ |
55
|
|
|
public function __construct(HttpClient $httpClient, $archive, |
56
|
|
|
$destination) { |
57
|
|
|
$this->httpClient = $httpClient; |
58
|
|
|
|
59
|
|
|
$this->archive = $archive; |
60
|
|
|
$this->destination = $destination; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc Step |
65
|
|
|
* |
66
|
|
|
* @param PackageTask $task |
67
|
|
|
*/ |
68
|
|
|
public function execute($task, LoggerInterface $logger) { |
69
|
|
|
$uri = $task->getMoodleVersion()->getDownloadUri(); |
70
|
|
|
|
71
|
|
|
$logger->info('Downloading Moodle', [ |
72
|
|
|
'uri' => $uri, |
73
|
|
|
'archive' => $this->archive, |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
$message = $this->httpClient->createRequest(Request::METHOD_GET, $uri); |
77
|
|
|
$response = $this->httpClient->sendRequest($message); |
78
|
|
|
file_put_contents($this->archive, $response->getBody()); |
79
|
|
|
|
80
|
|
|
$logger->info('Extracting Moodle archive', [ |
81
|
|
|
'archive' => $this->archive, |
82
|
|
|
'destination' => $this->destination, |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$archive = new ZipArchive(); |
86
|
|
|
if (!$archive->open($this->archive)) { |
87
|
|
|
throw new InstallationFailureException( |
88
|
|
|
"Unable to open archive \"{$this->archive}\"", |
|
|
|
|
89
|
|
|
InstallationFailureException::CODE_EXTRACTION_FAILED); |
90
|
|
|
} |
91
|
|
|
$archive->extractTo($this->destination); |
92
|
|
|
$archive->close(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.