1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* The Imaging Source Download System PHP Wrapper |
6
|
|
|
* |
7
|
|
|
* PHP wrapper for The Imaging Source Download System Web API. Authored and supported by The Imaging Source Europe GmbH. |
8
|
|
|
* |
9
|
|
|
* @link http://dl-gui.theimagingsource.com to learn more about The Imaging Source Download System |
10
|
|
|
* @link https://github.com/jonathanmaron/theimagingsource-tisd-sdk for the canonical source repository |
11
|
|
|
* @license https://github.com/jonathanmaron/theimagingsource-tisd-sdk/blob/master/LICENSE.md |
12
|
|
|
* @copyright © 2022 The Imaging Source Europe GmbH |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Tisd\Sdk; |
16
|
|
|
|
17
|
|
|
use Tisd\Sdk\Cache\Cache; |
18
|
|
|
use Tisd\Sdk\Defaults\Defaults; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait ConsolidatedTrait |
22
|
|
|
* |
23
|
|
|
* @package Tisd\Sdk |
24
|
|
|
*/ |
25
|
|
|
trait ConsolidatedTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Array of consolidated data |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private array $consolidated; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get the Cache instance |
36
|
|
|
* |
37
|
|
|
* @return Cache |
38
|
|
|
*/ |
39
|
|
|
abstract public function getCache(): Cache; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the context |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
abstract public function getContext(): string; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the hostname |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
abstract public function getHostname(): string; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the locale |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
abstract public function getLocale(): string; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the timeout |
64
|
|
|
* |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
|
|
abstract public function getTimeout(): int; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the version |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
abstract public function getVersion(): string; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Filter the packages by key |
78
|
|
|
* |
79
|
|
|
* @param array $packages |
80
|
|
|
* @param string $key |
81
|
|
|
* @param array|string $value |
82
|
|
|
* @param bool $fuzzy |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
abstract protected function filter(array $packages, string $key, array|string $value, bool $fuzzy = false): array; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the array of consolidated data |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
39 |
|
protected function getConsolidated(): array |
94
|
|
|
{ |
95
|
39 |
|
if (0 === count($this->consolidated)) { |
96
|
|
|
|
97
|
39 |
|
$cache = $this->getCache(); |
98
|
39 |
|
$cacheId = $this->getLocale() . __METHOD__; |
99
|
|
|
|
100
|
39 |
|
if ($cache->getTtl() > 0) { |
101
|
38 |
|
$cacheId = $cache->getId($cacheId); |
102
|
38 |
|
$consolidated = $cache->read($cacheId); |
103
|
38 |
|
if (0 === count($consolidated)) { |
104
|
4 |
|
$consolidated = $this->downloadConsolidated(); |
105
|
38 |
|
$cache->write($cacheId, $consolidated); |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
1 |
|
$consolidated = $this->downloadConsolidated(); |
109
|
|
|
} |
110
|
|
|
|
111
|
39 |
|
if ('' !== $this->getContext()) { |
112
|
1 |
|
assert(is_array($consolidated['packages'])); |
113
|
1 |
|
$packages = $this->filter($consolidated['packages'], 'contexts', $this->getContext()); |
114
|
1 |
|
$consolidated['packages'] = $packages; |
115
|
|
|
} |
116
|
|
|
|
117
|
39 |
|
$this->setConsolidated($consolidated); |
118
|
|
|
} |
119
|
|
|
|
120
|
39 |
|
return $this->consolidated; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set the consolidated data array |
125
|
|
|
* |
126
|
|
|
* @param array $consolidated |
127
|
|
|
* |
128
|
|
|
* @return Sdk |
129
|
|
|
*/ |
130
|
47 |
|
protected function setConsolidated(array $consolidated): self |
131
|
|
|
{ |
132
|
47 |
|
$this->consolidated = $consolidated; |
133
|
|
|
|
134
|
47 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Download the consolidated data array |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
5 |
|
private function downloadConsolidated(): array |
143
|
|
|
{ |
144
|
5 |
|
$format = 'https://%s/api/%s/consolidated/%s.json'; |
145
|
5 |
|
$uri = sprintf($format, $this->getHostname(), $this->getVersion(), $this->getLocale()); |
146
|
|
|
|
147
|
5 |
|
$options = [ |
148
|
5 |
|
'http' => [ |
149
|
5 |
|
'timeout' => $this->getTimeout(), |
150
|
5 |
|
'method' => 'GET', |
151
|
5 |
|
'header' => sprintf('User-Agent: TIS Download System SDK (PHP %s)', phpversion()), |
152
|
5 |
|
], |
153
|
5 |
|
]; |
154
|
|
|
|
155
|
5 |
|
if ($this->getHostname() === Defaults::HOSTNAME_DEVELOPMENT) { |
156
|
|
|
$options['ssl'] = [ |
157
|
|
|
'verify_peer' => false, |
158
|
|
|
'verify_peer_name' => false, |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
5 |
|
$context = stream_context_create($options); |
163
|
|
|
|
164
|
5 |
|
$json = file_get_contents($uri, false, $context); |
165
|
5 |
|
assert(is_string($json)); |
166
|
|
|
|
167
|
5 |
|
$consolidated = json_decode($json, true); |
168
|
5 |
|
assert(is_array($consolidated)); |
169
|
|
|
|
170
|
5 |
|
return $consolidated; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|