1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Configuration; |
4
|
|
|
|
5
|
|
|
use Magium\Configuration\Config\BuilderFactory; |
6
|
|
|
use Magium\Configuration\Config\BuilderFactoryInterface; |
7
|
|
|
use Magium\Configuration\Config\BuilderInterface; |
8
|
|
|
use Magium\Configuration\Config\Repository\ConfigurationRepository; |
9
|
|
|
use Magium\Configuration\Config\Context; |
10
|
|
|
use Magium\Configuration\Config\MissingConfigurationException; |
11
|
|
|
use Magium\Configuration\File\Context\AbstractContextConfigurationFile; |
12
|
|
|
use Magium\Configuration\Manager\CacheFactory; |
13
|
|
|
use Magium\Configuration\Manager\Manager; |
14
|
|
|
use Magium\Configuration\Manager\ManagerInterface; |
15
|
|
|
|
16
|
|
|
class MagiumConfigurationFactory implements MagiumConfigurationFactoryInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
protected $file; |
20
|
|
|
protected $xml; |
21
|
|
|
|
22
|
|
|
protected $manager; |
23
|
|
|
protected $builder; |
24
|
|
|
protected $baseDir; |
25
|
|
|
protected $contextFile; |
26
|
|
|
protected $builderFactory; |
27
|
|
|
protected $context = ConfigurationRepository::CONTEXT_DEFAULT; |
28
|
|
|
|
29
|
|
|
protected static $me; |
30
|
|
|
|
31
|
16 |
|
public function __construct($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT) |
32
|
|
|
{ |
33
|
16 |
|
self::$me = $this; |
34
|
16 |
|
if ($context instanceof Context) { |
35
|
1 |
|
$context = $context->getContext(); |
36
|
|
|
} |
37
|
16 |
|
$this->context = $context; |
38
|
16 |
|
if (!$magiumConfigurationFile) { |
39
|
7 |
|
$cwd = __DIR__; |
40
|
7 |
|
$baseDir = realpath(DIRECTORY_SEPARATOR); |
41
|
7 |
|
while ($cwd && $cwd != $baseDir && file_exists($cwd)) { |
42
|
7 |
|
$checkFile = $cwd . DIRECTORY_SEPARATOR . 'magium-configuration.xml'; |
43
|
7 |
|
if (file_exists($checkFile)) { |
44
|
6 |
|
$magiumConfigurationFile = $checkFile; |
45
|
6 |
|
break; |
46
|
|
|
} |
47
|
7 |
|
$lastPos = strrpos($cwd, DIRECTORY_SEPARATOR); |
48
|
7 |
|
$cwd = substr($cwd, 0, $lastPos); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
16 |
|
if (file_exists($magiumConfigurationFile)) { |
53
|
15 |
|
$this->file = realpath($magiumConfigurationFile); |
54
|
|
|
} else { |
55
|
1 |
|
throw new InvalidConfigurationFileException('Unable to file configuration file: ' . $magiumConfigurationFile); |
56
|
|
|
} |
57
|
15 |
|
$this->baseDir = dirname($this->file); |
58
|
15 |
|
chdir($this->baseDir); |
59
|
15 |
|
$this->xml = simplexml_load_file($magiumConfigurationFile); |
60
|
15 |
|
} |
61
|
|
|
|
62
|
2 |
|
protected static function getInstance($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT) |
|
|
|
|
63
|
|
|
{ |
64
|
2 |
|
if (!self::$me instanceof self) { |
65
|
|
|
new self($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT); |
66
|
|
|
} |
67
|
2 |
|
return self::$me; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function configurationFactory($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT) |
71
|
|
|
{ |
72
|
|
|
$me = self::getInstance($magiumConfigurationFile, $context); |
73
|
|
|
return $me->getConfiguration($context); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public static function builderFactory($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT) |
77
|
|
|
{ |
78
|
1 |
|
$me = self::getInstance($magiumConfigurationFile, $context); |
79
|
1 |
|
return $me->getBuilder(); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public static function managerFactory($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT) |
83
|
|
|
{ |
84
|
1 |
|
$me = self::getInstance($magiumConfigurationFile, $context); |
85
|
1 |
|
return $me->getManager(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getConfiguration($context = ConfigurationRepository::CONTEXT_DEFAULT) |
89
|
|
|
{ |
90
|
|
|
return $this->getManager()->getConfiguration($context); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function setContext($context) |
94
|
|
|
{ |
95
|
|
|
$this->context = $context; |
96
|
|
|
} |
97
|
|
|
|
98
|
5 |
|
protected function buildContextFile() |
99
|
|
|
{ |
100
|
5 |
|
chdir($this->baseDir); |
101
|
5 |
|
$contextFileCheck = (string)$this->xml->contextConfigurationFile['file']; |
102
|
5 |
|
$contextFileType = (string)$this->xml->contextConfigurationFile['type']; |
103
|
5 |
|
$contextFile = realpath($contextFileCheck); |
104
|
5 |
|
if (!$contextFile) { |
105
|
1 |
|
throw new MissingConfigurationException('Unable to find context file: ' . $contextFileCheck); |
106
|
|
|
} |
107
|
4 |
|
$class = 'Magium\Configuration\File\Context\\' . ucfirst($contextFileType) . 'File'; |
108
|
4 |
|
$reflectionClass = new \ReflectionClass($class); |
109
|
4 |
|
if ($reflectionClass->isSubclassOf(AbstractContextConfigurationFile::class)) { |
110
|
3 |
|
$instance = $reflectionClass->newInstance($contextFile); |
111
|
3 |
|
if ($instance instanceof AbstractContextConfigurationFile) { |
112
|
3 |
|
return $instance; |
113
|
|
|
} |
114
|
|
|
} |
115
|
1 |
|
throw new InvalidConfigurationException('Unable to load context configuration file: ' . $contextFileCheck); |
116
|
|
|
} |
117
|
|
|
|
118
|
5 |
|
public function getContextFile() |
119
|
|
|
{ |
120
|
5 |
|
if (!$this->contextFile instanceof AbstractContextConfigurationFile) { |
121
|
5 |
|
$this->contextFile = $this->buildContextFile(); |
122
|
|
|
} |
123
|
3 |
|
return $this->contextFile; |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
public function validateConfigurationFile() |
127
|
|
|
{ |
128
|
2 |
|
$result = false; |
129
|
|
|
try { |
130
|
2 |
|
$doc = new \DOMDocument(); |
131
|
2 |
|
$doc->load($this->file); |
132
|
2 |
|
$result = $doc->schemaValidate(__DIR__ . '/../assets/magium-configuration.xsd'); |
133
|
1 |
|
} catch (\Exception $e) { |
134
|
|
|
// $result value is already set |
135
|
|
|
} |
136
|
2 |
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Retrieves an instance of the cache based off of the XML cache configuration |
141
|
|
|
* |
142
|
|
|
* @param \SimpleXMLElement $element |
143
|
|
|
* @return \Zend\Cache\Storage\StorageInterface |
144
|
|
|
*/ |
145
|
|
|
|
146
|
3 |
|
protected function getCache(\SimpleXMLElement $element) |
147
|
|
|
{ |
148
|
3 |
|
$cacheFactory = new CacheFactory(); |
149
|
3 |
|
return $cacheFactory->getCache($element); |
150
|
|
|
} |
151
|
|
|
|
152
|
4 |
|
public function getBuilderFactory() |
153
|
|
|
{ |
154
|
4 |
|
if (!$this->builderFactory instanceof BuilderFactoryInterface) { |
155
|
4 |
|
$builderFactoryConfig = $this->xml->builderFactory; |
156
|
4 |
|
$class = (string)$builderFactoryConfig['class']; |
157
|
4 |
|
if (!$class) { |
158
|
3 |
|
$class = BuilderFactory::class; // das default |
159
|
|
|
} |
160
|
4 |
|
$reflection = new \ReflectionClass($class); |
161
|
4 |
|
if (!$reflection->implementsInterface(BuilderFactoryInterface::class)) { |
162
|
1 |
|
throw new InvalidConfigurationException($class . ' must implement ' . BuilderFactoryInterface::class); |
163
|
|
|
} |
164
|
3 |
|
$this->builderFactory = $reflection->newInstance(new \SplFileInfo($this->baseDir), $this->xml, $this->getContextFile()); |
165
|
|
|
} |
166
|
3 |
|
return $this->builderFactory; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return BuilderInterface |
171
|
|
|
*/ |
172
|
|
|
|
173
|
4 |
|
public function getBuilder() |
174
|
|
|
{ |
175
|
4 |
|
if (!$this->builder instanceof BuilderInterface) { |
176
|
3 |
|
$this->builder = $this->getBuilderFactory()->getBuilder(); |
177
|
|
|
} |
178
|
3 |
|
return $this->builder; |
179
|
|
|
} |
180
|
|
|
|
181
|
5 |
|
protected function getRemoteCache() |
182
|
|
|
{ |
183
|
5 |
|
$cacheConfig = $this->xml->cache; |
184
|
5 |
|
$globalAdapter = $this->getCache($cacheConfig); |
185
|
5 |
|
return $globalAdapter; |
186
|
|
|
} |
187
|
|
|
|
188
|
4 |
|
protected function getLocalCache() |
189
|
|
|
{ |
190
|
4 |
|
$localCache = null; |
191
|
4 |
|
$localCacheConfig = $this->xml->localCache; |
192
|
4 |
|
if ($localCacheConfig) { |
193
|
2 |
|
$localCache = $this->getCache($localCacheConfig); |
194
|
|
|
} |
195
|
4 |
|
return $localCache; |
196
|
|
|
} |
197
|
|
|
|
198
|
7 |
|
public function getManager() |
199
|
|
|
{ |
200
|
7 |
|
if (!$this->manager instanceof ManagerInterface) { |
201
|
6 |
|
$managerClass = Manager::class; |
202
|
6 |
|
if (isset($this->xml->manager['class'])) { |
203
|
2 |
|
$managerClass = (string)$this->xml->manager['class']; |
204
|
|
|
} |
205
|
6 |
|
$reflectionClass = new \ReflectionClass($managerClass); |
206
|
6 |
|
if ($managerClass == Manager::class) { |
207
|
|
|
// just a shortcut so I don't have to rewrite some complicated unit tests. I'm just lazy. |
208
|
4 |
|
$this->manager = new Manager($this->getRemoteCache(), $this->getBuilder(), $this->getLocalCache()); |
209
|
3 |
|
return $this->manager; |
210
|
|
|
} |
211
|
2 |
|
if (!$reflectionClass->implementsInterface(ManagerInterface::class)) { |
212
|
1 |
|
throw new InvalidConfigurationException('Manager class must implement ' . ManagerInterface::class); |
213
|
|
|
} |
214
|
1 |
|
$manager = $reflectionClass->newInstance(); |
215
|
|
|
/* @var $manager ManagerInterface */ |
216
|
1 |
|
$manager->setBuilder($this->getBuilder()); |
217
|
1 |
|
$manager->setLocalCache($this->getLocalCache()); |
218
|
1 |
|
$manager->setRemoteCache($this->getRemoteCache()); |
219
|
1 |
|
$this->manager = $manager; |
220
|
|
|
} |
221
|
2 |
|
return $this->manager; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.