|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Cache\Collection\Utils\CacheKeys |
|
5
|
|
|
* |
|
6
|
|
|
* PHP version 7 |
|
7
|
|
|
* |
|
8
|
|
|
* @author Tim Wagner <[email protected]> |
|
9
|
|
|
* @copyright 2021 TechDivision GmbH <[email protected]> |
|
10
|
|
|
* @license https://opensource.org/licenses/MIT |
|
11
|
|
|
* @link https://github.com/techdivision/import-cache-collection |
|
12
|
|
|
* @link http://www.techdivision.com |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace TechDivision\Import\Cache\Collection\Utils; |
|
16
|
|
|
|
|
17
|
|
|
use TechDivision\Import\Cache\Utils\CacheKeyUtilInterface; |
|
18
|
|
|
use TechDivision\Import\Cache\Configuration\ConfigurationInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A utility class to create cache keys. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Tim Wagner <[email protected]> |
|
24
|
|
|
* @copyright 2021 TechDivision GmbH <[email protected]> |
|
25
|
|
|
* @license https://opensource.org/licenses/MIT |
|
26
|
|
|
* @link https://github.com/techdivision/import-cache-collection |
|
27
|
|
|
* @link http://www.techdivision.com |
|
28
|
|
|
*/ |
|
29
|
|
|
class CacheKeyUtil implements CacheKeyUtilInterface |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The separator for cache key elements. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
const SEPARATOR = '-'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The prefix to use. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $prefix; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Initializes the cache key util with the passed configuration. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \TechDivision\Import\Cache\Configuration\ConfigurationInterface $configuration The configuration instance |
|
50
|
|
|
*/ |
|
51
|
20 |
|
public function __construct(ConfigurationInterface $configuration) |
|
52
|
|
|
{ |
|
53
|
20 |
|
$this->prefix = $configuration->getSerial(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Creates a unique cache key from the passed data. |
|
58
|
|
|
* |
|
59
|
|
|
* @param mixed $data The date to create the cache key from |
|
60
|
|
|
* @param boolean $usePrefix Flag to signal using the prefix or not |
|
61
|
|
|
* |
|
62
|
|
|
* @return string The generated cache key |
|
63
|
|
|
* @throws \Exception Is thrown if the passed data is not supported to create a cache key from |
|
64
|
|
|
*/ |
|
65
|
20 |
|
public function cacheKey($data, $usePrefix = true) |
|
66
|
|
|
{ |
|
67
|
|
|
|
|
68
|
|
|
// return the cache key for scalar data |
|
69
|
20 |
|
if (is_scalar($data)) { |
|
70
|
20 |
|
return $this->prefix($this->scalarKey($data), $usePrefix); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// return the cache key for an array |
|
74
|
|
|
if (is_array($data)) { |
|
75
|
|
|
return $this->prefix($this->arrayKey($data), $usePrefix); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// return the cache key for an object |
|
79
|
|
|
if (is_object($data)) { |
|
80
|
|
|
return $this->prefix($this->objectKey($data), $usePrefix); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// throw an exception if the passed data type is NOT supported |
|
84
|
|
|
throw new \Exception( |
|
85
|
|
|
sprintf('Found not supported data type "%s" when preparing cache key', gettype($data)) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Prefixes the cache key, e. g. with the products serial. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $cacheKey The cache key to prefix |
|
93
|
|
|
* @param boolean $usePrefix Flag to signal using the prefix or not |
|
94
|
|
|
* |
|
95
|
|
|
* @return string The prefixed cache key |
|
96
|
|
|
*/ |
|
97
|
20 |
|
protected function prefix($cacheKey, $usePrefix) |
|
98
|
|
|
{ |
|
99
|
20 |
|
return $usePrefix ? $this->prefix . $cacheKey : ltrim($cacheKey, CacheKeyUtil::SEPARATOR); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Creates a cache key for a scalar values. |
|
104
|
|
|
* |
|
105
|
|
|
* @param mixed $data The scalar value to c |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
20 |
|
protected function scalarKey($data) |
|
110
|
|
|
{ |
|
111
|
20 |
|
return CacheKeyUtil::SEPARATOR . $data; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Creates a cache key from the passed object. |
|
116
|
|
|
* |
|
117
|
|
|
* The object MUST implement the __toString method, else an exception will be thrown. |
|
118
|
|
|
* |
|
119
|
|
|
* @param object $data The object to create the cache key for |
|
120
|
|
|
* |
|
121
|
|
|
* @return string The cache key from the object's __toString method |
|
122
|
|
|
* @throws \Exception Is thrown, if the object doesn't implement the __toString method |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function objectKey($data) |
|
125
|
|
|
{ |
|
126
|
|
|
|
|
127
|
|
|
// query whether or not the object implements the __toString method |
|
128
|
|
|
if (method_exists($data, '__toString')) { |
|
129
|
|
|
return CacheKeyUtil::SEPARATOR . $data->__toString(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// throw an exception if not |
|
133
|
|
|
throw new \Exception( |
|
134
|
|
|
sprintf('Class "%s" doesn\'t implement necessary method "__toString" to create a cache key', get_class($data)) |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Creates a cache key from the passed array. |
|
140
|
|
|
* |
|
141
|
|
|
* @param array $data The array to create the cache key for |
|
142
|
|
|
* |
|
143
|
|
|
* @return string The cache key created from the array's key => value pairs |
|
144
|
|
|
* @throws \Exception Is thrown, if the array contains unsupported values or the array is empty |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function arrayKey(array $data) |
|
147
|
|
|
{ |
|
148
|
|
|
|
|
149
|
|
|
// query whether or not the array contains data |
|
150
|
|
|
if (sizeof($data) > 0) { |
|
151
|
|
|
// intialize the cache key |
|
152
|
|
|
$cacheKey = ''; |
|
153
|
|
|
// iterate over the array's values, prepare and finally return the cache key |
|
154
|
|
|
foreach ($data as $key => $value) { |
|
155
|
|
|
if (is_scalar($value)) { |
|
156
|
|
|
$cacheKey .= $this->scalarKey($key) . $this->scalarKey($value); |
|
157
|
|
|
} elseif (is_array($value)) { |
|
158
|
|
|
$cacheKey .= $this->scalarKey($key) . $this->arrayKey($value); |
|
159
|
|
|
} elseif (is_object($data)) { |
|
160
|
|
|
$cacheKey .= $this->scalarKey($key) . $this->objectKey($value); |
|
161
|
|
|
} else { |
|
162
|
|
|
throw new \Exception( |
|
163
|
|
|
sprintf('Found not supported data type "%s" for key "%s" in array when preparing cache key', $key, gettype($value)) |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// return the cache key |
|
169
|
|
|
return $cacheKey; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// throw an exception if the array contains no data |
|
173
|
|
|
throw new \Exception('Array to create the cache key from contains no data'); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|