-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheWarmerBinary.php
184 lines (147 loc) · 6.31 KB
/
CacheWarmerBinary.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php declare(strict_types = 1);
/*
* This file is part of the Vairogs package.
*
* (c) Dāvis Zālītis (k0d3r1s) <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Vairogs\Component\CacheWarmer;
use RuntimeException;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Process\Process;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Throwable;
use Vairogs\Component\Functions\Local;
use Vairogs\Component\Functions\Php;
use Vairogs\Component\Functions\Web;
use function array_values;
use function chmod;
use function fclose;
use function file_put_contents;
use function filesize;
use function fopen;
use function fwrite;
use function getcwd;
use function is_dir;
use function is_file;
use function is_int;
use function is_resource;
use function is_writable;
use function sprintf;
use const PHP_EOL;
class CacheWarmerBinary
{
public const string REPOSITORY = 'lettland/cache-warmer';
private ?SymfonyStyle $output = null;
public function __construct(
private readonly ParameterBagInterface $bag,
private ?HttpClientInterface $httpClient = null,
) {
$this->httpClient ??= HttpClient::create();
}
/**
* @throws TransportExceptionInterface
*/
public function createProcess(
array $arguments,
bool $manual = false,
): Process {
if (null === $this->output) {
throw new RuntimeException(sprintf('%s can only be used with %s. Call ->setOutput($style) before %s function', __CLASS__, SymfonyStyle::class, __FUNCTION__));
}
static $_helper = null;
if (null === $_helper) {
$_helper = new class {
use Local\_CurlUA;
use Local\_MkDir;
use Php\_SystemInfo;
use Web\_LatestReleaseTag;
};
}
$info = $_helper->systemInfo();
if (!$manual) {
try {
$response = $this->httpClient->request(Request::METHOD_HEAD, 'https://github.com');
if (Response::HTTP_OK !== $response->getStatusCode()) {
throw new RuntimeException('https://github.com could not be reached');
}
} catch (Throwable $throwable) {
$version = 'nightly' === $this->bag->get('vairogs.cache_warmer.version') ? 'nightly' : 'latest';
$url = sprintf('https://github.com/%s/releases/download/%s/cache-warmer-%s-%s%s', self::REPOSITORY, $version, ...array_values($info));
throw new RuntimeException(sprintf('Unable to connect to github!%sPlease download desired binary from %s, put it in var/%s/manual (name MUST match as is in github) and run this command again with --manual option', PHP_EOL, self::REPOSITORY, $url), previous: $throwable);
}
$configVersion = $this->bag->get('vairogs.cache_warmer.version');
$version = match ($configVersion) {
'latest' => $_helper->latestReleaseTag(self::REPOSITORY),
default => $configVersion,
};
} else {
$version = 'manual';
}
$directory = sprintf('%s/var/%s/%s', $this->bag->get('kernel.project_dir'), self::REPOSITORY, $version);
$isDir = true;
if (!is_dir($directory)) {
$isDir = $_helper->mkdir($directory);
}
if (!$isDir || !is_writable($directory)) {
throw new RuntimeException(sprintf('Unable to create or write directory %s', $directory));
}
if (!is_file($directory . '/.gitignore')) {
file_put_contents($directory . '/.gitignore', '*' . PHP_EOL);
}
$path = sprintf('cache-warmer-%s-%s%s', ...array_values($info));
$binary = $directory . '/' . $path;
if (!$manual && !is_file($binary)) {
$url = sprintf('https://github.com/%s/releases/download/%s/%s', self::REPOSITORY, $version, $path);
$response = $this->httpClient->request(Request::METHOD_HEAD, $url);
$statusCode = $response->getStatusCode();
if (Response::HTTP_OK !== $statusCode) {
throw new RuntimeException(sprintf('Unable to download binary from %s. Please check that version tag is set correctly in configuration', $url));
}
$this->output->note(sprintf('Downloading %s binary from %s', self::REPOSITORY, $url));
$progressBar = null;
$response = $this->httpClient->request(Request::METHOD_GET, $url, [
'on_progress' => function (int $dlNow, int $dlSize) use (&$progressBar): void {
if (0 === $dlSize) {
return;
}
if (!$progressBar) {
$progressBar = $this->output->createProgressBar($dlSize);
}
$progressBar?->setProgress($dlNow);
},
]);
$fileHandler = fopen($binary, 'wb');
if (!is_resource($fileHandler)) {
throw new RuntimeException(sprintf('Cannot open file "%s" for writing.', $binary));
}
foreach ($this->httpClient->stream($response) as $chunk) {
fwrite($fileHandler, $chunk->getContent());
}
fclose($fileHandler);
$progressBar?->finish();
$this->output->writeln('');
chmod($binary, 0o777);
}
if (!is_file($binary) || false === ($filesize = filesize($binary)) || !is_int($filesize) || 1 > $filesize) {
throw new RuntimeException(sprintf('Cannot find or open file "%s"', $binary));
}
return new Process([$binary, ...$arguments], getcwd())
->setTty(Process::isTtySupported())
->setEnv(['TERM' => 'xterm-256color', 'FORCE_COLOR' => '1'])
->setTimeout(null);
}
public function setOutput(
?SymfonyStyle $output,
): self {
$this->output = $output;
return $this;
}
}