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 | 9 | ||
25 | public function __construct($magiumConfigurationFile = null) |
||
26 | 9 | { |
|
27 | 4 | if (!$magiumConfigurationFile) { |
|
28 | 4 | $cwd = __DIR__; |
|
29 | 4 | $baseDir = realpath(DIRECTORY_SEPARATOR); |
|
30 | 4 | while ($cwd && $cwd != $baseDir && file_exists($cwd)) { |
|
31 | 4 | $checkFile = $cwd . DIRECTORY_SEPARATOR . 'magium-configuration.xml'; |
|
32 | 3 | if (file_exists($checkFile)) { |
|
33 | 3 | $magiumConfigurationFile = $checkFile; |
|
34 | break; |
||
35 | 4 | } |
|
36 | 4 | $lastPos = strrpos($cwd, DIRECTORY_SEPARATOR); |
|
37 | $cwd = substr($cwd, 0, $lastPos); |
||
38 | } |
||
39 | } |
||
40 | 9 | ||
41 | 8 | if (file_exists($magiumConfigurationFile)) { |
|
42 | $this->file = realpath($magiumConfigurationFile); |
||
43 | 1 | } else { |
|
44 | throw new InvalidConfigurationFileException('Unable to file configuration file: ' . $magiumConfigurationFile); |
||
45 | 8 | } |
|
46 | 8 | $this->baseDir = dirname($this->file); |
|
47 | 8 | chdir($this->baseDir); |
|
48 | 8 | $this->xml = simplexml_load_file($magiumConfigurationFile); |
|
49 | } |
||
50 | |||
51 | protected function buildContextFile() |
||
52 | { |
||
53 | chdir($this->baseDir); |
||
54 | $contextFileCheck = (string)$this->xml->contextConfigurationFile['file']; |
||
55 | $contextFileType = (string)$this->xml->contextConfigurationFile['type']; |
||
56 | $contextFile = realpath($contextFileCheck); |
||
57 | if (!$contextFile) { |
||
58 | throw new MissingConfigurationException('Unable to find context file: ' . $contextFileCheck); |
||
59 | } |
||
60 | $class = 'Magium\Configuration\File\Context\\' . ucfirst($contextFileType) . 'File'; |
||
61 | $reflectionClass = new \ReflectionClass($class); |
||
62 | if ($reflectionClass->isSubclassOf(AbstractContextConfigurationFile::class)) { |
||
63 | $instance = $reflectionClass->newInstance($contextFile); |
||
64 | if ($instance instanceof AbstractContextConfigurationFile) { |
||
65 | return $instance; |
||
66 | } |
||
67 | } |
||
68 | throw new InvalidConfigurationException('Unable to load context configuration file: ' . $contextFileCheck); |
||
69 | } |
||
70 | |||
71 | public function getContextFile() |
||
72 | { |
||
73 | if (!$this->contextFile instanceof AbstractContextConfigurationFile) { |
||
74 | $this->contextFile = $this->buildContextFile(); |
||
75 | } |
||
76 | return $this->contextFile; |
||
77 | } |
||
78 | 2 | ||
79 | public function validateConfigurationFile() |
||
80 | 2 | { |
|
81 | $result = false; |
||
82 | 2 | try { |
|
83 | 2 | $doc = new \DOMDocument(); |
|
84 | 2 | $doc->load($this->file); |
|
85 | 1 | $result = $doc->schemaValidate(__DIR__ . '/../assets/magium-configuration.xsd'); |
|
86 | } catch (\Exception $e) { |
||
87 | // $result value is already set |
||
88 | 2 | } |
|
89 | 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 | 2 | ||
99 | protected function getCache(\SimpleXMLElement $element) |
||
100 | 2 | { |
|
101 | 2 | $cacheFactory = new CacheFactory(); |
|
102 | return $cacheFactory->getCache($element); |
||
103 | } |
||
104 | |||
105 | public function getBuilderFactory() |
||
106 | { |
||
107 | 2 | if (!$this->builderFactory instanceof BuilderFactoryInterface) { |
|
108 | $builderFactoryConfig = $this->xml->builderFactory; |
||
109 | 2 | $class = (string)$builderFactoryConfig['class']; |
|
110 | 2 | if (!$class) { |
|
111 | 2 | $class = BuilderFactory::class; // das default |
|
112 | 2 | } |
|
113 | 2 | $reflection = new \ReflectionClass($class); |
|
114 | if (!$reflection->implementsInterface(BuilderFactoryInterface::class)) { |
||
115 | 2 | throw new InvalidConfigurationException($class . ' must implement ' . BuilderFactoryInterface::class); |
|
116 | 2 | } |
|
117 | $this->builderFactory = $reflection->newInstance($this->xml); |
||
118 | } |
||
119 | 2 | return $this->builderFactory; |
|
120 | 2 | } |
|
121 | |||
122 | /** |
||
123 | * @return BuilderInterface |
||
124 | */ |
||
125 | |||
126 | public function getBuilder() |
||
127 | { |
||
128 | if (!$this->builder instanceof BuilderInterface) { |
||
129 | 2 | $this->builder = $this->getBuilderFactory()->getBuilder(); |
|
130 | } |
||
131 | 1 | return $this->builder; |
|
132 | } |
||
133 | |||
134 | 4 | protected function getRemoteCache() |
|
140 | |||
141 | 3 | protected function getLocalCache() |
|
150 | |||
151 | 4 | public function getManager() |
|
152 | { |
||
153 | 4 | if (!$this->manager instanceof ManagerInterface) { |
|
154 | 4 | $managerClass = Manager::class; |
|
155 | if (isset($this->xml->manager['class'])) { |
||
156 | 3 | $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: