1 | <?php |
||
14 | class MagiumConfigurationFactory implements MagiumConfigurationFactoryInterface |
||
15 | { |
||
16 | protected $file; |
||
17 | protected $xml; |
||
18 | |||
19 | protected $manager; |
||
20 | protected $builder; |
||
21 | protected $baseDir; |
||
22 | protected $contextFile; |
||
23 | protected $builderFactory; |
||
24 | |||
25 | 13 | public function __construct($magiumConfigurationFile = null) |
|
26 | { |
||
27 | 13 | if (!$magiumConfigurationFile) { |
|
28 | 5 | $cwd = __DIR__; |
|
29 | 5 | $baseDir = realpath(DIRECTORY_SEPARATOR); |
|
30 | 5 | while ($cwd && $cwd != $baseDir && file_exists($cwd)) { |
|
31 | 5 | $checkFile = $cwd . DIRECTORY_SEPARATOR . 'magium-configuration.xml'; |
|
32 | 5 | if (file_exists($checkFile)) { |
|
33 | 4 | $magiumConfigurationFile = $checkFile; |
|
34 | 4 | break; |
|
35 | } |
||
36 | 5 | $lastPos = strrpos($cwd, DIRECTORY_SEPARATOR); |
|
37 | 5 | $cwd = substr($cwd, 0, $lastPos); |
|
38 | } |
||
39 | } |
||
40 | |||
41 | 13 | if (file_exists($magiumConfigurationFile)) { |
|
42 | 12 | $this->file = realpath($magiumConfigurationFile); |
|
43 | } else { |
||
44 | 1 | throw new InvalidConfigurationFileException('Unable to file configuration file: ' . $magiumConfigurationFile); |
|
45 | } |
||
46 | 12 | $this->baseDir = dirname($this->file); |
|
47 | 12 | chdir($this->baseDir); |
|
48 | 12 | $this->xml = simplexml_load_file($magiumConfigurationFile); |
|
49 | 12 | } |
|
50 | |||
51 | 3 | protected function buildContextFile() |
|
52 | { |
||
53 | 3 | chdir($this->baseDir); |
|
54 | 3 | $contextFileCheck = (string)$this->xml->contextConfigurationFile['file']; |
|
55 | 3 | $contextFileType = (string)$this->xml->contextConfigurationFile['type']; |
|
56 | 3 | $contextFile = realpath($contextFileCheck); |
|
57 | 3 | if (!$contextFile) { |
|
58 | 1 | throw new MissingConfigurationException('Unable to find context file: ' . $contextFileCheck); |
|
59 | } |
||
60 | 2 | $class = 'Magium\Configuration\File\Context\\' . ucfirst($contextFileType) . 'File'; |
|
61 | 2 | $reflectionClass = new \ReflectionClass($class); |
|
62 | 2 | if ($reflectionClass->isSubclassOf(AbstractContextConfigurationFile::class)) { |
|
63 | 1 | $instance = $reflectionClass->newInstance($contextFile); |
|
64 | 1 | if ($instance instanceof AbstractContextConfigurationFile) { |
|
65 | 1 | return $instance; |
|
66 | } |
||
67 | } |
||
68 | 1 | throw new InvalidConfigurationException('Unable to load context configuration file: ' . $contextFileCheck); |
|
69 | } |
||
70 | |||
71 | 3 | public function getContextFile() |
|
72 | { |
||
73 | 3 | if (!$this->contextFile instanceof AbstractContextConfigurationFile) { |
|
74 | 3 | $this->contextFile = $this->buildContextFile(); |
|
75 | } |
||
76 | 1 | return $this->contextFile; |
|
77 | } |
||
78 | |||
79 | 2 | public function validateConfigurationFile() |
|
80 | { |
||
81 | 2 | $result = false; |
|
82 | try { |
||
83 | 2 | $doc = new \DOMDocument(); |
|
84 | 2 | $doc->load($this->file); |
|
85 | 2 | $result = $doc->schemaValidate(__DIR__ . '/../assets/magium-configuration.xsd'); |
|
86 | 1 | } catch (\Exception $e) { |
|
87 | // $result value is already set |
||
88 | } |
||
89 | 2 | return $result; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Retrieves an instance of the cache based off of the XML cache configuration |
||
94 | * |
||
95 | * @param \SimpleXMLElement $element |
||
96 | * @return \Zend\Cache\Storage\StorageInterface |
||
97 | */ |
||
98 | |||
99 | 1 | protected function getCache(\SimpleXMLElement $element) |
|
100 | { |
||
101 | 1 | $cacheFactory = new CacheFactory(); |
|
102 | 1 | return $cacheFactory->getCache($element); |
|
103 | } |
||
104 | |||
105 | 3 | public function getBuilderFactory() |
|
106 | { |
||
107 | 3 | if (!$this->builderFactory instanceof BuilderFactoryInterface) { |
|
108 | 3 | $builderFactoryConfig = $this->xml->builderFactory; |
|
109 | 3 | $class = (string)$builderFactoryConfig['class']; |
|
110 | 3 | if (!$class) { |
|
111 | 2 | $class = BuilderFactory::class; // das default |
|
112 | } |
||
113 | 3 | $reflection = new \ReflectionClass($class); |
|
114 | 3 | if (!$reflection->implementsInterface(BuilderFactoryInterface::class)) { |
|
115 | 1 | throw new InvalidConfigurationException($class . ' must implement ' . BuilderFactoryInterface::class); |
|
116 | } |
||
117 | 2 | $this->builderFactory = $reflection->newInstance($this->xml); |
|
118 | } |
||
119 | 2 | return $this->builderFactory; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return BuilderInterface |
||
124 | */ |
||
125 | |||
126 | 2 | public function getBuilder() |
|
127 | { |
||
128 | 2 | if (!$this->builder instanceof BuilderInterface) { |
|
129 | 2 | $this->builder = $this->getBuilderFactory()->getBuilder(); |
|
130 | } |
||
131 | 1 | return $this->builder; |
|
132 | } |
||
133 | |||
134 | 3 | protected function getRemoteCache() |
|
140 | |||
141 | 4 | protected function getLocalCache() |
|
150 | |||
151 | 4 | public function getManager() |
|
152 | { |
||
153 | 4 | if (!$this->manager instanceof ManagerInterface) { |
|
154 | 4 | $managerClass = Manager::class; |
|
155 | 4 | if (isset($this->xml->manager['class'])) { |
|
156 | $managerClass = $this->xml->manager['class']; |
||
157 | } |
||
176 | |||
177 | } |
||
178 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: