1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Configuration; |
4
|
|
|
|
5
|
|
|
use Magium\Configuration\Config\Builder; |
6
|
|
|
use Magium\Configuration\Config\BuilderFactory; |
7
|
|
|
use Magium\Configuration\Config\BuilderFactoryInterface; |
8
|
|
|
use Magium\Configuration\Config\InvalidConfigurationLocationException; |
9
|
|
|
use Magium\Configuration\Config\MissingConfigurationException; |
10
|
|
|
use Magium\Configuration\File\Context\AbstractContextConfigurationFile; |
11
|
|
|
use Magium\Configuration\Manager\CacheFactory; |
12
|
|
|
use Magium\Configuration\Manager\Manager; |
13
|
|
|
|
14
|
|
|
class MagiumConfigurationFactory |
15
|
|
|
{ |
16
|
|
|
protected $file; |
17
|
|
|
protected $xml; |
18
|
|
|
|
19
|
|
|
protected $manager; |
20
|
|
|
protected $builder; |
21
|
|
|
protected $baseDir; |
22
|
|
|
protected $contextFile; |
23
|
|
|
|
24
|
|
|
public function __construct($magiumConfigurationFile = null) |
25
|
|
|
{ |
26
|
|
|
if (!$magiumConfigurationFile) { |
27
|
|
|
$cwd = __DIR__; |
28
|
|
|
$baseDir = realpath(DIRECTORY_SEPARATOR); |
29
|
|
|
while ($cwd && $cwd != $baseDir && file_exists($cwd)) { |
30
|
|
|
$checkFile = $cwd . DIRECTORY_SEPARATOR . 'magium-configuration.xml'; |
31
|
|
|
if (file_exists($checkFile)) { |
32
|
|
|
$magiumConfigurationFile = $checkFile; |
33
|
|
|
break; |
34
|
|
|
} |
35
|
|
|
$lastPos = strrpos($cwd, DIRECTORY_SEPARATOR); |
36
|
|
|
$cwd = substr($cwd, 0, $lastPos); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (file_exists($magiumConfigurationFile)) { |
41
|
|
|
$this->file = realpath($magiumConfigurationFile); |
42
|
|
|
} else { |
43
|
|
|
throw new InvalidConfigurationFileException('Unable to file configuration file: ' . $magiumConfigurationFile); |
44
|
|
|
} |
45
|
|
|
$this->baseDir = dirname($this->file); |
46
|
|
|
chdir($this->baseDir); |
47
|
|
|
$this->xml = simplexml_load_file($magiumConfigurationFile); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function buildContextFile() |
51
|
|
|
{ |
52
|
|
|
chdir($this->baseDir); |
53
|
|
|
$contextFileCheck = (string)$this->xml->contextConfigurationFile['file']; |
54
|
|
|
$contextFileType = (string)$this->xml->contextConfigurationFile['type']; |
55
|
|
|
$contextFile = realpath($contextFileCheck); |
56
|
|
|
if (!$contextFile) { |
57
|
|
|
throw new MissingConfigurationException('Unable to find context file: ' . $contextFileCheck); |
58
|
|
|
} |
59
|
|
|
$class = 'Magium\Configuration\File\Context\\' . ucfirst($contextFileType) . 'File'; |
60
|
|
|
$reflectionClass = new \ReflectionClass($class); |
61
|
|
|
if ($reflectionClass->isSubclassOf(AbstractContextConfigurationFile::class)) { |
62
|
|
|
$instance = $reflectionClass->newInstance($contextFile); |
63
|
|
|
if ($instance instanceof AbstractContextConfigurationFile) { |
64
|
|
|
return $instance; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getContextFile() |
70
|
|
|
{ |
71
|
|
|
if (!$this->contextFile) { |
|
|
|
|
72
|
|
|
// if ($this->) |
|
|
|
|
73
|
|
|
} |
74
|
|
|
return $this->contextFile; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function validateConfigurationFile() |
78
|
|
|
{ |
79
|
|
|
$result = false; |
80
|
|
|
try { |
81
|
|
|
$doc = new \DOMDocument(); |
82
|
|
|
$doc->load($this->file); |
83
|
|
|
$result = $doc->schemaValidate(__DIR__ . '/../assets/magium-configuration.xsd'); |
84
|
|
|
} catch (\Exception $e) { |
85
|
|
|
// $result value is already set |
86
|
|
|
} |
87
|
|
|
return $result; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Retrieves an instance of the cache based off of the XML cache configuration |
92
|
|
|
* |
93
|
|
|
* @param \SimpleXMLElement $element |
94
|
|
|
* @return \Zend\Cache\Storage\StorageInterface |
95
|
|
|
*/ |
96
|
|
|
|
97
|
|
|
protected function getCache(\SimpleXMLElement $element) |
98
|
|
|
{ |
99
|
|
|
$cacheFactory = new CacheFactory(); |
100
|
|
|
return $cacheFactory->getCache($element); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Builder |
105
|
|
|
*/ |
106
|
|
|
public function getBuilder() |
107
|
|
|
{ |
108
|
|
|
if (!$this->builder instanceof Builder) { |
109
|
|
|
$builderFactoryConfig = $this->xml->builderFactory; |
110
|
|
|
$class = (string)$builderFactoryConfig['class']; |
111
|
|
|
if (!$class) { |
112
|
|
|
$class = BuilderFactory::class; // das default |
113
|
|
|
} |
114
|
|
|
$reflection = new \ReflectionClass($class); |
115
|
|
|
if (!$reflection->implementsInterface(BuilderFactoryInterface::class)) { |
116
|
|
|
throw new InvalidConfigurationException($class . ' must implement ' . BuilderFactoryInterface::class); |
117
|
|
|
} |
118
|
|
|
$builderFactory = $reflection->newInstance($this->xml); |
119
|
|
|
if (!$builderFactory instanceof BuilderFactoryInterface) { |
120
|
|
|
throw new InvalidConfigurationException( |
121
|
|
|
sprintf( |
122
|
|
|
'The builder factory %s must implement %s', |
123
|
|
|
get_class($builderFactory), |
124
|
|
|
BuilderFactoryInterface::class |
125
|
|
|
) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
$this->builder = $builderFactory->getBuilder(); |
129
|
|
|
} |
130
|
|
|
return $this->builder; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function getRemoteCache() |
134
|
|
|
{ |
135
|
|
|
$cacheConfig = $this->xml->cache; |
136
|
|
|
$globalAdapter = $this->getCache($cacheConfig); |
137
|
|
|
return $globalAdapter; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function getLocalCache() |
141
|
|
|
{ |
142
|
|
|
$localCache = null; |
143
|
|
|
$localCacheConfig = $this->xml->localCache; |
144
|
|
|
if ($localCacheConfig) { |
145
|
|
|
$localCache = $this->getCache($localCacheConfig); |
146
|
|
|
} |
147
|
|
|
return $localCache; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getManager() |
151
|
|
|
{ |
152
|
|
|
if (!$this->manager instanceof Manager) { |
153
|
|
|
$this->manager = new Manager($this->getRemoteCache(), $this->getBuilder(), $this->getLocalCache()); |
154
|
|
|
} |
155
|
|
|
return $this->manager; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.