1 <?php
2 3 4 5 6 7
8
9 defined('FOF_INCLUDED') or die();
10
11 12 13 14 15 16 17
18 class FOFConfigProvider
19 {
20 21 22 23 24
25 public static $configurations = array();
26
27 28 29 30 31 32 33 34
35 public function parseComponent($component, $force = false)
36 {
37 if (!$force && isset(self::$configurations[$component]))
38 {
39 return;
40 }
41
42 if (FOFPlatform::getInstance()->isCli())
43 {
44 $order = array('cli', 'backend');
45 }
46 elseif (FOFPlatform::getInstance()->isBackend())
47 {
48 $order = array('backend');
49 }
50 else
51 {
52 $order = array('frontend');
53 }
54
55 $order[] = 'common';
56
57 $order = array_reverse($order);
58 self::$configurations[$component] = array();
59
60 foreach ($order as $area)
61 {
62 $config = $this->parseComponentArea($component, $area);
63 self::$configurations[$component] = array_merge_recursive(self::$configurations[$component], $config);
64 }
65 }
66
67 68 69 70 71 72 73 74 75 76
77 public function get($variable, $default = null)
78 {
79 static $domains = null;
80
81 if (is_null($domains))
82 {
83 $domains = $this->getDomains();
84 }
85
86 list($component, $domain, $var) = explode('.', $variable, 3);
87
88 if (!isset(self::$configurations[$component]))
89 {
90 $this->parseComponent($component);
91 }
92
93 if (!in_array($domain, $domains))
94 {
95 return $default;
96 }
97
98 $class = 'FOFConfigDomain' . ucfirst($domain);
99 $o = new $class;
100
101 return $o->get(self::$configurations[$component], $var, $default);
102 }
103
104 105 106 107 108 109 110 111
112 protected function parseComponentArea($component, $area)
113 {
114
115 $ret = array();
116
117
118 $componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($component);
119 $filesystem = FOFPlatform::getInstance()->getIntegrationObject('filesystem');
120
121
122 $path = $componentPaths['admin'];
123 $path = $filesystem->pathCheck($path);
124
125 if (!$filesystem->folderExists($path))
126 {
127 return $ret;
128 }
129
130
131 $filename = $path . '/fof.xml';
132
133 if (!$filesystem->fileExists($filename))
134 {
135 return $ret;
136 }
137
138 $data = file_get_contents($filename);
139
140
141 $xml = simplexml_load_string($data);
142
143 if (!($xml instanceof SimpleXMLElement))
144 {
145 return $ret;
146 }
147
148
149 $areaData = $xml->xpath('//' . $area);
150
151 if (empty($areaData))
152 {
153 return $ret;
154 }
155
156 $xml = array_shift($areaData);
157
158
159 $domains = $this->getDomains();
160
161 foreach ($domains as $dom)
162 {
163 $class = 'FOFConfigDomain' . ucfirst($dom);
164
165 if (class_exists($class, true))
166 {
167 $o = new $class;
168 $o->parseDomain($xml, $ret);
169 }
170 }
171
172
173 return $ret;
174 }
175
176 177 178 179 180
181 protected function getDomains()
182 {
183 static $domains = array();
184
185 if (empty($domains))
186 {
187 $filesystem = FOFPlatform::getInstance()->getIntegrationObject('filesystem');
188
189 $files = $filesystem->folderFiles(__DIR__ . '/domain', '.php');
190
191 if (!empty($files))
192 {
193 foreach ($files as $file)
194 {
195 $domain = basename($file, '.php');
196
197 if ($domain == 'interface')
198 {
199 continue;
200 }
201
202 $domain = preg_replace('/[^A-Za-z0-9]/', '', $domain);
203 $domains[] = $domain;
204 }
205
206 $domains = array_unique($domains);
207 }
208 }
209
210 return $domains;
211 }
212 }
213