|
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\Cache; |
|
16
|
|
|
|
|
17
|
|
|
use Tisd\Sdk\Defaults\Defaults; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class AbstractCache |
|
21
|
|
|
* |
|
22
|
|
|
* @package Tisd\Sdk\Cache |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractCache |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Path to cache file |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected string $path; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Time-to-live in seconds |
|
35
|
|
|
* |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
protected int $ttl; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* AbstractCache constructor |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $options |
|
44
|
|
|
*/ |
|
45
|
57 |
|
public function __construct(array $options = []) |
|
46
|
|
|
{ |
|
47
|
57 |
|
if (array_key_exists('ttl', $options)) { |
|
48
|
1 |
|
$ttl = $options['ttl']; |
|
49
|
|
|
} else { |
|
50
|
57 |
|
$ttl = Defaults::TTL; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
57 |
|
$this->setTtl($ttl); |
|
54
|
|
|
|
|
55
|
|
|
// not configurable |
|
56
|
57 |
|
$this->setPath(sys_get_temp_dir()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the path to cache file |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
45 |
|
public function getPath(): string |
|
65
|
|
|
{ |
|
66
|
45 |
|
return $this->path; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the path to cache file |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $path |
|
73
|
|
|
* |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
57 |
|
public function setPath(string $path): self |
|
77
|
|
|
{ |
|
78
|
57 |
|
$this->path = $path; |
|
79
|
|
|
|
|
80
|
57 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the time-to-live |
|
85
|
|
|
* |
|
86
|
|
|
* @return int |
|
87
|
|
|
*/ |
|
88
|
43 |
|
public function getTtl(): int |
|
89
|
|
|
{ |
|
90
|
43 |
|
return $this->ttl; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set the time-to-live |
|
95
|
|
|
* |
|
96
|
|
|
* @param int $ttl |
|
97
|
|
|
* |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
57 |
|
public function setTtl(int $ttl): self |
|
101
|
|
|
{ |
|
102
|
57 |
|
$this->ttl = $ttl; |
|
103
|
|
|
|
|
104
|
57 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the cache ID |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $uri |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
38 |
|
public function getId(string $uri): string |
|
115
|
|
|
{ |
|
116
|
38 |
|
return hash('sha256', $uri); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get the filename of the cache file |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $cacheId |
|
123
|
|
|
* @param string $user |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
44 |
|
public function getFilename(string $cacheId, string $user = ''): string |
|
128
|
|
|
{ |
|
129
|
44 |
|
if ('' === $user) { |
|
130
|
43 |
|
$user = $this->getUser(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
44 |
|
return sprintf('%s/tisd_sdk_cache_%s_%s.php', $this->getPath(), $cacheId, $user); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get the unix username of the executing user |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
45 |
|
public function getUser(): string |
|
142
|
|
|
{ |
|
143
|
45 |
|
$userApache = getenv('APACHE_RUN_USER'); |
|
144
|
45 |
|
$userCli = getenv('LOGNAME'); |
|
145
|
|
|
|
|
146
|
45 |
|
if (is_string($userApache)) { |
|
|
|
|
|
|
147
|
44 |
|
return trim($userApache); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
if (is_string($userCli)) { |
|
151
|
1 |
|
return trim($userCli); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return 'nouser'; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|