|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Services\RegistryProcessor |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Services; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Cache\CacheAdapterInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Array based implementation of a registry. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Tim Wagner <[email protected]> |
|
29
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
31
|
|
|
* @link https://github.com/techdivision/import |
|
32
|
|
|
* @link http://www.techdivision.com |
|
33
|
|
|
*/ |
|
34
|
|
|
class RegistryProcessor implements RegistryProcessorInterface |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The cache adapter instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \TechDivision\Import\Cache\CacheAdapterInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $cacheAdapter; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Initialize the repository with the passed connection and utility class name. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \TechDivision\Import\Cache\CacheAdapterInterface $cacheAdapter The cache adapter instance |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(CacheAdapterInterface $cacheAdapter) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->cacheAdapter = $cacheAdapter; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Register the passed attribute under the specified key in the registry. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $key The cache key to use |
|
58
|
|
|
* @param mixed $value The value that has to be cached |
|
59
|
|
|
* @param array $references An array with references to add |
|
60
|
|
|
* @param array $tags An array with tags to add |
|
61
|
|
|
* @param boolean $override Flag that allows to override an exising cache entry |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
* @throws \Exception Is thrown, if the key has already been used |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setAttribute($key, $value, array $references = array(), array $tags = array(), $override = false) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->cacheAdapter->toCache($key, $value, $references, $tags, $override); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Return's the attribute with the passed key from the registry. |
|
73
|
|
|
* |
|
74
|
|
|
* @param mixed $key The key to return the attribute for |
|
75
|
|
|
* |
|
76
|
|
|
* @return mixed The requested attribute |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getAttribute($key) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->cacheAdapter->fromCache($key); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Query whether or not an attribute with the passed key has already been registered. |
|
85
|
|
|
* |
|
86
|
|
|
* @param mixed $key The key to query for |
|
87
|
|
|
* |
|
88
|
|
|
* @return boolean TRUE if the attribute has already been registered, else FALSE |
|
89
|
|
|
*/ |
|
90
|
|
|
public function hasAttribute($key) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->cacheAdapter->isCached($key); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Remove the attribute with the passed key from the registry. |
|
97
|
|
|
* |
|
98
|
|
|
* @param mixed $key The key of the attribute to return |
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
|
|
public function removeAttribute($key) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->cacheAdapter->removeCache($key); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Flush the cache. |
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
public function flushCache() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->cacheAdapter->flushCache(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Raises the value for the attribute with the passed key by one. |
|
119
|
|
|
* |
|
120
|
|
|
* @param mixed $key The key of the attribute to raise the value for |
|
121
|
|
|
* @param mixed $counterName The name of the counter to raise |
|
122
|
|
|
* |
|
123
|
|
|
* @return integer The counter's new value |
|
124
|
|
|
*/ |
|
125
|
|
|
public function raiseCounter($key, $counterName) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->cacheAdapter->raiseCounter($key, $counterName); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Lowers the value for the attribute with the passed key by one. |
|
132
|
|
|
* |
|
133
|
|
|
* @param mixed $key The key of the attribute to lower the value for |
|
134
|
|
|
* @param mixed $counterName The name of the counter to lower |
|
135
|
|
|
* |
|
136
|
|
|
* @return integer The counter's new value |
|
137
|
|
|
*/ |
|
138
|
|
|
public function lowerCounter($key, $counterName) |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->cacheAdapter->lowerCounter($key, $counterName); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* This method merges the passed attributes with an array that |
|
145
|
|
|
* has already been added under the passed key. |
|
146
|
|
|
* |
|
147
|
|
|
* If no value will be found under the passed key, the attributes |
|
148
|
|
|
* will simply be registered. |
|
149
|
|
|
* |
|
150
|
|
|
* @param mixed $key The key of the attributes that has to be merged with the passed ones |
|
151
|
|
|
* @param array $attributes The attributes that has to be merged with the exising ones |
|
152
|
|
|
* |
|
153
|
|
|
* @return void |
|
154
|
|
|
* @throws \Exception Is thrown, if the already registered value is no array |
|
155
|
|
|
* @link http://php.net/array_replace_recursive |
|
156
|
|
|
*/ |
|
157
|
|
|
public function mergeAttributesRecursive($key, array $attributes) |
|
158
|
|
|
{ |
|
159
|
|
|
$this->cacheAdapter->mergeAttributesRecursive($key, $attributes); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Load's the data with the passed key from the registry. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $key The key of the data to load |
|
166
|
|
|
* @param string $delimiter The delimiter to explode the key with |
|
167
|
|
|
* |
|
168
|
|
|
* @return mixed The data |
|
169
|
|
|
*/ |
|
170
|
|
|
public function load($key, $delimiter = '.') |
|
171
|
|
|
{ |
|
172
|
|
|
|
|
173
|
|
|
// explode the key elements by the passed delimiter |
|
174
|
|
|
$elements = explode($delimiter, $key); |
|
175
|
|
|
|
|
176
|
|
|
// load the data for the first key element |
|
177
|
|
|
$data = $this->getAttribute(array_shift($elements)) ; |
|
178
|
|
|
|
|
179
|
|
|
// try to resolve the data recursively by the key elemens |
|
180
|
|
|
while ($k = array_shift($elements)) { |
|
181
|
|
|
if (isset($data[$k])) { |
|
182
|
|
|
$data = $data[$k]; |
|
183
|
|
|
} else { |
|
184
|
|
|
throw new \InvalidArgumentException(sprintf('Can\'t resolve data for registry key "%s"', $k)); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// finally return the loaded data |
|
189
|
|
|
return $data; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|