forked from flack/openpsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.php
194 lines (179 loc) · 6.38 KB
/
configuration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* @package midcom.baseclasses
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License
*/
/**
* Configuration class for components.
*
* @package midcom.baseclasses
*/
class midcom_baseclasses_components_configuration
{
/**
* This is a component specific global data storage area, which should
* be used for stuff like default configurations etc. thus avoiding the
* pollution of the global namespace. Each component has its own array
* in the global one, allowing storage of arbitrary data indexed by arbitrary
* keys in there. The component-specific arrays are indexed by their
* name.
*
* Note, that this facility is quite a different thing to the component
* context from midcom_application, even if it has many similar applications.
* The component context is only available and valid for components, which
* are actually handling a request. This data storage area is static to the
* complete component and shared over all subrequests and therefore suitable
* to hold default configurations, -schemas and the like.
*
* @var array
*/
private static $_data = array();
public static function get($component, $key = null)
{
if (!array_key_exists($component, self::$_data))
{
self::_initialize($component);
}
if (is_null($key))
{
return self::$_data[$component];
}
else
{
return self::$_data[$component][$key];
}
}
public static function set($component, $key, $value)
{
if (!array_key_exists($component, self::$_data))
{
self::_initialize($component);
}
self::$_data[$component][$key] = $value;
}
/**
* Initialize the global data storage
*/
private static function _initialize($component)
{
self::$_data[$component] = array
(
'active_leaf' => false,
'config' => array(),
'routes' => array()
);
self::_load_configuration($component);
self::_load_routes($component);
}
/**
* Loads the configuration file specified by the component configuration
* and constructs a midcom_helper_configuration object out of it. Both
* Component defaults and sitegroup-configuration gets merged. The
* resulting object is stored under the key 'config' in the
* component's data storage area.
*
* Three files will be loaded in order:
*
* 1. The component's default configuration, placed in $prefix/config/config.inc
* 2. Any systemwide default configuration, currently placed in /etc/midgard/midcom/$component/config.inc.
* 3. Any site configuration in the snippet $GLOBALS['midcom_config']['midcom_sgconfig_basedir']/$component/config.
*
* @see midcom_helper_configuration
*/
private static function _load_configuration($component)
{
$data = array();
$loader = midcom::get('componentloader');
if (!empty($loader->manifests[$component]->extends))
{
$component_path = $loader->path_to_snippetpath($loader->manifests[$component]->extends);
// Load and parse the global config
$parent_data = self::read_array_from_file($component_path . '/config/config.inc');
if ($parent_data)
{
$data = $parent_data;
}
}
$component_path = $loader->path_to_snippetpath($component);
// Load and parse the global config
$component_data = self::read_array_from_file($component_path . '/config/config.inc');
if ($component_data)
{
$data = array_merge($data, $component_data);
}
// Go for the sitewide default
$fs_data = self::read_array_from_file("/etc/midgard/midcom/{$component}/config.inc");
if ($fs_data !== false)
{
$data = array_merge($data, $fs_data);
}
// Finally, check the sitegroup config
$sn_data = self::read_array_from_snippet("{$GLOBALS['midcom_config']['midcom_sgconfig_basedir']}/{$component}/config");
if ($sn_data !== false)
{
$data = array_merge($data, $sn_data);
}
self::$_data[$component]['config'] = new midcom_helper_configuration($data);
}
private static function _load_routes($component)
{
$loader = midcom::get('componentloader');
$component_path = $loader->path_to_snippetpath($component);
// Load and parse the global config
$data = self::read_array_from_file($component_path . '/config/routes.inc');
if (! $data)
{
// Empty defaults
$data = Array();
}
self::$_data[$component]['routes'] = $data;
}
/**
* This helper function reads a file from disk and evaluates its content as array.
* This is essentially a simple Array($data\n) eval construct.
*
* If the file does not exist, false is returned.
*
* This function may be called statically.
*
* @param string $filename The name of the file that should be parsed.
* @return Array The read data or false on failure.
* @see read_array_from_snippet()
*/
public static function read_array_from_file($filename)
{
if (!file_exists($filename))
{
return array();
}
try
{
$data = file_get_contents($filename);
}
catch (Exception $e)
{
return false;
}
return midcom_helper_misc::parse_config($data);
}
/**
* This helper function reads a snippet and evaluates its content as array.
* This is essentially a simple Array($data\n) eval construct.
*
* If the snippet does not exist, false is returned.
*
* This function may be called statically.
*
* @param string $snippetpath The full path to the snippet that should be returned.
* @return Array The read data or false on failure.
* @see read_array_from_file()
*/
public static function read_array_from_snippet($snippetpath)
{
$code = midcom_helper_misc::get_snippet_content_graceful($snippetpath);
return midcom_helper_misc::parse_config($code);
}
}
?>