forked from sensiolabs/minify-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinifyInstaller.php
More file actions
148 lines (127 loc) · 5.5 KB
/
MinifyInstaller.php
File metadata and controls
148 lines (127 loc) · 5.5 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
/*
* This file is part of the SensioLabs MinifyBundle package.
*
* (c) Simon André - Sensiolabs
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensiolabs\MinifyBundle;
use Sensiolabs\MinifyBundle\Exception\InstallException;
use Sensiolabs\MinifyBundle\Exception\LogicException;
use Sensiolabs\MinifyBundle\Minifier\MinifierInstallerInterface;
use Sensiolabs\MinifyBundle\Minifier\SystemUtils;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* @author Simon André <[email protected]>
*/
final class MinifyInstaller implements MinifierInstallerInterface
{
private const RELEASES_API_URL = 'https://api.github.com/repos/tdewolff/minify/releases';
private readonly HttpClientInterface $httpClient;
private readonly Filesystem $filesystem;
public function __construct(
private readonly string $installDirectory,
private readonly ?string $defaultVersion = null,
?HttpClientInterface $httpClient = null,
) {
if (null === $httpClient && !class_exists(HttpClient::class)) {
throw new LogicException(\sprintf('The "%s" class needs an HTTP client to download the minify binary. Try running "composer require symfony/http-client".', self::class));
}
$this->httpClient = $httpClient ?? HttpClient::create();
$this->filesystem = new Filesystem();
}
public function install(string $version = self::VERSION_LATEST, bool $force = false): void
{
if ($this->isInstalled() && !$force) {
return;
}
if (self::VERSION_LATEST === $version && null !== $this->defaultVersion && '' !== $this->defaultVersion) {
$version = $this->defaultVersion;
}
$this->download($version);
}
public function isInstalled(): bool
{
return file_exists($this->getInstallBinaryPath()) && is_executable($this->getInstallBinaryPath());
}
public function getInstallBinaryPath(): string
{
if ('\\' === \DIRECTORY_SEPARATOR) {
return Path::join($this->installDirectory, 'minify.exe');
}
return Path::join($this->installDirectory, 'minify');
}
public function download(string $version): void
{
$releaseAsset = $this->getReleaseAsset($version);
$releaseDownloadUrl = $releaseAsset['browser_download_url'];
$tempDir = sys_get_temp_dir().'/minify';
$this->filesystem->mkdir($tempDir);
$downloadFilename = Path::join($tempDir, basename($releaseDownloadUrl));
$response = $this->httpClient->request('GET', $releaseDownloadUrl, [
'headers' => [
'Accept' => 'application/octet-stream',
],
]);
if (200 !== $response->getStatusCode()) {
throw new InstallException(sprintf('Error downloading the minify binary from GitHub "%s".', $response->getContent(false)));
}
foreach ($this->httpClient->stream($response) as $chunk) {
$this->filesystem->appendToFile($downloadFilename, $chunk->getContent(), true);
}
$this->filesystem->mkdir(Path::getDirectory($this->getInstallBinaryPath()));
if (str_ends_with($downloadFilename, '.zip')) {
// Windows archive (minify.exe)
$archive = new \ZipArchive();
if (true !== $archive->open($downloadFilename)) {
throw new InstallException(sprintf('Error opening archive "%s".', $downloadFilename));
}
if (false === $archive->extractTo($tempDir, 'minify.exe')) {
throw new InstallException(sprintf('Error extracting minify.exe from archive "%s".', $downloadFilename));
}
$archive->close();
$this->filesystem->copy(Path::join($tempDir, 'minify.exe'), $this->getInstallBinaryPath());
$this->filesystem->chmod($this->getInstallBinaryPath(), 0755);
} else {
$archive = new \PharData($downloadFilename);
try {
$archive->extractTo($tempDir, ['minify'], true);
} catch (\Exception $e) {
throw new InstallException(sprintf('Error extracting the binary from archive "%s".', $downloadFilename), 0, $e);
}
$this->filesystem->copy(Path::join($tempDir, 'minify'), $this->getInstallBinaryPath());
}
$this->filesystem->remove($tempDir);
}
/**
* @return array{
* name: string,
* browser_download_url: string,
* content_type: string,
* }
*/
private function getReleaseAsset(string $version): array
{
$versionUrl = self::VERSION_LATEST === $version ? $version : 'tags/'.$version;
$response = $this->httpClient->request('GET', self::RELEASES_API_URL.'/'.$versionUrl, [
'headers' => ['Accept' => 'application/json'],
'max_redirects' => 2,
]);
if (200 !== $response->getStatusCode()) {
throw new InstallException(sprintf('The release "%s" does not exist.', $version));
}
$systemUtils = SystemUtils::create();
foreach ($response->toArray()['assets'] ?? [] as $asset) {
if ($systemUtils->match($asset['name'])) {
return $asset;
}
}
throw new InstallException(sprintf('Unable to find a binary for release "%s".', $version));
}
}