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