1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Configuration\Manager; |
4
|
|
|
|
5
|
|
|
use Magium\Configuration\Config\Builder; |
6
|
|
|
use Magium\Configuration\Config\BuilderInterface; |
7
|
|
|
use Magium\Configuration\Config\Config; |
8
|
|
|
use Magium\Configuration\Config\ConfigInterface; |
9
|
|
|
use Zend\Cache\Storage\StorageInterface; |
10
|
|
|
|
11
|
|
|
class Manager implements ManagerInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
protected $config = []; |
15
|
|
|
|
16
|
|
|
protected $cache; |
17
|
|
|
protected $localCache; |
18
|
|
|
protected $builder; |
19
|
|
|
protected $configurationLocation; |
20
|
|
|
protected $hashAlgo; |
21
|
|
|
protected $context; |
22
|
|
|
|
23
|
14 |
|
public function __construct( |
24
|
|
|
StorageInterface $cache = null, |
25
|
|
|
BuilderInterface $builder = null, |
26
|
|
|
StorageInterface $localCache = null, |
27
|
|
|
$context = Config::CONTEXT_DEFAULT, |
28
|
|
|
$hashAlgo = 'sha1' |
29
|
|
|
) |
30
|
|
|
{ |
31
|
14 |
|
$this->cache = $cache; |
32
|
14 |
|
$this->builder = $builder; |
33
|
14 |
|
$this->localCache = $localCache; |
34
|
14 |
|
$this->context = $context; |
35
|
14 |
|
$this->hashAlgo = $hashAlgo; |
36
|
14 |
|
} |
37
|
|
|
|
38
|
|
|
public function getBuilder() |
39
|
|
|
{ |
40
|
|
|
return $this->builder; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setContext($context) |
44
|
|
|
{ |
45
|
|
|
$this->context = $context; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setLocalCache(StorageInterface $storage = null) |
49
|
|
|
{ |
50
|
|
|
$this->localCache = $storage; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setRemoteCache(StorageInterface $storage) |
54
|
|
|
{ |
55
|
|
|
$this->cache = $storage; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setBuilder(BuilderInterface $builder) |
59
|
|
|
{ |
60
|
|
|
$this->builder = $builder; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
9 |
|
public function getContextCacheKey($context = Config::CONTEXT_DEFAULT) |
65
|
|
|
{ |
66
|
9 |
|
return strtoupper('current_cache_object_' . $context); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $context The (configurable) context for the needed configuration object |
71
|
|
|
* @return Config |
72
|
|
|
* @throws NoConfigurationException |
73
|
|
|
*/ |
74
|
|
|
|
75
|
9 |
|
public function getConfiguration($context = null, $storeScopeLocally = false) |
76
|
|
|
{ |
77
|
9 |
|
if ($context === null) { |
78
|
2 |
|
$context = $this->context; |
79
|
|
|
} |
80
|
9 |
|
$key = $this->getContextCacheKey($context); |
81
|
9 |
|
if (isset($this->config[$key]) && $this->config[$key] instanceof Config) { |
82
|
2 |
|
return $this->config[$key]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// By default we will get the config location from the remote server for each request. But we can override that. |
86
|
9 |
|
if ($this->localCache instanceof StorageInterface && $storeScopeLocally) { |
87
|
|
|
// If a local cache is defined we check there for the current scope key first |
88
|
2 |
|
$currentConfigItem = $this->localCache->getItem($key); |
89
|
|
|
// if we don't find the scope key we check the global cache |
90
|
2 |
|
if (!$currentConfigItem) { |
91
|
1 |
|
$currentConfigItem = $this->cache->getItem($key); |
92
|
|
|
// If the primary cache gives us a value we store it locally |
93
|
1 |
|
if ($currentConfigItem) { |
94
|
2 |
|
$this->localCache->setItem($key, $currentConfigItem); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
// Local cache is not configured, so we get the current config cache key from the global cache. |
99
|
7 |
|
$currentConfigItem = $this->cache->getItem($key); |
100
|
|
|
} |
101
|
|
|
|
102
|
9 |
|
$config = null; |
103
|
9 |
|
if ($this->localCache instanceof StorageInterface) { |
104
|
|
|
// First we check the local cache for the configuration object if it exists. |
105
|
4 |
|
$config = $this->localCache->getItem($currentConfigItem); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Either way, if the config is null we check the global cache |
109
|
9 |
|
if ($config === null) { |
110
|
8 |
|
$config = $this->cache->getItem($currentConfigItem); |
111
|
8 |
|
if ($config !== null) { |
112
|
4 |
|
if ($this->localCache instanceof StorageInterface) { |
113
|
3 |
|
$this->localCache->setItem($currentConfigItem, $config); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
9 |
|
if ($config) { |
119
|
5 |
|
$config = new Config($config); |
120
|
5 |
|
$this->config[$key] = $config; |
121
|
5 |
|
return $this->config[$key]; |
122
|
|
|
} |
123
|
4 |
|
$config = $this->builder->build($context); |
124
|
4 |
|
$this->storeConfigurationObject($config, $context); |
125
|
4 |
|
$this->config[$key] = $config; |
126
|
4 |
|
return $config; |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
public function storeConfigurationObject(ConfigInterface $config, $context = Config::CONTEXT_DEFAULT) |
130
|
|
|
{ |
131
|
2 |
|
$contextCacheKey = $this->getContextCacheKey($context); |
132
|
2 |
|
$previousConfigKey = $this->cache->getItem($contextCacheKey); |
133
|
2 |
|
$xml = $config->asXML(); |
134
|
2 |
|
$configKey = hash_hmac($this->hashAlgo, $xml, ''); |
135
|
2 |
|
$this->cache->addItem($configKey, $xml); |
136
|
2 |
|
$this->cache->setItem($contextCacheKey, $configKey); |
137
|
|
|
|
138
|
2 |
|
if ($previousConfigKey) { |
139
|
1 |
|
$this->cache->removeItem($previousConfigKey); |
140
|
|
|
} |
141
|
2 |
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|