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
|
1 |
|
public static function builderFactory($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT) |
71
|
|
|
{ |
72
|
1 |
|
$me = self::getInstance($magiumConfigurationFile, $context); |
73
|
1 |
|
return $me->getBuilder(); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public static function managerFactory($magiumConfigurationFile = null, $context = ConfigurationRepository::CONTEXT_DEFAULT) |
77
|
|
|
{ |
78
|
1 |
|
$me = self::getInstance($magiumConfigurationFile, $context); |
79
|
1 |
|
return $me->getManager(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setContext($context) |
83
|
|
|
{ |
84
|
|
|
$this->context = $context; |
85
|
|
|
} |
86
|
|
|
|
87
|
5 |
|
protected function buildContextFile() |
88
|
|
|
{ |
89
|
5 |
|
chdir($this->baseDir); |
90
|
5 |
|
$contextFileCheck = (string)$this->xml->contextConfigurationFile['file']; |
91
|
5 |
|
$contextFileType = (string)$this->xml->contextConfigurationFile['type']; |
92
|
5 |
|
$contextFile = realpath($contextFileCheck); |
93
|
5 |
|
if (!$contextFile) { |
94
|
1 |
|
throw new MissingConfigurationException('Unable to find context file: ' . $contextFileCheck); |
95
|
|
|
} |
96
|
4 |
|
$class = 'Magium\Configuration\File\Context\\' . ucfirst($contextFileType) . 'File'; |
97
|
4 |
|
$reflectionClass = new \ReflectionClass($class); |
98
|
4 |
|
if ($reflectionClass->isSubclassOf(AbstractContextConfigurationFile::class)) { |
99
|
3 |
|
$instance = $reflectionClass->newInstance($contextFile); |
100
|
3 |
|
if ($instance instanceof AbstractContextConfigurationFile) { |
101
|
3 |
|
return $instance; |
102
|
|
|
} |
103
|
|
|
} |
104
|
1 |
|
throw new InvalidConfigurationException('Unable to load context configuration file: ' . $contextFileCheck); |
105
|
|
|
} |
106
|
|
|
|
107
|
5 |
|
public function getContextFile() |
108
|
|
|
{ |
109
|
5 |
|
if (!$this->contextFile instanceof AbstractContextConfigurationFile) { |
110
|
5 |
|
$this->contextFile = $this->buildContextFile(); |
111
|
|
|
} |
112
|
3 |
|
return $this->contextFile; |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
public function validateConfigurationFile() |
116
|
|
|
{ |
117
|
2 |
|
$result = false; |
118
|
|
|
try { |
119
|
2 |
|
$doc = new \DOMDocument(); |
120
|
2 |
|
$doc->load($this->file); |
121
|
2 |
|
$result = $doc->schemaValidate(__DIR__ . '/../assets/magium-configuration.xsd'); |
122
|
1 |
|
} catch (\Exception $e) { |
123
|
|
|
// $result value is already set |
124
|
|
|
} |
125
|
2 |
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Retrieves an instance of the cache based off of the XML cache configuration |
130
|
|
|
* |
131
|
|
|
* @param \SimpleXMLElement $element |
132
|
|
|
* @return \Zend\Cache\Storage\StorageInterface |
133
|
|
|
*/ |
134
|
|
|
|
135
|
3 |
|
protected function getCache(\SimpleXMLElement $element) |
136
|
|
|
{ |
137
|
3 |
|
$cacheFactory = new CacheFactory(); |
138
|
3 |
|
return $cacheFactory->getCache($element); |
139
|
|
|
} |
140
|
|
|
|
141
|
4 |
|
public function getBuilderFactory() |
142
|
|
|
{ |
143
|
4 |
|
if (!$this->builderFactory instanceof BuilderFactoryInterface) { |
144
|
4 |
|
$builderFactoryConfig = $this->xml->builderFactory; |
145
|
4 |
|
$class = (string)$builderFactoryConfig['class']; |
146
|
4 |
|
if (!$class) { |
147
|
3 |
|
$class = BuilderFactory::class; // das default |
148
|
|
|
} |
149
|
4 |
|
$reflection = new \ReflectionClass($class); |
150
|
4 |
|
if (!$reflection->implementsInterface(BuilderFactoryInterface::class)) { |
151
|
1 |
|
throw new InvalidConfigurationException($class . ' must implement ' . BuilderFactoryInterface::class); |
152
|
|
|
} |
153
|
3 |
|
$this->builderFactory = $reflection->newInstance(new \SplFileInfo($this->baseDir), $this->xml, $this->getContextFile()); |
154
|
|
|
} |
155
|
3 |
|
return $this->builderFactory; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return BuilderInterface |
160
|
|
|
*/ |
161
|
|
|
|
162
|
4 |
|
public function getBuilder() |
163
|
|
|
{ |
164
|
4 |
|
if (!$this->builder instanceof BuilderInterface) { |
165
|
3 |
|
$this->builder = $this->getBuilderFactory()->getBuilder(); |
166
|
|
|
} |
167
|
3 |
|
return $this->builder; |
168
|
|
|
} |
169
|
|
|
|
170
|
5 |
|
protected function getRemoteCache() |
171
|
|
|
{ |
172
|
5 |
|
$cacheConfig = $this->xml->cache; |
173
|
5 |
|
$globalAdapter = $this->getCache($cacheConfig); |
174
|
5 |
|
return $globalAdapter; |
175
|
|
|
} |
176
|
|
|
|
177
|
4 |
|
protected function getLocalCache() |
178
|
|
|
{ |
179
|
4 |
|
$localCache = null; |
180
|
4 |
|
$localCacheConfig = $this->xml->localCache; |
181
|
4 |
|
if ($localCacheConfig) { |
182
|
2 |
|
$localCache = $this->getCache($localCacheConfig); |
183
|
|
|
} |
184
|
4 |
|
return $localCache; |
185
|
|
|
} |
186
|
|
|
|
187
|
7 |
|
public function getManager() |
188
|
|
|
{ |
189
|
7 |
|
if (!$this->manager instanceof ManagerInterface) { |
190
|
6 |
|
$managerClass = Manager::class; |
191
|
6 |
|
if (isset($this->xml->manager['class'])) { |
192
|
2 |
|
$managerClass = (string)$this->xml->manager['class']; |
193
|
|
|
} |
194
|
6 |
|
$reflectionClass = new \ReflectionClass($managerClass); |
195
|
6 |
|
if ($managerClass == Manager::class) { |
196
|
|
|
// just a shortcut so I don't have to rewrite some complicated unit tests. I'm just lazy. |
197
|
4 |
|
$this->manager = new Manager($this->getRemoteCache(), $this->getBuilder(), $this->getLocalCache()); |
198
|
3 |
|
return $this->manager; |
199
|
|
|
} |
200
|
2 |
|
if (!$reflectionClass->implementsInterface(ManagerInterface::class)) { |
201
|
1 |
|
throw new InvalidConfigurationException('Manager class must implement ' . ManagerInterface::class); |
202
|
|
|
} |
203
|
1 |
|
$manager = $reflectionClass->newInstance(); |
204
|
|
|
/* @var $manager ManagerInterface */ |
205
|
1 |
|
$manager->setBuilder($this->getBuilder()); |
206
|
1 |
|
$manager->setLocalCache($this->getLocalCache()); |
207
|
1 |
|
$manager->setRemoteCache($this->getRemoteCache()); |
208
|
1 |
|
$this->manager = $manager; |
209
|
|
|
} |
210
|
2 |
|
return $this->manager; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.