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
|
58 |
|
public function __construct(array $options = []) |
46
|
|
|
{ |
47
|
58 |
|
if (array_key_exists('ttl', $options)) { |
48
|
1 |
|
$ttl = $options['ttl']; |
49
|
|
|
} else { |
50
|
58 |
|
$ttl = Defaults::TTL; |
51
|
|
|
} |
52
|
|
|
|
53
|
58 |
|
$this->setTtl($ttl); |
54
|
|
|
|
55
|
|
|
// not configurable |
56
|
58 |
|
$this->setPath(sys_get_temp_dir()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the path to cache file |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
46 |
|
public function getPath(): string |
65
|
|
|
{ |
66
|
46 |
|
return $this->path; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the path to cache file |
71
|
|
|
* |
72
|
|
|
* @param string $path |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
58 |
|
public function setPath(string $path): self |
77
|
|
|
{ |
78
|
58 |
|
$this->path = $path; |
79
|
|
|
|
80
|
58 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the time-to-live |
85
|
|
|
* |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
44 |
|
public function getTtl(): int |
89
|
|
|
{ |
90
|
44 |
|
return $this->ttl; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the time-to-live |
95
|
|
|
* |
96
|
|
|
* @param int $ttl |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
58 |
|
public function setTtl(int $ttl): self |
101
|
|
|
{ |
102
|
58 |
|
$this->ttl = $ttl; |
103
|
|
|
|
104
|
58 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the cache ID |
109
|
|
|
* |
110
|
|
|
* @param string $uri |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
39 |
|
public function getId(string $uri): string |
115
|
|
|
{ |
116
|
39 |
|
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
|
45 |
|
public function getFilename(string $cacheId, string $user = ''): string |
128
|
|
|
{ |
129
|
45 |
|
if ('' === $user) { |
130
|
44 |
|
$user = $this->getUser(); |
131
|
|
|
} |
132
|
|
|
|
133
|
45 |
|
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
|
46 |
|
public function getUser(): string |
142
|
|
|
{ |
143
|
46 |
|
$userApache = getenv('APACHE_RUN_USER'); |
144
|
46 |
|
$userCli = getenv('LOGNAME'); |
145
|
|
|
|
146
|
46 |
|
if (is_string($userApache)) { |
|
|
|
|
147
|
45 |
|
return trim($userApache); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
if (is_string($userCli)) { |
151
|
1 |
|
return trim($userCli); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return 'nouser'; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|