forked from basis-company/tarantool-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.php
64 lines (51 loc) · 1.82 KB
/
Configuration.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
<?php
namespace Job\Admin;
class Configuration
{
public string $repository = 'basis-company/tarantool-admin';
// once per hour
public int $ttl = 3600;
public function run(): array
{
$version = (@include dirname(__DIR__, 3) . '/var/version.php') ?: [];
$latest = '';
if (array_key_exists('tag', $version)) {
$latest = $version['tag'];
}
if (getenv('TARANTOOL_CHECK_VERSION') !== 'false') {
$latest = $this->getLatest();
}
return [
'connectionsReadOnly' => (bool) getenv('TARANTOOL_CONNECTIONS_READONLY'),
'connections' => explode(',', getenv('TARANTOOL_CONNECTIONS')),
'query' => (bool) getenv('TARANTOOL_DATABASE_QUERY'),
'readOnly' => getenv('TARANTOOL_READONLY') == 'true' || getenv('TARANTOOL_READONLY') == '1',
'version' => $version,
'latest' => $latest,
];
}
protected function getLatest()
{
$filename = dirname(__DIR__, 3) . '/var/latest.php';
if (file_exists($filename)) {
$latest = include $filename;
if ($latest['tag'] && $latest['timestamp'] + $this->ttl >= time()) {
return $latest['tag'];
}
}
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => [
'User-Agent: PHP',
]
]
]);
$url = "https://api.github.com/repos/$this->repository/releases/latest";
$tag = @json_decode(file_get_contents($url, false, $context))->tag_name;
$timestamp = time();
$contents = '<' . '?php return ' . var_export(compact('tag', 'timestamp'), true) . ';';
file_put_contents($filename, $contents);
return $tag;
}
}